Hidden field has lost its value in postback

I have an ascx page where I use a hidden field to store the value of a drop down list as it is created using google search device. My problem is that when I try to store the value directly in a hidden field:

hfDdlVerifyID.Value = ddlVerifySS.SelectedValue;

if the button is pressed, the value is saved, but is lost again in the reverse gear. If I try to use the Scriptmanager for this, nothing is saved.

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

Ascx page:

<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" />
+3
source share
6 answers

You should consider using Viewstate. Hope this helps

http://msdn.microsoft.com/en-us/library/bb386448.aspx#Y2000

+1

asp, ajax, ajax. , . , .

+2

, , ( , ), , , reset if(!IsPostback)

+1

, UpdatePanel?

, , getBuild, $get ddlVerifySS.ClientID. , , ? , , , script.

0

-, Ajax. HiddenFields - intellisense. , , , HiddenField . ... HiddenFields Init Unload . , . , Multiview ActiveViewState . .

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

, HiddenField 0, .

This may not be the most beautiful or smartest solution. But it worked for my situation.

~ Ian

0
source

I had the same problem a few weeks ago. I solved this with a text box and CSS. Try using asp: displaying TextBox and CSS properties: none;

This is just my suggestion and it helped me.

Hope this helps you!

0
source

Source: https://habr.com/ru/post/1748911/


All Articles