6
Answers

How to access hidden field value in the code-behind

Kriti

Kriti

14y
31.6k
1

My page.ascx is
<body>
<form>
<div>
<
ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value="" />
<asp:DropDownList id="dd" runat="server" onselectedindexchanged="dd_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Value="Pending">Pending</asp:ListItem>
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</div>
</form>
</body>
SCRIPT is
function ConfirmIt()
{
if (confirm("do u want to notify the changes to the user?") == true)
{
//Set the Hidden Field Value
document.getElementById(
"HiddenField1").value = "1";

}
else
{
document.getElementById(
"HiddenField1").value = "0";
}
}
 
My code-behind is-
protected
void dd_SelectedIndexChanged(object sender, System.EventArgs e)
{
//this.ClientScript.RegisterClientScriptBlock(this.GetType(),"", "ConfirmIt()");
Page.RegisterClientScriptBlock(
"", "<script>ConfirmIt()</script>");
/*Now Access the Hidden Filed Value Since it Contain the value that
User Clicked OK or Cancel */

int result = int.Parse(HiddenField1.Value.ToString());
if (result == 1)
{
// Do somthing
}
}
 
Error is
The name 'HiddenField1' does not exist in the current context
FYI: class starts like this
public
class A: System.Web.UI.UserControl
{/code goes}
How should i rectify the following error:The name 'HiddenField1' does not exist in the current context?
Thanks

Answers (6)