Preferred way to access control text in postback

I make a site, and on the whole site I do not really agree with how I get user input for postback. For example, say in a button event, which takes two lines extracted from text fields and combines them and displays the sum of the line in the label:

protected void btnCalculate_Click(object sender, EventArgs e)
{
    string text1 = textBox1.Text;    //one way
    string text2 = Request["textBox2"];    //the other way
    lblSum.Text = text1+text2;
}

I assume that you will want to use Request [""] if the data was sent to a new page, but for this situation one of the methods is preferable to the other, and why?

+3
source share
3 answers

ASP 3.0 ASP.NET - " ", .. Control.Text, Request [] , .

+3

:

textBox1.Text;

: Session, Context, PreviousPage. ASP.NET

,

String.Concat(text1, text2);

String.Format("{0}{1}",text1, text2);
+2

Request[] -. , , .

For any control that does not just contain text, you will access unstructured data, not access to the updated properties of the control to which it is bound.

In any case, you really should not post from one page to another in ASP.NET - this destroys the architecture, and there are not many reasons why you would like to do this in most situations.

+1
source

Source: https://habr.com/ru/post/1795816/


All Articles