How to Read the value in a hiddenField from javascript - c#

I want to read a value in the hidden field in javascript/Jquery.How can i acheive this ??

try this
var value=$('#HidennfieldID').val()
value will hold the value of hidden field

It's the same as for a regular field:
document.forms[xxx].elements[yyyy].value
xxx is the name of the form, or its number. yyyy is the name of the element (or its number).
You can also use document.getElementById() to get either the form or the field.

Use the field name or id, e.g.
var fVal = $('[name="name"]').val();
var fVal = $('#id').val();

<input type="hidden" value="12345" id="ixd" name='ixd' />
var val = document.getElementById("ixd").value;

This an example to read and write value just with pure javascript.
<input type="hidden" id="field-id" />
<script type="text/javascript">
document.getElementById('field-id').value = "your value which you want to insert";
alert(document.getElementById('field-id').value);
</script>

Related

#Html.Radiobutton generating long id & name

I have a view that uses the next Model
#model Merak.Models.Product.Options.NumberOfObjectsProductOptionModel
And 2 radiobuttons
#Html.RadioButton(Constants.HtmlControlValues.OptionNumberObjectSelect, #Model.OptionOnePrice, true) <br/>
#Html.RadioButton(Constants.HtmlControlValues.OptionNumberObjectSelect, #Model.OptionTwoPrice)
Inside constants.HtmlControlValues.OptionNumberObjectSelect is a string with "groupname" inside.
I would think that the Html control would generate something like:
<input id="groupname" name="groupname" type="radio" value="50,00">
But instead it generates something like:
<input id="NumberOfObjectsProductOptionModel_groupname" name="NumberOfObjectsProductOptionModel.groupname" type="radio" value="50,00">
Any idea why this behaviour is happening and what I can do to get the short name/id version?
You can assign the constants.HtmlControlValues.OptionNumberObjectSelect to a variable a then get the value:
#model Merak.Models.Product.Options.NumberOfObjectsProductOptionModel
#{
string myControl = constants.HtmlControlValues.OptionNumberObjectSelect
}
And then use it in your control:
#Html.RadioButton(#myControl, #Model.OptionOnePrice, true)
Could you use #Html.RadioButtonFor(model => Model.OptionOnePrice) and see if that works better. That way it's bound to your model.

Mutliple checkbox with same name attribute looping

