简体   繁体   English

如何从C#中的数据库获取html输入占位符值?

[英]How to get html input placeholder value from database in C#?

I have a HTML input text and place holder. 我有一个HTML输入文本和占位符。 I want to get a place holder value from a database but I want to do it programatically: 我想从数据库中获取占位符的值,但是我想通过编程来实现:

<td><input runat="server" placeholder="Title" type="text" /></td>

Any help would be appreciated. 任何帮助,将不胜感激。

First, you need to give the input a name so that you can reference it in code behind. 首先,您需要给输入一个名称,以便您可以在后面的代码中引用它。 Then, once you go grab the value that you want from the database you will do something like this: 然后,一旦您从数据库中获取所需的值,您将执行以下操作:

inputName.Placeholder = DataRow[0]("TextFromDBColumnName").ToString();

Obviously you want to put all safeguards in place to make sure that the DataTable or DataReader you are using has data and that the value that came back from the database is not NULL, which I would safeguard against in the query or stored procedure. 显然,您想采取所有保护措施,以确保您使用的DataTable或DataReader具有数据,并且从数据库返回的值不是NULL,我将在查询或存储过程中防止这种情况。

As ammills01 said you've got to give the input an ID attribute so you can call it from code-behind: 正如ammills01所说,您必须为输入提供一个ID属性,以便可以从背后的代码中调用它:

<td><input ID="YourID" runat="server" placeholder="Title" type="text" /></td>

And then call it: 然后调用它:

YourID.Placeholder = DataRow[0]("TextFromDBColumnName").ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM