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
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With