Reputation: 883
I have a ascx page where I am using a hidden field to store the value of a the drop down box as it is generated using a google address finder. My problem is that when I try to store the value directly in the hidden field:
hfDdlVerifyID.Value = ddlVerifySS.SelectedValue;
in the event of a button click, the value is stored but on postback is lost again. Whereas, if i try to use Scriptmanager to do it, nothing is stored.
getBuild.AppendLine("$get('" + hfDdlVerifyID.ClientID + "').value = $get('" + ddlVerifySS.ClientID + ").value;");
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "storeHidden", getBuild.ToString(), true);
// Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "storeHidden", getBuild.ToString(), true);
string test = hfDdlVerifyID.Value.ToString();
The ascx page is :
<asp:UpdatePanel ID = ddlUpdate runat="server">
<ContentTemplate>
<asp:Panel ID="pVerify" runat="server">
<br />
<fieldset>
<legend>
<asp:Literal ID="lVerify" runat="server" />
</legend>
<asp:CheckBox
ID ="cbVerify"
runat ="server"
Text ="Use the value from the following list, (Uncheck to accept address as it is)."
Checked ="true" />
<br />
<asp:DropDownList ID="ddlVerifySS" runat="server"
onselectedindexchanged="ddlVerifySS_SelectIndexChange" />
<asp:HiddenField id="hfDdlVerifyID"
runat ="server" />
</fieldset>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<padrap:Button ID ="bVerify"
runat ="server"
CssClass ="btn"
OnClick ="bVerify_Click"
Text ="Verify Address" />
<asp:Button ID ="btnSubSite"
runat ="server"
text ="Save"
CssCLass ="btn"
OnClick ="save_btn_Click_subSite"
onLoad="ddlVerify_Load" />
Upvotes: 3
Views: 13087
Reputation: 148178
The asp button which invokes the ajax call should be in ajax panel. Put the button in the update panel then try again. I hope you will get the value of hidden field.
Upvotes: 2
Reputation: 155
I had the same problem few weeks ago. I solved it using textbox and CSS. Try to use asp:TextBox and CSS property display: none;
This is only my suggestion and it helped me.
Hope it will help you!
Upvotes: 0
Reputation: 230
Usualy, when I have problems like this one(after post back I lost the data) I end finding out that I forgot to put the function where I reset the data on the fields inside a if(!IsPostback)
Upvotes: 1
Reputation: 1598
You should consider using Viewstate. Hope this helps
http://msdn.microsoft.com/en-us/library/bb386448.aspx#Y2000
Upvotes: 1
Reputation: 115
In the past I have had some pretty complex webpages that heavily utilized Ajax. I like HiddenFields because of the ease of use and intellisense. In one case recently I found that, like you my HiddenField was losing it value on postbacks. So I cheated a little… I used the HiddenFields Init event and Unload event to initialize and capture its value in Session. A better solution would be to either fix why it was losing state or switch to Session. I was using the value to manipulate a Multiview ActiveViewState property in a bunch of different areas and did not want to have to rewire or do an in-depth debug trace. Time was an issue.
Protected Sub HiddenFieldActiveView_Init(sender As Object, e As System.EventArgs) Handles HiddenFieldActiveView.Init
If Not Page.IsPostBack Then
HiddenFieldActiveView.Value = "0"
Else
HiddenFieldActiveView.Value = CStr(Session("ActiveViewIndex"))
End If
End Sub
Protected Sub HiddenFieldActiveView_Unload(sender As Object, e As System.EventArgs) Handles HiddenFieldActiveView.Unload
Session.Add("ActiveViewIndex", HiddenFieldActiveView.Value)
End Sub
So the firsttime the page loads the HiddenField is initialized with the value of 0, then on subsequent initializations it loads the value from Session.
This may not be the prettiest or smartest solution. But it worked for my situation.
~Ian
Upvotes: 0
Reputation: 309
Does your this code work if you remove the UpdatePanel?
Also, the code you posted for the getBuild call is missing a closing single quote in the call to $get for the item ddlVerifySS.ClientID. Is this a typo entering this here, or a problem in your code? If it is the latter, this would explain why you don't see the value copied when you use the script manager.
Upvotes: 0