在代码隐藏的html控件中设置值而不进行服务器控制
在代码隐藏中设置 html 控件中的值,而不使服务器控件
<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" />
代码隐藏:
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
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这个答案来自记忆,所以如果略有偏差,我深表歉意。
您可以做的是使用 ASP.NET 内联表达式在页面加载期间设置值。
首先,在页面的代码隐藏中添加一个属性。
在
Page_Load
事件中,设置该属性的值。最后,在页面标记中添加一个内联表达式,如下所示:
这将允许您设置
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.
In the
Page_Load
event, set the value of the property.Finally, add an inline expression in your page markup like this:
This will let you set the value of the
input
element without making it a server-side tag.未设置 runat="server" 的元素被视为纯文本,并收集到尽可能少的 Literal 对象中(每个服务器端控件之间一个)。我想如果你真的想要的话,你可以尝试在 Page.Controls 中找到正确的 Literal (或者可能是 LiteralControl)对象,并修改它,但我绝对建议不要这样做。
将其设置为 runat="server" 有什么可怕的?
是的,当然你也可以使用<%= %>。 嵌入式代码块。它们是在渲染时评估的,因此这样做应该相对安全。
Elements that are not set runat="server" are considered plain text and collected into as few Literal objects as possible (one between each serverside control). I suppose if you really really wanted to, you could try to find the correct Literal (or maybe LiteralControl) object in Page.Controls, and modify it, but I'd definately recommend against it.
What's so terrible about setting it runat="server" ?
And yes, of course you can also use <%= %>. Embedded code blocks. They're evaluated at Render time, so it should be relatively safe to do so.
在页面中添加如下表达式:
在页面的代码隐藏中添加属性:
在 Page_Load 事件中,设置属性的值:
Add an expression in your page like this:
Add a property in the code-behind of your page:
In the Page_Load event, set the value of the property:
添加 runat 服务器
或使用如下所示的 Asp 控件
另外,请确保您使用 ClientIDMode="Static" 来为 clinet 中的控件命名,就像服务器一样。
享受!
Add runat server
or use Asp controls like below
Also make sure that you used ClientIDMode="Static" for the naming of control to be in clinet as like server.
Enjoy!