开发者

c# how to retrieve post data

开发者 https://www.devze.com 2022-12-08 09:08 出处:网络
If I have a textbox txtInfo in a form that I submit via post, if I post back to the page I can read the value entered in the textbox using txtInfo.Text.What if I am posting to a different page?Do I ha

If I have a textbox txtInfo in a form that I submit via post, if I post back to the page I can read the value entered in the textbox using txtInfo.Text. What if I am posting to a different page? Do I have to parse Reque开发者_如何学编程st.Form for the control name mutilations (which is what I am doing now) or can I get it from that mess .net passes around as state?

Thanks


Thank you for the answers so far... Sorry I should have been a little more clear. This control is a runat="server" control. This is what I am relegated to now - not very pretty.

    foreach (String key in page.Request.Form.AllKeys)
    {
        String[] controlName = key.Split('$');//remove that horrrible .net naming - thanks Bill.
        keyName = controlName[controlName.Length - 1];//get the last value so we always have the name
        keyValue = page.Request.Form[key];
        if (keyValue != "")
        {
            switch (keyName)...


You should look into Cross Page Postbacks.

As noted on this page you can easily get access to txtInfo using the following:

if (Page.PreviousPage != null)
{
    TextBox SourceTextBox = 
        (TextBox)Page.PreviousPage.FindControl("txtInfo");
    if (SourceTextBox != null)
    {
        Label1.Text = SourceTextBox.Text;
    }
}


What's wrong with...

string txtInfo = Request.Form["txtInfo"];
if(txtInfo == null) txtInfo = "";


A simple solution would be use a simple <input type="text"> instead of an <asp:TextBox>. Give it a name attribute and then access it via Request.Form.

.aspx file:

<input type="text" name="foo" />

Posted-to code-behind (same page, different page, doesn't matter):

var text = Request.Form["foo"];
0

精彩评论

暂无评论...
验证码 换一张
取 消