I am using asp.net 2.0
I set a hidden text box from javascript and on postback, i want to check the value stored on the text box in page init event. Here is what i tried
string 开发者_高级运维t = Request.Form["currentMode"].ToString();
but i get an error saying " Object reference not set to an instance of an object."
Any idea?asp.
In init the postback values have not been loaded into the controls yet, you could in theory fish the values from Request.Form variables, but they are named as the client ID of the controls. If your controls are in any container like a contentplaceholder or detailsview they will have various guff prepended on the ID. e.g. ctl00$cphContentBody$txtMyTextbox
could be the id of a control that has a server side id of txtMyTextbox
.
Init is the page life cycle where everything is initialized. You can't thrust that anything is availble. In order to do that you have to use the Page_Load event...
I came across this the other day while trying to build->submit->retrieve values from a dynamically created form (assembled during the Init() event of a postback). As Ben stated, you can use the Request.Form
collection to pull out the values of the page's controls manually, using the UniqueID
of the desired control (which will contain the "guff" Ben is talking about).
Of course, this is only necessary when trying to discover the value of a page's control before it is populated from viewstate (before LoadComplete). Otherwise, you could simply do something like TextBox1.Text
.
精彩评论