I'm trying to submit the below form where a master page is applied.
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<p>
What is your name?
<asp:TextBox ID="YourName" runat="server" ClientIDMode="Static"></asp:TextBox>
</p>
<p>
What is your age?
<asp:TextBox ID="YourAge" runat="server" Columns="3" ClientIDMode="Static"></asp:TextBox>
</p>
<p>
What is your favorite color?<br />
<asp:RadioButtonList ID="FavoriteColor" runat="server" ClientIDMode="Static">
<asp:ListItem Selected="True">Red</asp:ListItem>
<asp:ListItem>Green</asp:ListItem>
<asp:ListItem>Blue</asp:ListItem>
</asp:RadioButtonList>
</p>
<p>
<asp:CheckBox ID="EatGreenEggsAndHam" runat="server" Checked="True" ClientIDMode="Static"
Text="Will You Eat Green Eggs and Ham?" TextAlign="Left" />
</p>
<p>
<asp:Button ID="SubmitButton" runat="server" Text="Submit Form"
PostBackUrl="~/About.aspx"
onclick="SubmitButton_Click" />
</p>
</asp:Content>
I'm trying to use POST method to get submitted data from the form.
in About.aspx page i'm using the below code
Response.Write(Request.Form["YourName"].ToString());
expected behavior is to print the name typed in textbox with the id "YourAge" but the submitted form element ids have changed like below
ctl00$MainContent$YourName
why does this happen? i need to get the value of the control with the id "YourName"
I want to remove this "ctl00$MainContent$" part when submitting form. retrieving the form values are out of my scope and it's done by some other people. They only require form values to be submitted as "YourName" not "ctl00$MainContent$YourName"
Try to utilise the abstraction that ASP.net provides. You can use YourName.Text to retrieve the value of the YourName TextBox.
Response.Write(YourName.Text);
Also, ctl00$MainContent$YourName is not the ID of the element generated, but its name. The ID will probably be something like BodyContent_YourName.
In the first place, it is not advisable to directly read from the Request.Form. Most of the time You should be using the YourName.Text to read the value.
But, if you would still like to read the value from Request.Form you need to understand how the ASP.NET pipe line and http protocol works. When you submit a page from browser, browser would create a HTTP Request with key value pairs, where the key denotes the "name" of the input control while the value denoted the corresponding value.
In your code you are only setting the ID to be static but not the name. So, to set the name same as ID, you can use the techniques described for this SO Question
Udpate
Another approach I could think of is as below
Response.Write(Request.Form[YourName.UniqueID].ToString());
The Unique ID is the name which is rendered to the element and you can use the same for fetching the values from the Request.Form
Related
in an old ASP.NET Web Forms application I got this warning in the aspx code of a page:
Attribute 'fullname' is not a valid attribute for element 'input'
Why was written in this way?
And there is a way to resolve it?
You only get a warning. The simple issue is that the developer just made up a attribute, and shoved in some value. this is legal, and I often do this for say a standard text box.
so, I might for example place 5 or 10 text boxes and controls inside of a div, say like this:
<div id="EditRecord" runat="server" style="float:left;display: normal;border:solid 2px;padding:5px">
<div style="float:left" class="iForm">
<label>HotelName</label>
<asp:TextBox ID="txtHotel" runat="server" f="HOtelName" width="280" /> <br />
<label>First Name</label>
<asp:TextBox ID="tFN" runat="server" f="FirstName" Width="140" /> <br />
<label>Last Name</label>
<asp:TextBox ID="tLN" runat="server" f="LastName" Width="140" /> <br />
<label>City</label>
<asp:TextBox ID="tCity" runat="server" f="City" Width="140" /> <br />
<label>Province</label><asp:TextBox ID="tProvince" runat="server" f="Province" Width="75"></asp:TextBox> <br />
</div>
etc. etc. etc.
Note in above, I wanted to define what data base column for a routine to "fill out" the above controls.
So, I have a routine I call with a data row.
Call fLoader(EditRecord, rstData.Rows(0))
the above routine looks for any control with a "f=datacolum", and fills out the controls for me.
So, I get this:
In other words, my custom "f" attribute allows me to pull data from a database, and fill out the web page - and without having to write new code each time. (and I have a reverse routine - writes out all controls with f="some data column" back to the database. With this simple concept, then I am able to have any web page read/write data to/from the database - and not have to write that same code over and over. In effect, I get a whole crud system, and all achieved by a simple concept of adding a "made up" attribute "f" that defines the data base column to use.
So, you can in VS have that warning suppressed, but I as noted OFTEN make and add some custom values. Even for a button in a grid view, you might have this:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Button ID="cmdView" runat="server" Text="Button"
OnClick="cmdView_Click"
ProductID = '<%# Eval("ProductID") %>'
CustomerID = '<%# Eval("CustomerID") %>'
/>
And then in code behind, you have this:
Protected Sub cmdView_Click(sender As Object, e As EventArgs)
Dim btn As Button = sender
Debug.Print(btn.Attributes("CustomerID").ToString())
Debug.Print(btn.Attributes("ProductID").ToString())
So, we often just make up some attribute, and it is legal to do so in most cases.
So, that button - say dropped into a grid view now can pass values to the button click - even values not displayed in the gridview.
So, you can just as a general rule ignore the warning. It is not super common, but a lot of developers often will add and make up their own attributes, and client side code (JavaScript) or even code behind as above shows is then free to use the values in those custom, or "made up" attributes. In most cases, you get a warning, but other then that, it not a huge deal nor issue. You of course do have to be carful, since such custom attributes in most cases don't have automatic view state, and their values will not persist correctly for a round trip (post-back, and page return). However, for expressions or values such as above example, then its not a problem.
I'm making a custom module for mojoPortal CMS which needs to allow the client to add an affiliate into the database. As far as I can tell, this requires creating a .ascx file and then installing that using the administration toolbar in the Web interface to get it to a point where I can put it into a page, as http://www.mojoportal.com/hello-world-developer-quick-start.aspx.
The form is simple enough, but the values in the text boxes just stay empty when I submit, though the file upload works fine. The code:
<asp:Label ID="Label1" runat="server" Text="Company Name" AssociatedControlID="CompanyName">
</asp:Label>
<asp:TextBox ID="CompanyName" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Company Description" AssociatedControlID="CompanyDescription"></asp:Label>
<asp:TextBox ID="CompanyDescription" TextMode="MultiLine" runat="server"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="Company Logo" AssociatedControlID="CompanyLogo"></asp:Label>
<asp:FileUpload ID="CompanyLogo" runat="server" />
<asp:Button ID="SubmitButton" runat="server" Text="Add Affiliate" />
EnableViewState for the page and the controls is enabled
The text box is not set to ReadOnly, and there is no funky JavaScript dynamically modifying elements (at least, I didn't set any).
I can work around this by using HTML elements, and get the values using Request.Form. The information is actually there, I can see it in the Request.Form, but I would have to get that by something like Request.Form[CompanyName.ClientId.Replace("_","$")] or Request.Form[6] which both seem very messy and IIRC aren't really the way things are supposed to roll in .NET. Besides, having worked until 3 last night, I really want to know what the answer is now!
Any thoughts anyone?
What I had done was not created a click event for the button, relying on the fact that it was posting to the server (like I would in PHP). Oops! When I added a click event, then the text boxes retained their value when I was within that method.
I have a page that looks like this:
Head and body tags...
<form runat="server">
<ul>
<asp:RequiredFieldValidator runat="server" id="reqUser" controltovalidate="Username" errormessage="<li>There is no username!</li>" />
More validators...
</ul>
<h3><span style="color: Red;">*</span>Username</h3>
<asp:TextBox runat="server" id="Username" /></br>
More textboxes...
</form>
The thing is, I discovered that based on the id from the <asp:Textbox />, the names are created, but with a strange format (id=" MainContent_originalID" and name="ctl00$MainContent$originalID")
For me it is ok to have it submit the form to itself. The code works to handle that. The problem is, I don't know if when I use Request.Form["Name"] I have to use the original ID or the suplied name after processing. What to I do?
I believe in the code you do have to use the supplied name after processing. But, I think there may be a way to get that name based on the original name.
Something like:
originalName.ClientID
See: http://www.codeproject.com/Tips/95796/How-to-get-the-id-name-of-the-ASP-NET-server-contr
P.S. You have to use the original ID name with the statement (controllName.UniqueID)
I have a master page that contains a search box which is validated as a required field before the user can submit the search field.
The problem I'm having now is that one of my content pages has a DetailsView which won't let me edit a record, because the search box coming from the master page is blank.
The structure of the code is like this:
Master Page:
<form runat="server">
<asp:RequiredFieldValidator ID="SearchBoxRequiredFieldValidator"
runat="server" ControlToValidate="searchTextBox"
Display="None" ErrorMessage="Enter an employee's last name"></asp:RequiredFieldValidator>
<asp:TextBox ID="searchTextBox" autocomplete ="off" runat="server" Width="180px"></asp:TextBox>
<asp:Button ID="SearchButton" runat="server" Text="Employee Search"/>
<!--.....-->
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</form>
The "MainContent" placeholder is populated with a page with only a DetailsView. How should I change my code so that I can submit forms from my MainContent pages, but also allow the Master page's search feature to function properly?
I'm pretty new to asp.net forms, so any help is greatly appreciated!
Take a look at ValidationGroups. You can separate each logical form into different validation groups giving the effect of multiple discrete forms.
I have a database with a row that has "TEXTTEXT" inside a "noteText" column and null in that column for a different row. If I run a query that generates 1 rows, then run a different query that has 2 rows, Eval("noteText") returns different information when placed in a TextBox vs. placed in a Label. The TextBox and Label were created specifically for this test; it is not referenced in the codebehind. I think the TextBox for row 1 of the second query is somewhow pulling the data used in row1 of the first query.
I don't think it could be a browser-related issue since the source code of the returned page is itself incorrect.
Aspx file code:
<asp:TemplateField HeaderText ="Notes" SortExpression="noteid">
<ItemTemplate>
STARTXX
<asp:TextBox ID="AtxtNote" ViewStateMode="Disabled" runat="server"
TextMode= "SingleLine" Width="260" Text='<%# Eval("noteText") %>' />
<br />
<asp:Label ID="BtxtNote" runat="server" Width="260"
Text='<%# Eval("noteText") %>' />
<br />
ENDXX
</ItemTemplate>
</asp:TemplateField>
Source code on page:
STARTXX
<input name="ctl00$CPH1$gv$ctl02$AtxtNote" type="text"
value="TEXTTEXT" id="ctl00_ContentPlaceHolder1_gv_ctl02_AtxtNote"
style="width:260px;" />
<br />
<span id="ctl00_CPH1_gv_ctl02_BtxtNote"
style="display:inline-block;width:260px;"></span><br />
ENDXX
Actual spacing is a bit different; I edited the text of the source and output code to prevent horizontal scrolling.
I expected both Eval calls to show an empty string.
We fixed it by adding more calls to DataBind.
The current theory is that the TextBox was being submitted when clicking the button to query the database. After the DataBind happened, Asp.Net repopulated the TextBox with the data that was submitted. The TextBox now logically corresponded to a different row in the database, but was in the same position as the TextBox from the previous page view and thus had the same ClientID.
One reason for this suspicion is that the posts were triggering OnTextChanged events.
I'm still unhappy with this; I don't have full confidence that I know what's going on, even though it is now working.