Hidden field value not accessible from javascript - 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")

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.

dropdownlist not changed SelectedValue on server side

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);

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

ASP.NET hidden field not updating after postback

I have some code on my ASP page which looks like this:
<asp:UpdatePanel runat="server" id="updatepanel1" UpdateMode="Conditional" onload="updatepanel1_Load" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:HiddenField id="sendingRequest" runat="server" Value="0" />
....
</ContentTemplate>
</asp:UpdatePanel>
I also have some javascript on my page which does this, to trigger the update of the updatepanel:
var sendingRequest = document.getElementById("<%=sendingRequest.ClientID%>");
sendingRequest.value = "1";
__doPostBack('<%= updatepanel1.ClientID %>', '');
Everything works fine up to now, but in my updatepanel1_Load event, I try to set the value back to "0" :
sendingRequest.Value = "0";
This value never gets updated and set back to 0 on the client after the postback, and I can't figure out why!
Can anyone help? Thanks
If you're having problems with a hidden field, you could use a TextBox instead. Hide the textbox with css (display: none;) to achieve similar results to a hidden field. Its not exactly pretty, but its a workable workaround.
Try to call registerstartupscript or something like that from server side. I can't remember exactly the method name but its part of page object. This will register any javascript you would like to execute after postback on the client side.
This similar scenario is done here successfully:
http://encosia.com/easily-refresh-an-updatepanel-using-javascript/
Ensure you are following the same steps - I can't see all of your code. Try with a label first to make sure it gets updated as a visible control. If that works then narrow it down with your hidden value to make sure the behavior isn't different for a hidden control.
I had an issue with three HiddenFields being set in Code-Behind, but their values were not set when polled from JQuery.
My issue turned out being that my Master Page uses an UpdatePanel, and in my ASP.Net Init event I was purposing that UpdatePanel with conditional rendering.
Private Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
mstr = CType(Master, Site)
'setup partial rendering so Log can update asynchronously
scriptManager = CType(mstr.FindControl("ScriptManager1"), ScriptManager)
scriptManager.EnablePartialRendering = True
scriptManager.AsyncPostBackTimeout = 28800
CType(mstr.FindControl("UpdatePanel1"), UpdatePanel).UpdateMode = UpdatePanelUpdateMode.Conditional
CType(mstr.FindControl("UpdatePanel1"), UpdatePanel).ChildrenAsTriggers = False
End Sub
The issue was that I forgot to then call update on my panel after setting the HiddenFields. I had to do this because my button was a partial-postback control (UseSubmitBehaviour=False)
hfParams.Value = paramlist.ToString()
hfForms.Value = formlist.ToString()
hfStartJob.Value = "True"
CType(mstr.FindControl("UpdatePanel1"), UpdatePanel).Update()

Categories

Resources