How do I implement the Post/Redirect/Get pattern in asp.net WebForms?

后端 未结 2 632
闹比i
闹比i 2020-12-10 08:52

So I understand the basics of the prg pattern. But I haven\'t come across a technique for making arbitrary data available to the \"get\" instance of a page.

For exa

2条回答
  •  感情败类
    2020-12-10 09:11

    I think you can still use the get method in the form element. In this case you won't be able to use the ID of your control in normal way. But you can use the Request.Params collection to get the viewstate.

    Update: Sorry, I just tried again. and found that you can access your server control by its ID in codebehind. like:

    Response.Write(text1.Text)
    

    see the example:

    the aspx page(only the form element):

    < form id="form1" runat="server" method="get">
    < div>
    < asp:TextBox ID="text1" runat="server" />
    < asp:Button ID="button1" runat="server" OnClick="buttonClick" />
    < div>
    < form>
    

    NOTE: I used a space before each" <" otherwise the code above will not be visible.

    the code behind page (only button click event):

    protected void buttonClick(object sender, EventArgs e)
        {
            string text = Request.Params["text1"];
    
            Response.Write(text);
        }
    

    when you click the button the url will look like this:

    http://localhost:1157/WebSite1/Default.aspx?__VIEWSTATE=%2FwEPDwUKMTkwNjc4NTIwMWRkoJEtEHZ8lHQ53QllSkz8ncpEw80%3D&__EVENTVALIDATION=%2FwEWAwKf%2BPPUCwKTjKGwCgKs34rGBvus9oxN%2FQkHlkzpKUEwrbxHLM6u&text1=ashish&button1=

提交回复
热议问题