dropdownlist not changed SelectedValue on server side - javascript

I have an dropdownlist
<asp:DropDownList ID="ddlNationality" runat="server" ClientIDMode="Static">
</asp:DropDownList>
in document ready I set
ddlNationality.selectedIndex = -1;
for show to user nothing selected
After I change value and click some button on server side my selected value is alway zero, after I change selected value in javascript I check if selected values is changed and all i ok, but o server side selected value not changed,.. how to proceed?
I can to insert in this dropdownlist first item with no value and no text, but I want to have in dropdownlist only my values without clear field

looks like you are missing attribute AutoPostBack="true"

When you are not posting back and changing the value of dropdownlist in client, asp.net cannot be informed about it.
Create a hidden field:
<input id="MyHiddenField" type="hidden" runat="server" class="my-hidden-field" value="" />
Whenever you are changing the value in client set the value to hidden field:
$(document).ready(function () {
$('select.my-ddl-class').on('change', function () {
$('.my-hidden-field').val(this.options[this.selectedIndex].value);
});
});
Then, on the code-behind instead of getting the DropDownList selected value, read hidden field value:
selectedValue = int.Parse(MyHiddenField.Value);

Related

how can i use javascript variable value in asp.net button click

Given
<asp:Label ID="lbldistance" runat="server"></asp:Label>
I am assigning it the value with:
var distance = response.rows[0].elements[0].distance.text;
document.getElementById('<%=lbldistance.ClientID%>').innerHTML=distance;
I want to assign lbldistance value in textbox
protected void btnValue_Click(object sender, EventArgs e)
{
txtJSValue.Text = lbldistance.Text;
}
but when i click the btnValue, lbldistance value disappears and i don't see the value in the TextBox..
I am afraid you cannot do that with a label. In ASP.NET state is kept in ViewState across postback. An ASP.NET <asp:Label> is rendered into an HTML span and a span does not have ViewState. Therefore, when you change the innerHTML of the label, you are actually changing the innertHTML of the span tag. Once you press the button, the page is posted to the server where the Label is constructed and it is constructed with the initial text, NOT the one you think it should since it was changed for a span. This (not keeping ViewState for a label), I think, is done for a good reason:
An HTML label should display something to the user and it is not meant to be changed by the user so there is no point in keeping the state across postback.
To accomplish what you want, use a hidden field like this:
<asp:HiddenField ID="HiddenField1" runat="server" />
Your javascript:
var distance = response.rows[0].elements[0].distance.text;
// Assign distance to your label so it shows on the page
document.getElementById('<%=lbldistance.ClientID%>').innerHTML=distance;
// Assing distance to hidden field so you can get it on the server side
document.getElementById('<%=HiddenField1.ClientID%>').value = distance;
Here is how to get the value on the server side:
txtJSValue.Text = this.HiddenField1.Value;
I am not sure why you are going all the way to the server to change the Text of the txtJSValue textbox. You can do that easily on the browser side the same as you are setting the label:
document.getElementById('<%=txtJSValue.ClientID%>').value = distance;

JQuery Function Not Updating Hidden Field

I have two hidden fields for business and home address. They are updated to "true" on the event that the content for the specific textboxes are changed. However, because there are various fields within the div, I used jquery to get all objects of type text within the div. Basically what I'm trying to do is check if the text has changed and then update a hidden field value on my ascx form to true.
InitAddressOnChange: function () {
$('#divBusinessAddress input[type=text]').change(function () {
$("[id$='hiddenBusinessField']").val("true");
});
$('#divHomeAddress input[type=text]').change(function () {
$("[id$='hiddenBusinessField']").val("true");
});
}
Here are my hidden fields:
<asp:HiddenField ID ="hiddenBusinessField" runat="server" value="false"/>
<asp:HiddenField ID ="hiddenHomeField" runat="server" value="false"/>
I tried debugging in the browser but I'm having an issue figuring this out. Is there anything I'm doing wrong here? I'm fairly new to JQuery.

Is there a way to access the checked value of a checkbox in a gridview using javascript

So I have a template field with a checkbox and a hidden field in it.
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkAdd" />
<asp:HiddenField runat="server" ID="hfValue" Value='<%Item.SomeValue%>'/>
</ItemTemplate>
</asp:TemplateField>
Using javascript, I want to able to look through to see which checkboxes are checked and get the corresponding value stored in the the HiddenValue control.
I know how to loop through the rows of a gridview and get a cell's innerText. How would I go about getting a checkbox's checked and a hidden field's value?
IF you want to find hidden field value from gridview on check changed of check box of gridview by javascript then you can use like below function.
function IAmSelected(source, eventArgs) {
if (source) {
// Get the HiddenField ID.
var hiddenfieldID = source.get_id().replace("chkAdd", "hfValue");// here you can add any control of your gridview row which value you want
$get(hiddenfieldID).value = eventArgs.get_value();
}
}

Hidden field value not accessible from javascript

I have hidden field,
<asp:HiddenField ID="HiddenField1" runat="server" Value="" ViewStateMode="Enabled" />
I am setting value of hidden field in code behind file in videoFile_UploadedComplete event (AsyncFileUpload control's event)
HiddenField1.Value = "yespost";
I am trying to access value in javascript,
if (document.getElementById("<%=HiddenField1.ClientID%>").value == "yespost")
Its giving empty string ("").
I am calling script on OnClientUploadComplete event.
Note: First server side event gets executed and client side so value is set first.
Help needed
You want to select by id. Add a '#' before your selector:
if (document.getElementById("#<%=HiddenField1.ClientID%>").value == "yespost")

Using JQuery onblur to set textbox value

I have a textbox with these rules:
1) I populate the textbox.text from a dictionary in session
2) If the user enters a new value, setTextBoxData will save it in the dictionary
3) On entry (on focus) the field text is blanked.
4) On blur, if the field is still empty, I want to set it to the original value.
<asp:TextBox ID="txtNumberEmployees" runat="server" Width="50px" onfocus="this.value='';"
onchange= "javaScript:$(function()setTextBoxData('NUMBEREMPLOYEES','txtNumberEmployees');});"
onblur="javaScript:restore ('txtNumberEmployees', 'NUMBEREMPLOYEES');"/>
The "restore" function referenced above is:
function restore(control, input) {
var data = getInputData(input);
$('#' + control).val(data);
}
getInputData returns the data value correctly. The problem is with the last line.
I have tried many ways to set this, but none seem to work. It should be a simple problem, but I can't get it to work yet.
The problem is ASP.NET will generate an ID that will not be txtNumberEmployees. ASP.NET will generate an ID for your input that will end with txtNumEmployees.
Change this line:
$('#' + control).val(data);
to this:
$('[id$=' + control + ']').val(data);
It will work because this is the Attribute Ends with Selector.
1: Make sure you have no javascript errors. I see there's a missing '{' in the onchange.
2: You can simply pass 'this' as the textbox reference and update it like below:
<asp:TextBox ID="txtNumberEmployees" runat="server" Width="50px" onfocus="this.value='';"
onchange= "javaScript:$(function(){setTextBoxData('NUMBEREMPLOYEES',this);});"
onblur="javaScript:restore (this, 'NUMBEREMPLOYEES');"/>
then simply set the value like:
$(control).val(data);
3: There are other ways as well to grab an asp.net element like shown here. Find ASP.NET ClientID in jquery

Categories

Resources