I have a html form that submits to a C# ashx handler that i'm hoping will insert/update the database
I've written this in PHP and Coldfusion, but I cannot figure out how to do this in C#
HTML form
<form id="copyto">
<input type="hidden" name="operation" value="update" />
<label><input type="checkbox" name="children[]" checked="checked" value="001">
Andrew Regan</label>
<label><input type="checkbox" name="children[]" checked="checked" value="101">
Arthur Regan, III</label>
<input type="checkbox" name="children[]" checked="checked" value="968">
Tim Reagan
</form>
C# ASHX handler
foreach(string key in context.Request.Params["children"])
{
ListDictionary updateParams = new ListDictionary();
updateParams.Add("rowid", key);
string sSql = #"insert into temp select * from children where c.id = :rowid";
dbi.ExecuteNonQuerySql(sSql, updateParams);
}
Typically i would iterate over the $_POST['children'] in php , and execute the sql
How exactly does this translate?
EDIT
ok ive almost gotten this, however my iterator goes over ALL of the request collection variables, i want it to go over only a specific named variable, in this case "children"
i.e localhost/page?operation=update&children=9&children=8&children=17
foreach(string key in context.Request.QueryString)
{
ListDictionary updateParams = new ListDictionary();
updateParams.Add("row_id", context.Request.QueryString[key]);
string sSql = #"insert into dug select :row_id from dual";
dbi.ExecuteNonQuerySql(sSql, updateParams);
}
i want it to ignore everything but the specific var
If you are doing a post. I think something like this would work.
<input type="checkbox" name="children" value="108"/>
<input type="checkbox" name="children" value="109"/>
<input type="checkbox" name="children" value="110"/>
<input type="checkbox" name="children" value="111"/>
The browser will send all of the values comma seperated to the server when the form is submited
Then on your server side you can do this:
var selected = context.Request.Form["children"].Split(',');
Selected will be an array of strings for each value that was passed in by the browser. You can then loop over them and do whatever you need to.
Hope this helps some.
I was just working on this yesterday. I ended up using a hidden field that will hold the multiple checked checkbox id's. So, if that route works for you, you could create a checkboxlist editor template or control. This could have a script such as:
(tempId will hold the common "name" attribute's value for your checkbox/checkboxlist, and we have the "somehiddenfield" hidden field to hold the selected values)
<script>
$(function () {
var arrTmp = [];
//Update array on selection change
$('input[name="#String.Format("{0}[]", tempId)"]').change(function () {
arrTmp = [];
$('input:checked[name="#String.Format("{0}[]", tempId)"]').each(function () { arrTmp.push($(this).val()); });
$('input[id="somehiddenfield"]').val(arrTmp.join(','));
});
});
</script>
Then, on postback on the server-side the form collection will simply have the hidden field we wrote the checked values into. Split that in whatever way works for you (like comma separated in my example) and you're good to go. My server-side is implemented in MVC but for WebForms you can pull the elements from the Request.Form dictionary (Request.Form["somehiddenfield"].ToString()) or even Request.Params as you are using.
Right after i put out the bounty of course -_-
foreach (string k in context.Request.QueryString)
{
if (k.StartsWith("children")){
foreach (string v in context.Request.QueryString.GetValues(k)){
ListDictionary updateParamss = new ListDictionary();
updateParamss.Add("row_id", v);
string Sql = #"insert into dug select :row_id from dual";
dbi.ExecuteNonQuerySql(Sql, updateParamss);
}
}
}

How to set a default value with Html.TextBoxFor (C#) so it is W3C markup compliant?

I am setting a default value with the following code:
#Html.TextBoxFor(m => m.Age, new { #Value = "0"})
The problem is the generated code contains two value attributes, breaking W3C markup validation.
<input Value="0" data-val="true" data-val-required="Age is required." id="Age" name="Age" type="text" value="" />
Notice the capital 'V' and the lowercase 'v' value attributes...
Does anyone know of a work around for the html helper TextBoxFor to only have one value attribute??
Get rid of the "new { #Value = "0"}" and just use the following...
#Html.TextBoxFor(m => m.Age)
The value of the input will be whatever value the Age property of the model is. If you want the default value to be 0, than set the age property of the model to 0.
This happen because the Value is not the the same attribute the value, HTML 5 is case sensitive and the Html.TextBoxFor give the output rightly (considering value is the right and Value a custom).
Try:
#Html.TextBoxFor(m => m.Age, new { #value = "0"})

Pass hidden ID when textbox is changed

I have multiple textboxes, every textbox has a related hidden field, the hidden field's ID is a concatenation of a string with the model's ID (ex: "FormState25")
How can I pass the ID of a hidden field when a textbox being changed? I'm using the following code to detect textbox change:
$("#body-content-container").on('change', 'input[type="text"]', function () {
$("#FormState").val('dirty');
});
You can add custom attributes to the textbox tag itself that includes the Id of the hidden field, for example:
In View
#Html.TextBoxFor(model => model.Name, new { HiddenFieldId = "FormState" + Model.Id })
This way when a textbox get changed you can get the Id of the hidden field you already use to store whatever you want, and then modify the javascript to handle that hidden field's Id, like this:
Javascript
$("#body-content-container").on('change', 'input[type="text"]', function () {
var hiddenId = $(this).attr("HiddenFieldId");
$("#" + hiddenId).val('dirty');
});
The javascript will get the HiddenFieldId attribute of the corresponding hidden field from the textbox and change it's value. Try this and let me know..
You can use this to reference the element on which the anonymous function is defined, e.g.:
$("#body-content-container").on('change', 'input[type="text"]', function () {
$("#FormState" + this.attr('id')).val('dirty');
});
try this:
$("input[type=text]").click(function(){
var hiddenId = "FormState" + $(this).attr("Id");
var hiddenField = $("#" + hiddenId);
hiddenField.val("dirty");
});
EDIT:
if your hidden field rendered just after your inputbox then you can do the following :
$("input[type=text]").click(function(){
var hiddenField = $(this).next();
hiddenField.val("dirty");
});
hope this could help.
Here's another trick which I've used in the past. Enclose the two related input fields in a div (or other) element. Then use the fact that the text field and the hidden field are siblings to select the correct hidden field. Perhaps something like this (NOTE: I have not tested this example):
<div id="someId">
<input type="hidden" value="unchanged" />
<input type="text" value="some data" />
</div>
<script type="text/javascript">
var hiddenInput = $('#someId input:first')
var textInput = $('#someId input:last')
textInput.on('change', function() {
hiddenInput.val('modified');
});
</script>

Html Agility - Get Value by Name Tag

I need to get a value from an input, but the id always change, only the name is the same.
Didn't found anything on google to extract the value by the name tag.
Example:
<input type="hidden" name="data[_Token][key]" value="5aafaee2dd21555c2615fd26c0cccd0f1b2c3018" id="Token749368899" /></div>
I look forward to some answers.
var input= doc.DocumentNode
.Descendants("input")
.First(n=>n.Attributes["name"].Value=="data[_Token][key]");
You can try getting the first part of Id, if it's always Token#####
//input[starts-with(#id, 'Token')]
Try this
var dummy = document.getElementsByName("data[_Token][key]");
This will return you elements which has name "data[_Token][key]".

Categories