Populate Hidden field on post back and retrieve value from Hidden Field

Discussion in 'ASP .Net' started by Rick, Mar 24, 2010.

  1. Rick

    Rick Guest

    Can someone help me? I have a HTMLSelect control that is populated on the
    Page_Load event. I want to basically when the user makes a selection to
    populate session variables upon an Item change in the HTMLSelect. I have
    tried just about everything I've read and can think of, but have not been
    able to get this to work.
    Currently I get a script error that Hidden1 field is undefined after the
    post back.

    My code:
    ASPX:
    <script language="javascript" type="text/javascript">
    function dothepostback()
    {
    //ddCustList gets renamed
    var selval = document.getElementById("_ctl2_ddCustList").value;
    Hidden1.value = selval;
    //Post back the page
    document.forms[0].submit();
    }
    </script>

    <table cellpadding="0" cellspacing="0" width="100%" class="TopNavMenu">
    <tr>
    <td>
    <CYBERAKT:ASPNETMENU id="ASPNetMenu"
    clientscriptlocation="/aspnetmenu_client/" menustyle="ClassicHorizontal"
    runat="server"></CYBERAKT:ASPNETMENU>
    </td>
    <td align="right">
    <input type="text" id="Hidden1" name="Hidden1" value="" runat="server"
    enableviewstate="true" />
    Choose Customer:<select id="ddCustList" runat="server"
    onchange="dothepostback()" enableviewstate="true"><option></option></select>
    </td>
    </tr>
    </table>

    Code Behind to get Hidden Field Value:
    Dim hdnSearch As New HtmlInputText
    Dim x As String
    hdnSearch = FindControl("Hidden1")
    x = hdnSearch.Value

    Session("HIDVal") = x

    I cannot use an asp DropDownList control because it requires a Form tag with
    runat="server", which you cannot have multples in a web page Tthe web site
    I'm working with was initially built to Inherit different parts of a page
    and then put it all together at the end (Not well planned out and to change
    it would be a massive undertaking). So other parts of the page may already
    have the Form tag with runat="server"

    Thanks in Advance!
    Rick, Mar 24, 2010
    #1
    1. Advertising

  2. Re: Populate Hidden field on post back and retrieve value from HiddenField

    On Mar 24, 9:17 pm, "Rick" <> wrote:
    > Can someone help me? I have a HTMLSelect control that is populated on the
    > Page_Load event. I want to basically when the user makes a selection to
    > populate session variables upon an Item change in the HTMLSelect. I have
    > tried just about everything I've read and can think of, but have not been
    > able to get this to work.
    > Currently I get a script error that Hidden1 field is undefined after the
    > post back.
    >


    Well, you need to find Hidden1 field exactly as you do it for selval

    instead of

    var selval = document.getElementById("_ctl2_ddCustList").value;
    Hidden1.value = selval;

    do

    var selval = document.getElementById("_ctl2_ddCustList").value;
    var h = document.getElementById("<%=Hidden1.getClientID()%>");

    You can't use Hidden1 by referring to its ASP.NET's id from js because
    it has another id on the client. Just open your result page in source
    view in the browser and you will what I mean. To fix it I used
    getClientID() which should return it.

    Hope this helps
    Alexey Smirnov, Mar 25, 2010
    #2
    1. Advertising

  3. Rick

    Rick Guest

    Thanks! That solved the issue with the HiddenField Error, but when I get to
    the code behind, the hidden field value is gone. Viewstate does not appear
    to be working. Any suggestions?

    function dothepostback() {
    //Get Drop down list

    var seldd = document.getElementById("<%=ddCustList.UniqueID()%>");

    //Get Hidden Field

    var hidctl = document.getElementById("<%=Hidden1.UniqueID()%>");

    //Set Hidden field to selected item

    hidctl.value = seldd.value;


    //post back the page

    document.forms[0].submit();

    }

    <table cellpadding="0" cellspacing="0" width="100%" class="TopNavMenu">

    <tr>

    <td>

    <CYBERAKT:ASPNETMENU id="ASPNetMenu"
    clientscriptlocation="/aspnetmenu_client/" menustyle="ClassicHorizontal"
    runat="server"></CYBERAKT:ASPNETMENU>

    </td>

    <td align="right">

    <input type="hidden" id="Hidden1" name="Hidden1" runat="server"
    enableviewstate="true" />

    Choose Customer:<select id="ddCustList" onchange="dothepostback()"
    runat="server" enableviewstate="true"><option></option></select>

    </td>

    </tr>

    </table>

    Code behind:

    Dim hdnSearch As New HtmlInputHidden

    Dim x As String

    hdnSearch = FindControl("Hidden1")

    x = hdnSearch.Value

    "Alexey Smirnov" <> wrote in message
    news:...
    On Mar 24, 9:17 pm, "Rick" <> wrote:
    > Can someone help me? I have a HTMLSelect control that is populated on the
    > Page_Load event. I want to basically when the user makes a selection to
    > populate session variables upon an Item change in the HTMLSelect. I have
    > tried just about everything I've read and can think of, but have not been
    > able to get this to work.
    > Currently I get a script error that Hidden1 field is undefined after the
    > post back.
    >


    Well, you need to find Hidden1 field exactly as you do it for selval

    instead of

    var selval = document.getElementById("_ctl2_ddCustList").value;
    Hidden1.value = selval;

    do

    var selval = document.getElementById("_ctl2_ddCustList").value;
    var h = document.getElementById("<%=Hidden1.getClientID()%>");

    You can't use Hidden1 by referring to its ASP.NET's id from js because
    it has another id on the client. Just open your result page in source
    view in the browser and you will what I mean. To fix it I used
    getClientID() which should return it.

    Hope this helps
    Rick, Mar 25, 2010
    #3
  4. Re: Populate Hidden field on post back and retrieve value from HiddenField

    On Mar 25, 6:40 pm, "Rick" <> wrote:
    > Thanks! That solved the issue with the HiddenField Error, but when I get to
    > the code behind, the hidden field value is gone. Viewstate does not appear
    > to be working. Any suggestions?
    >
    > function dothepostback() {
    > //Get Drop down list
    >
    > var seldd = document.getElementById("<%=ddCustList.UniqueID()%>");
    >
    > //Get Hidden Field
    >
    > var hidctl = document.getElementById("<%=Hidden1.UniqueID()%>");
    >
    > //Set Hidden field to selected item
    >
    > hidctl.value = seldd.value;
    >
    > //post back the page
    >
    > document.forms[0].submit();
    >
    > }
    >
    > <table cellpadding="0" cellspacing="0" width="100%" class="TopNavMenu">
    >
    > <tr>
    >
    > <td>
    >
    > <CYBERAKT:ASPNETMENU id="ASPNetMenu"
    > clientscriptlocation="/aspnetmenu_client/" menustyle="ClassicHorizontal"
    > runat="server"></CYBERAKT:ASPNETMENU>
    >
    > </td>
    >
    > <td align="right">
    >
    > <input type="hidden" id="Hidden1" name="Hidden1" runat="server"
    > enableviewstate="true" />
    >
    > Choose Customer:<select id="ddCustList" onchange="dothepostback()"
    > runat="server" enableviewstate="true"><option></option></select>
    >
    > </td>
    >
    > </tr>
    >
    > </table>
    >
    > Code behind:
    >
    > Dim hdnSearch As New HtmlInputHidden
    >
    > Dim x As String
    >
    > hdnSearch = FindControl("Hidden1")
    >
    > x = hdnSearch.Value
    >
    > "Alexey Smirnov" <> wrote in message
    >
    > news:...
    > On Mar 24, 9:17 pm, "Rick" <> wrote:
    >
    > > Can someone help me? I have a HTMLSelect control that is populated on the
    > > Page_Load event. I want to basically when the user makes a selection to
    > > populate session variables upon an Item change in the HTMLSelect. I have
    > > tried just about everything I've read and can think of, but have not been
    > > able to get this to work.
    > > Currently I get a script error that Hidden1 field is undefined after the
    > > post back.

    >
    > Well, you need to find Hidden1 field exactly as you do it for selval
    >
    > instead of
    >
    > var selval = document.getElementById("_ctl2_ddCustList").value;
    > Hidden1.value = selval;
    >
    > do
    >
    > var selval = document.getElementById("_ctl2_ddCustList").value;
    > var h = document.getElementById("<%=Hidden1.getClientID()%>");
    >
    > You can't use Hidden1 by referring to its ASP.NET's id from js because
    > it has another id on the client. Just open your result page in source
    > view in the browser and you will what I mean. To fix it I used
    > getClientID() which should return it.
    >
    > Hope this helps


    The easiest way to debug js is to put alert(); in the code. Try to get
    on what step you get wrong value.
    Alexey Smirnov, Apr 13, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin
    Replies:
    2
    Views:
    4,893
    Kevin McGovern
    Feb 25, 2004
  2. =?Utf-8?B?Z29yaWxsYQ==?=
    Replies:
    1
    Views:
    750
    Masudur
    May 25, 2007
  3. Enjoy Life
    Replies:
    2
    Views:
    257
    Roland Hall
    Feb 23, 2005
  4. GavMc
    Replies:
    4
    Views:
    311
    Evertjan.
    Sep 22, 2005
  5. Rauf ul Hassan

    Post a hidden field value

    Rauf ul Hassan, Aug 20, 2003, in forum: Javascript
    Replies:
    1
    Views:
    132
    Andy Fish
    Aug 20, 2003
Loading...

Share This Page