Setting value in html control in code without server control

Setting value in html control in code behind without server control

 <input type="text" name="txt" />
    <!--Please note I don't want put 'runat="server"' here to get the control in code behind-->
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

Code behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        //If I want to initlize some value in input, how can I set here
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    Request["txt"] // Here I am getting the value of input
}
+5
source share
5 answers

Elements that are not set to runat = "server" are considered plain text and as few Literal objects as possible are collected (one between each server management). I believe that if you really want to, you can try to find the correct Literal object (or perhaps LiteralControl) in Page.Controls and change it, but I would definitely recommend against it.

What is so terrible that it sets runat = "server"?

, , <% =% > . . Render, .

+11

, , .

, ASP.NET .

.

protected string InputValue { get; set; }

Page_Load .

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        this.InputValue = "something";
    }
}

, , :

<input type="text" name="txt" value="<%= this.InputValue %>" />

input, .

+26

:

<input class="field" id="locality" name="loc" value="<%= this.inputtypeCT %>"/> 

:

protected string inputtypeCT;

In the Page_Load event, set the property value:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
         this.inputtypeCT = "test"
    }
}
+2
source
<input type="text" name="txt" value="<%= System.Web.HttpUtility.HtmlEncode(this.InputValue) %>" />
+1
source

Add runat server

or use Asp controls as shown below

 <asp:TextBox type="text" runat="server" class="demoHeaders" id="datepicker" ClientIDMode="Static" Text=""/> 

Also, make sure you use ClientIDMode = "Static" to name the control in the client as a server.

Enjoy it!

0
source

All Articles