[SOLVED] Assign int value to Hidden field

Issue

I need to assign int value to hidden field.But it fails

Here goes my code

ASP.NET

<asp:HiddenField ID="hdnCId" runat="server" />

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        int Id= 0;
        if (Request.QueryString["Id"] != "" && Request.QueryString["Id"] != null)
        {
            ChurchId = Convert.ToInt32(Request.QueryString["Id"]);
        }
        else
        {
            Id = -1;
        }

        hdnCId.Value =Id;
    }
}

Solution

Hidden fields only takes strings

if (Request.QueryString["Id"] != "" && Request.QueryString["Id"] != null)
        {
            hdnCId.Value = Request.QueryString["Id"].ToString();
        }

Just this will do

convert the value to int when using it

//when there is some value in it

Int32 Id = Convert.ToInt32(hdnCId.Value);

Answered By – Vinay Pratap Singh Bhadauria

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *