Getting form field names on Submit when form is us

2019-08-29 06:16发布

I am slowly, piece by piece learning what I am doing with ASP.NET

I've created the beginnings of my new web application, but I am often coming up against the issue that ASP.NET renames elements IDs or in the case of form fields, their names.

I have a form which is basically a sales system. It is essentially made up of two User Controls, one is a form for Customer Details (name, address etc) and the second is a form for the customer's purchases, it consists of a number lines dynamically created by Javascript created as you list the items the customer is purchasing.

Both these sections are User Controls because they are to be used for other areas of the system where this data will need to be recalled/re-entered.

When the USer Control is loaded, the field which contains the Customers' Name is renamed "m$mainContent$customerForm$name" I understand where this comes from, "m" is the ID of my Master Page, "mainContent" is the main Content Placeholder and "customerForm" is the name of the User Control.

In fact, in my case, this will always remain the same on all forms, so it is relative easy to overcome... but... suppose it wasn't

I can see there are ways I could deal with this with Javascript, but the form doesn't need an AJAX submit, a normal Post will do fine for this, so when I open up the recieving page I want to call Request.Form("name")% to save the customer's name into the database, but of course I really need Request.Form("m$mainContent$customerForm$name")%

How would I dynamically extract those prefixes from the posting form to ensure that if I rename anything or use it in a different scenario, the problem will not break it?

I am using .NET 2.0, so can't use Static Client.

3条回答
虎瘦雄心在
2楼-- · 2019-08-29 06:39

suppose you have a form inside user control that has a TextBox field with ID="txtfname".You have registered your user control in page1 and you want to POST form data to page2 :

in page1 you have user control:

<My:WebUserControl1 ID="myuc" runat="server" />

in page2 codebehind , you can get the value of user control form field (txtfname) this way :

string firstName = Request.Form["myuc$txtfname"];
查看更多
狗以群分
3楼-- · 2019-08-29 07:01

This is really old but let's see if it's useful for you.

Getting this example I managed to get the value from a given field name without prefix.

I'm sure it is not the best solution, but it was useful for me. Let's say you want to recover the value of a field named "hfIdAviso", that comes in the POST as "ctl00$body$hfIdAviso":

var value = Request.Form.Cast<string>()
                    .Where(key => key.EndsWith("hfIdAviso"))
                    .ToDictionary(key => key, key => Request.Form[key])
                    .Values.FirstOrDefault();

That will return the value for that field.

Hope it helps.

查看更多
不美不萌又怎样
4楼-- · 2019-08-29 07:04

If you include runat="server" in the declaration of your user control in the ASPX page, then you can access this control by just using the ID value of the control.

For example In the ASPX:

<uc:MyUserControl ID="mycontrol1" runat="server" />

In the code behind (in C# syntax):

object foobar = mycontrol1.SelectedValue;
查看更多
登录 后发表回答