views:

934

answers:

5

First I'll apologize for the unclear title of my question. I wasn't sure how to succinctly describe my problem in a title.

I have a hidden field in my .aspx

<input type="hidden" name="hid1" value="0" />

I want to set the value of this field during the page load event, and if it is not a postback.

protected void Page_Load(object sender, EventArgs e) {
    if (!Page.IsPostBack) {

        // This doesn't work!
        Request.Form["hid1"] = "1";

    }

    if (Page.IsPostBack) {

        // This DOES work!
        Request.Form["hid1"] = "1";

    }
}

The problem is that the Request doesn't contain the hidden field in the Form array during the page load event when it's not a postback (ie - the first time the page is hit). Subsequent hits to the page (ie - postbacks) result in the Form array containing the hidden field.

I'm sure that it has to do with the lifecycle of the page, but I really need to know, how do I set the hidden field during the page load event and when it is not a postback?!

EDIT: I really, really don't want to incorporate the runat="server" attribute!

+1  A: 

Why don't you make it a server control by setting 'runat="server"' on the input control? Then it will be accessible from your code behind, and you will be able to set the value during the first page load.

tomlog
In a nutshell, because I need to use the hidden field within some JQuery functions, and I wanted to keep the id from getting mangled due to runat="server". If I can't figure it out any other way, I'll go this route, but I'd be **very** surprised to find out there's not a way to access the hidden field from the page load on the initial page hit.
Jagd
@Jagd -- this is one of my great fears of JQuery + ASP.NET. Use an idiom that looks at what your id ends with, like this: startDatePicker = $("input[id$='TextVisitStartDate']").datepicker(); TextVisitStartDate is an asp.net TextBox control.
Bob Kaufman
@Bob - Yeah, that's typically what I do (id$='_whatever'), but I was trying to get away from that this one time. Sure seems like a lot of work though, and I'm beginning to think it's not worth the pain.
Jagd
Surely you can set a JavaScript/JQuery variable with the ClientId of the control during the page load. That way you always have the exact control id to use in your scripts.
tomlog
+1  A: 

Try converting the input into a HiddenField control (or, at least, a runat="server" input), and reference it by it's ID, rather than through Request.Form.

bdukes
+2  A: 

Instead of:

<input type="hidden" name="hid1" value="0" />

try this:

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

Then in your Page_Load()

hid1.Value = "whatever...";

It will be visible both before and after postback when you declare it in this manner.

Bob Kaufman
Negative, Houston. See my reponse to tomlog's answer. This would work, but it's not the solution that I want. runat = mangled id's
Jagd
i am not sure about JQuery, but if you use ClientId property, you should able to use that id in your JQuery.
Neil
+1  A: 

why dont you access that field through a style class and use runat server=?

Pablo Castilla
I'm not sure what you mean by accessing it through a style class. Can you clarify or give me an example of how to do so?
Jagd
@Jagd - you can add an arbitrary class to your hidden input element which you can use as a pseduo-id that asp.net won't mess with. For example: `<input type="hidden" name="hid1" value="0" class="hid1" />`. Then you can access it in jQuery using the class selector syntax without sweating Asp.Net's generated ids: `$(".hid1").function()`
Jeff Sternal
thats the idea.
Pablo Castilla
+3  A: 

You could define a property in your page class and then modify the property value in your code:

    protected string HiddenFieldValue { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            HiddenFieldValue = "postback";
        else
            HiddenFieldValue = "not postback";
    }

Then define the hidden form field like this so that it's value is set to the property value:

    <input type='hidden' id='hidden1' value='<%=HiddenFieldValue %>' />

If you only want to set the value form the property during a postback or non-postback you could add the condition as well:

    <input type='hidden' id='hidden1' value='<% if(IsPostBack) { %> <%=HiddenFieldValue%> <% } %>' />
Venr
Bam! That did it. Thanks for thinking outside of the box, Venr.
Jagd