Note: This article uses inputid as the control ID, name of the inputname control, value of the inputid. Text control, urlpramaname URL parameter name, And bindform () binding method. Some pseudoCode, And such "undeclared" code. Try to reach out to words.
Request. Form
Some may ask why you use the request. form ["inputname"]. We use inputid. text, we only use request. querystring ["urlpramaname"] to obtain the URL parameter value. In fact, both methods are acceptable, and the ID and name values in the HTML code generated by the control are also the same (in fact, this is not good, I will talk about it later ). After you click the submit button, the page is reloaded and a value is assigned to the control. Therefore, inputid. Text can obtain the value of a specific control.
The problem is how controls are assigned values? Do you know? Ispostback
Why is ispostback false when the page is loaded for the first time, but it becomes true after the page is submitted? in the previous section, I didn't mean that post is also a page request. How can I perform two requests but the ispostback value changes?
After the form is submitted, the browser will post the value of the Form Control to the page pointed to by the form action (usually its own page ). At this time, after IIS receives the request and post data, it will know that the data is submitted, rather than being opened for the first time (refreshing is actually the same as opening for the first time), with no data. So,
Background class (pagename. aspx. CS) In
Executing Ispostback is set to true!
"Is post back" means "whether to submit (request )" .
Therefore, when we write a button click event
Page_load Methods are often used
If (! Ispostback) bindform (); // Assign values to all controls in the form . This means that after submission, I will not bind the form, but use the specific event Method of click.
Why does the button not execute the click event during refresh, but after submission, it will work? Because the Click Event of the button does not have a subscription method when the button is loaded for the first time (normal request), after clicking submit, the Click Event of the button subscribes to the specific event method. Like ispostback, the button member is assigned a value.
Bindform
Note] Why not bind a form? In fact, when you visit csdn, you will find that many people will ask a question-why did he explicitly modify the value of the control, but the data has not changed after submission? Then many people reply, adding an ispostback to judge! Do you know why you want to add it? Well, if you execute bindform again, the value of the control will be re-assigned, and the old value will be obtained when you run the button event again.
So, why do I not execute bindform, and the controls on the form page still have values after submission (refresh? Isn't that strange?
In fact, this is of course a value. Otherwise, when you write records to the database, inputid. where does text data come from, and your bindform is nothing more than inputid. TEXT = "XXX :).