Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting value in html control in code behind without making server control

Setting value in html control in code behind without making 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
}
like image 799
Muhammad Akhtar Avatar asked Feb 24 '10 11:02

Muhammad Akhtar


1 Answers

This answer comes from memory, so I apologize if it's slightly off.

What you can do is use an ASP.NET inline expression to set the value during the loading of the page.

First, add a property in the code-behind of your page.

protected string InputValue { get; set; }

In the Page_Load event, set the value of the property.

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

Finally, add an inline expression in your page markup like this:

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

This will let you set the value of the input element without making it a server-side tag.

like image 50
Adam Maras Avatar answered Oct 01 '22 03:10

Adam Maras