[英]Dispay html textbox with value from code behind using c#
How can I display an html textbox with value obtained from code behinh?如何显示一个 html 文本框,其值是从后面的代码中获得的?
As long as your string value is not private, you should be able to do something like this只要您的字符串值不是私有的,您就应该能够做这样的事情
<input type="text" value="<%= YourStringValue %>" />
You could use an actual <asp:Textbox />
and set it's "text" value directly from the code behind.您可以使用实际的
<asp:Textbox />
并直接从后面的代码中设置它的“文本”值。 If you want to directly inject text into a "normal" html textbox (or anywhere else for that matter), you can use <%= SomeValue %>
.如果您想直接将文本注入“普通” html 文本框(或其他任何地方),您可以使用
<%= SomeValue %>
。 Yet another way is to include the "runat=server"
attribute on standard html elements, allowing you to manipulate them from the codebehind.另一种方法是在标准 html 元素上包含
"runat=server"
属性,允许您从代码隐藏中操作它们。
Normally I'd just go for the built-in ASP textbox control so I don't have to worry about persisting values/wiring up viewstate/etc.通常我只需 go 用于内置 ASP 文本框控件,因此我不必担心持久值/连接视图状态等。 Injecting dynamic content into plain html elements tends to be an edge case requirement...
将动态内容注入普通的 html 元素往往是边缘情况要求......
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//assign text value here
txt1.Value = "hello world!";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="txt1" runat="server" type="text" />
</div>
</form>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.