views:

158

answers:

4

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 Request.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)...
A: 

Take a look at the cross-page posting mechanism provided by Asp.net: http://msdn.microsoft.com/en-us/library/ms178139.aspx

This may explain what you need :)

Juri
+3  A: 

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;
    }
}
Wil P
This looks like a more stable solution than mine. I'll try it and vote accordingly. How is the performance on .Findcontrol? Is there a way to reference the control directly - ie Page.PreviousPage.txtInfo?
Praesagus
It's been a while since I've done ASP.NET but IIRC you can cast your Page.PreviousPage as your web form class. On your web form class you could expose the TxtInfo.Text value or the TextBox control all togeher. Then all you'd have to do is something like (Page.PreviousPage as MyInfoWebForm).MyTxtInfoTextBox.Text to access the value. I would recommend only exposing the value of Text if possible. If this is unclear just let me know.
Wil P
After looking at your example though, if you are not doing cross page posting all you need to do is Request.Form["txtInfo"] as mentioned by others. Additionally, if the <asp:TextBox ID="txtInfo" runat="server"/> is defined in your page then all you have to do in code behind is access the value like txtInfo.Text. You wouldn't need to do a Request.Form["txtInfo"]...
Wil P
+2  A: 

What's wrong with...

string txtInfo = Request.Form["txtInfo"];
if(txtInfo == null) txtInfo = "";
CptSkippy
I think that he's posting to a different page vs. just a regular post back. Maybe I misread?
Wil P
A complicated <input> name, generated by asp:TextField, is what's wrong
orip
Ah I gotcha - w/ all the naming container stuff. I see the prob now. Coo - thanks for your reply :)
Wil P
A: 

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"];
orip