简体   繁体   English

使用 c# 显示 html 文本框,其中包含来自代码后面的值

[英]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.

相关问题 如何传递 HTML 文本框数据并从 C# 代码后面检索编辑的文本? - How to pass HTML textbox data and retrieve edited text from C# code behind? 文本框值更改未反映在 c# 后面的代码中 - textbox value change not being reflected in code behind c# 如何在JavaScript函数中使用/引用所选值(TextBox)到后面的C​​#代码中? - How can I use/reference the selected value (TextBox) from a JavaScript Function into C# Code behind? 将html文本框值从jquery模态传递到背后的asp代码 - Pass html textbox value from a jquery modal to asp code behind 文本框未显示在C#文件后面的代码中 - textbox not displaying in code behind c# file 如何使用C#背后的代码将HTML代码转换为图像 - How to Convert HTML Code into Images Using code behind C# 使用html控件的asp.net将值传递给C#背后的代码 - asp.net using html control to pass value to code behind c# C#无法在代码中获取HTML选择选项值 - c# cannot get HTML select option value in code behind 在从后面的C#代码为html输入分配值之后,更改为提交时未反映的文本 - After assigning value to html input from C# code behind, changes to text not reflected when submitted c#asp文本框仅当文本框为只读时,文本才会从代码后面更改 - c# asp Textbox Text does only change from Code behind when Textbox is readonly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM