CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

How to Get and Set Value of HTML control from CodeBehind in ASP.Net?
Submitted By Satheesh Babu B
On 3/8/2010 6:41:42 AM
Tags: ASP.Net,C#,CodeDigest,HTML  

How to Get and Set Value of HTML control from CodeBehind in ASP.Net?


Sometimes, there may be scenarios where we need to access a HTML control from codebehind file. An easy way to do that is by replacing it with a server control or including runat="server" attribute. By doing like this, it is possible to set and get values of those controls from codebehind file. Refer below,


ASPX
<input id="txtDesc" runat="server" name="txtDesc" type="text" />


CodeBehind
 txtDesc.Value = "New Value!!";

 

If you still want to get or set values to HTML controls without runat="server" then you can use Request.Form collection to get the value. You can use public property and embedded code blocks to set the value from server. Refer the code below,

 

ASPX
<input id="txtDesc1" name="txtDesc1" type="text" value="Set in Client Side" />

<input id="txtDesc2" name="txtDesc2" type="text" value="<% =ServerValue %>" />

 

CodeBehind

public string ServerValue = String.Empty;

protected void btnSave_Click(object sender, EventArgs e)

    {

        string ClientValue = Request.Form["txtDesc1"];

        ServerValue = "Set in Server";

    }

}

 

In the above code, txtDesc1 value that is set from client side is accessed in server using Request.Form collection and the field txtDesc2 value is set from the server using a public property and embedded code expressions.


Happy Coding!!!

 

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..