Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get posted name value pairs from non asp form

Status
Not open for further replies.

JimWh

IS-IT--Management
Jul 15, 2009
10
US
I've got an application that submits a form using method=post. Having a tough time accessing these values in asp.net / c#.
Just to clarify a regular html form submits to test.aspx using post method. in the form-load section of the c# I attempt to query the value.
Ex.
Keeping it simple -
Label2.Text = Request.Form("Text1").ToString();
Text1 is the name of the form element in the submitting form.

----------------------------------------------------------
using example from MS below results in blank string in label1
System.Text.StringBuilder displayValues =
new System.Text.StringBuilder();
System.Collections.Specialized.NameValueCollection
postedValues = Request.Form;
String nextKey;

for (int i = 0; i < postedValues.AllKeys.Length; i++)
{
nextKey = postedValues.AllKeys;
if (nextKey.Substring(0, 2) != "__")
{
displayValues.Append("<br>");
displayValues.Append(nextKey);
displayValues.Append(" = ");
displayValues.Append(postedValues);
}
}
Label1.Text = displayValues.ToString();

}
 
I just created a quick test and had no problem. You are submitting using an asp page with a posting to your aspx page?

Can you post the HTML of your .asp page?
 
HTML not asp posting to aspx file
filename=testpost.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns=" >
<head>
<title>Untitled Page</title>
</head>
<body>
<FORM
ACTION=" METHOD="POST">
<input id="Text1" type="text" />&nbsp;<br />
<input id="Text2" type="text" />
<br />
<input id="Submit2" type="submit" value="submit" />
</form>

</body>
</html>
 
filename = testpost.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.Text.StringBuilder displayValues =
new System.Text.StringBuilder();
System.Collections.Specialized.NameValueCollection
postedValues = Request.Form;
String nextKey;

for (int i = 0; i < postedValues.AllKeys.Length; i++)
{
nextKey = postedValues.AllKeys;
if (nextKey.Substring(0, 2) != "__")
{
displayValues.Append("<br>");
displayValues.Append(nextKey);
displayValues.Append(" = ");
displayValues.Append(postedValues);
}
}
Label1.Text = displayValues.ToString();
//System.Collections.Specialized.NameValueCollection
Label2.Text = Request.Form["Text1"];

}
}
 
filename = testpost.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testpost.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns=" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
&nbsp;Label1 &nbsp;
<br />
<asp:Label ID="Label1" runat="server" Height="449px" Text="Label" Width="809px"></asp:Label><br />
Label2<br />
<asp:Label ID="Label2" runat="server" Height="82px" Text="Label" Width="251px"></asp:Label></div>
</form>
</body>
</html>
 
Code:
ACTION="[URL unfurl="true"]http://backup2//PayPalAspNetNvpSamples/AspNet/testpost.aspx?paymentType=Authorization"[/URL]
You action is wrong, it shold post to your testpost.aspx page
 
I dropped the query string from the post action.

<FORM ACTION=" METHOD="POST">

Still getting nothing showing up in those labels. If I use Label2.Text = "123".ToString(); then 123 does show in label2. Seems it must be something to do with the request.form

I'm really new to asp.net and c# - I've done some work with classic asp but I don't have a good understanding of the dependencies or many of the classes. Could the server config be preventing the passing of post args?
 
it's just the backend. php, .net, asp, java doesn't matter. you are submitting a form over http.

Change your code up some
Code:
var form = Request.Form;
lable1.Text = "items in form " + form.Keys.Count.ToString;

var builder = new StringBuilder();
foreach(var kvp in form)
{
   if(kvp.Key.StartsWith("__")) 
      continue;

   builder.AppendFormat("{0} = {1} | ", kvp.Key, kvp.Value);
}
lable2.Text = builder.ToString();



Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Jason,
Thanks for the code but it's a no go, don't even know what var is, maybe it's VB.

Hey all,
Let's distill this to it's simplest form. The following statement is about as basic a test as I can imagine.

Label2.Text = Request.Form["Text1"];

The result gives no error but it gives no value either. Does anyone have a suggestion on what might be wrong?
 
it's .net 3.0 syntax. if you're running 2.0 you will need to downgrade the syntax that's all. exchange var for the explicit types: string and KeyValuePair.


Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Should have had a .ToString() on that statement
Label2.Text = Request.Form["Text1"].ToString();

Using that statement I get the following error.
System.NullReferenceException: Object reference not set to an instance of an object.

Do I need to invoke some object prior to the request.form?
 
Thanks Jason,
That one sentence is incredibly helpful to me. It's tough being a newbie, basically I've got to get a Credit card application converted from tango / pearl to asp.net. I've got most of the code provided by paypal just need to tie the current database driven web forms into the .net code figuring that will be much faster than rewriting the DB stuff in .net too. I'm on the slow road to learn this language but need a leg up to get this done in the near future.
I really appreciate the help and do realize my questions are a pia to some.
Jim
 
Do I need to invoke some object prior to the request.form?
nope. Request.Form["Text1"] is null.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
I've got one of the code sections working in .net 2.0

System.Collections.Specialized.NameValueCollection formv = Request.Form;
Label1.Text = "items in form " + formv.Keys.Count.ToString();

Unfortunately it gives a zero value for the count in Label1. I have also simplified the test so that it's all local on the VS2005 machine rather than on 2 websites.
Does anyone have a idea of what I may be missing?

Jason,
Is the Request.Form["Text1"] null because it refers to the current form testpost.aspx not the testpost.htm?

Thanks,

Jim
 
that's a good question. I'm not sure. I know there are different containers for different data: Form, ViewState, Session, Items, Cookies, QueryString, etc.

If you don't care where it comes from you could try Request.Params. I think this is all of items, cookies, querystrings and forms.



Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
here is a quick spike I put together. Note the file names in the code block. I set Default.htm as the startup page.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
    <head>
        <title></title>
    </head>
    <body>
        <form action="Default.aspx" method="post">
            <input id="first.name" name="first_name" type="hidden" value="Jason" />
            <input type="submit" value="Submit" />
        </form>
    </body>
</html>
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Web._Default" %>
<%@ Import Namespace="Web"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Repeater ID="TheRepeater" runat="server">
            <SeparatorTemplate><br /></SeparatorTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%#((Poco)Container.DataItem).Key%>' /> =
                <asp:Label ID="Label2" runat="server" Text='<%#((Poco)Container.DataItem).Value%>' />
            </ItemTemplate>
        </asp:Repeater>
    </form>
</body>
</html>
Code:
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.UI;

namespace Web
{
    public partial class _Default : Page
    {
        protected override void OnLoad(EventArgs e)
        {
            if (IsPostBack) return;
            TheRepeater.DataSource = GetPocos();
            DataBind();
        }

        private IEnumerable<Poco> GetPocos()
        {
            NameValueCollection data = Request.Form;
            foreach (string key in data.Keys)
            {
                Poco poco = new Poco();
                poco.Key = key;
                poco.Value = data[key];
                yield return poco;
            }
        }
    }
}
Code:
namespace Web
{
    public class Poco
    {
        private string key;
        public string Key
        {
            get { return key; }
            set { key = value; }
        }

        private string value;
        public string Value
        {
            get { return value; }
            set { this.value = value; }
        }
    }
}
which will produce the following output
first_name = Jason

This means the key for Request.Form is coming from the input elements name, not id.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Jason,
You're a lifesaver. Thanks so much for the pointers and finding that obscure error I made. I learned I have a lot to learn.
Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top