Get html control value in code behind without runat = server

access html control value in codebehind without runat server
In asp.net application we need to access value of html control in code behind without runat="server". For that purpose we can use name property of control. And Request.Form we can get value to code behind.So if we have html text input and you want to pass value of html input to code behind set it name property like <input name="username" />  and access it using Rquest.Form["usename"] you will get value to code behind of control without runat="server".

Code to get html control value to code behind without runat server

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
  <script type="text/javascript">
      function SubmitForm() {
         var theform;
          if (window.navigator.appName.toUpperCase().indexOf("NETSCAPE") > -1) {
              theform = document.forms["form1"];
          }
          else {
              theform = document.forms.form1;
          }
          theform.__EVENTTARGET = "btn";   
          theform.__EVENTARGUMENT = "";
          theform.submit();
      }    
</script>  
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="text" id="username" name="username" />
        <input type="button" id="btn" onclick="SubmitForm()"  value="GO"/>
    </div>
    </form>
</body>
</html>


Code behind to access html control value


using System;

namespace aspnetcontrol
{
    public partial class Getvaluewithoutrunatservercontrol : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string value = Request.Form["username"];
            Response.Write(value);
        }
    }
}

0 Comments :

Post a Comment