views:

16

answers:

1

hi.

suppose you save data into a dynamic hidden field ,which is created dynamically during the handling of some postback event.

what is the best way to retrieve it from this field upon a postback, (besides searching the request for the key of this hidden field and then retrieving the corresponding value as in the code below)?

protected void Button2_Click(object sender, EventArgs e)
{
    bool found = false;
    for (int i=0; i<this.Request.Form.Keys.Count; i++)
    {
        string item = this.Request.Form.Keys[i];
        if ( item=="Hidden1")
        {
            Literal6.Text = Request.Form.GetValues(i)[0];
            found = true;
        }
    }

    if (found==false)
    {
        Literal6.Text = "Hidden1 is not found";
    }

}
A: 

you could do like this:

    Literal6.Text = "Hidden1 is not found";
    if (Request.Form.HasKeys() && Request.Form.AllKeys.Contains("Hidden1"))
    {
        Literal6.Text = Request.Form.GetValues("Hidden1")[0];
    }

but you can also use the findControl method. That is, if an element has an registered id... forgot to say that, since findcontrol takes the control ID, and GetValues determines the control by name. (which is not likely in your example ;)

Caspar Kleijne
very nice, thanks!
alex440