How to access HTML form input from ASP.NET code behind
If you are accessing a plain HTML form, it has to be submitted to the server via a submit button (or via JavaScript post). This usually means that your form definition will look like this (I'm going off of memory - make sure you check the HTML elements are correct):
<form method="POST" action="page.aspx">
<input id="customerName" name="customerName" type="Text" />
<input id="customerPhone" name="customerPhone" type="Text" />
<input value="Save" type="Submit" />
</form>
You should be able to access the customerName and customerPhone data like this:
string n = String.Format("{0}", Request.Form["customerName"]);
If you have method="GET"
in the form (not recommended - it messes up your URL space), you will have to access the form data like this:
string n = String.Format("{0}", Request.QueryString["customerName"]);
This of course will only work if the form was 'Posted', 'Submitted', or done via a 'Postback' (i.e., somebody clicked the 'Save' button, or this was done programmatically via JavaScript).
Also, keep in mind that accessing these elements in this manner can only be done when you are not using server controls (i.e., runat="server"
). With server controls, the id and name are different.
How to access an input type=text from code behind in ASP NET
you can change textarea to asp:TextBox
<asp:TextBox id="txtDescrizione" TextMode="multiline" Columns="50" Rows="5" runat="server" />
html form to C# code-behind code
Here's an example of some code you could put in your button OnClick event. This POST's data to another URL.
Hopefully you can figure out what's going on in this code. Basically it is building a string (data) with all of the data from the HTML form and submitting this to the other website using HTTP POST.
Most likely you would also need to check the response back from the other website.
string remoteUrl = "https://visitor2.constantcontact.com/api/signup";
ASCIIEncoding encoding = new ASCIIEncoding();
string data = "ca=my-secrect-key&list=3&source=EFD&required=list,email&url=&email=" + Server.UrlEncode(TextBox1.Text) + "&first_name=" + Server.UrlEncode(TextBox2.Text) + "&last_name=" + Server.UrlEncode(TextBox3.Text);
byte[] bytes = encoding.GetBytes(data);
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(remoteUrl);
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = bytes.Length;
using (Stream stream = httpRequest.GetRequestStream())
{
stream.Write(bytes, 0, bytes.Length);
stream.Close();
}
How to access an HTML tag in ASP.NET code behind without postback
That doesn't cause a postback, but you can't access elements from serverside if you are not on the server. If you just want to hide them client-side you can use javascript/jQuery and/or CSS. However, elements which are Visible=false
(serverside) don't exist on client-side at all.
So the runat=server
doesn't cause more postbacks than without it. It just says ASP.NET that it has to create a control in server-memory, so that you are able to access it there.
But as mentioned, if you want to change the visibility on client-side without a postback you can't use Visible=false
. Then use CSS either with a class or inline:
abc.Attributes.Add("style", "display:none");
or without codebehind:
<div id="abc" style="display:none"> Test </div>
Now you could access it from client-side even if it's invisible, for example to switch visibility.
How to access html control value in code behind without using runat server?
By using Request.Form["id"]` on Button_Click, you will get the value of html control
string id = Request.Form["id"]
Asp.net get contents of HTML input box
Make sure that your input is within a form element, as shown on this page:
How to access html form input from asp.net code behind
How to access html controls in code behind
Add id
and runat
server attributes to the input tag (see below)
<input type="text" value="Username" class="input-text autoclear" id="Username" runat="server"/>
<input type="password" value="Password" class="input-text autoclear" id="Password" runat="server"/>
You also need to change Text
to Value
in your code:
protected void LoginButton_Click(object sender, EventArgs e)
{
// Validate the user against the Membership framework user store
if (Membership.ValidateUser(Username.Value, Password.Value))
{
// Log the user into the site
FormsAuthentication.RedirectFromLoginPage(UserName.Value, RememberMe.Checked);
}
// If we reach here, the user's credentials were invalid
InvalidCredentialsMessage.Visible = true;
}
You can also add a html checkbox
for RememberMe
<input id="RememberMe" type="checkbox" runat="server" value ="RememberMe"/>
Now you can check the checked states by calling RememberMe.Checked
Related Topics
Kill Some Processes by .Exe File Name
Why Does "Abcd".Startswith("") Return True
How to Parse Hex Values into a Uint
"A Lambda Expression with a Statement Body Cannot Be Converted to an Expression Tree"
Bring a Window to the Front in Wpf
How to Change the Color of Progressbar in C# .Net 3.5
Howto Implement Callback Interface from Unmanaged Dll to .Net App
What Is the Equivalent of the Java Bigdecimal Class in C#
How to Get All Classes Within a Namespace
Xdocument Containing Namespaces
Algorithm to Compare Two Images in C#
Frombody String Parameter Is Giving Null
Token Based Authentication in Web API Without Any User Interface