从asp.net中的代码将文本框添加到html表中 [英] Adding text box into html table from code behind in asp.net

查看:155
本文介绍了从asp.net中的代码将文本框添加到html表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在页面加载事件上创建一个html表,并从Sql数据读取器填充该表的td.在TD中,我想放置一个文本框,并允许用户在其中填充值.怎么做.
我尝试使用文本框tb = new textbox();
并尝试将其添加到一个

''"+ tb +"''


它显示为System.Web.Ui ....
pls很快就会回复

i am trying to create a html table on page load event and filling the td s of that table from Sql data reader . in td i want to place a textbox and allow user to fill value in that. how to do that.
i tried using textbox tb=new textbox();
and am trying to add it in one

''"+tb+"''


it is appearing as System.Web.Ui....
pls reply soon

推荐答案

文本框的实例不能像字符串一样添加...

您必须将其添加到controlsCollection.

不幸的是,HTML表不提供任何服务器访问权限

您必须使用ASP服务器控件来构建整个表:

the instance of textbox can''t be added like a string...

you have to add it to a controlsCollection.

Unfortunatly a html table doesn''t provide any server access

you have to build the whole table with asp server controls:

var tbl = new Table();
var tr1 = new TableRow();
var tc1 = new TableCell();
var tb = new TextBox();

tc1.Controls.Add(tb); // add textbox to tablecell
tr1.Controls.Add(tc1); // add tablecel to tablerow
tbl.Controls.Add(tr1); // add tablerow to table


this.Controls.Add(tbl); // add table to webform (or any other container like panel)


您可以尝试使用模板化控件(例如Repeater或DataList)并使用数据绑定,而不是尝试从后面的代码中添加控件. IMO这是一个更强大的解决方案.

Rather than trying to add controls from the code behind you could use a templated control, such as Repeater or DataList, and use databinding. IMO this is a more robust solution.

<asp:Repeater runat=''server''>
   <HeaderTemplate>
      <table>
   </HeaderTemplate>
   <ItemTemplate>
      <tr>
        <td>
          <asp:Textbox runat=server Text=''eval(''myField'') />
        </td>
      </tr>
   </ItemTemplate>
   <FooterTemplate>
      </table>
   </FooterTemplate>
</asp:Repeater>




您可以通过类似
的字符串添加文本框
Hi,

you can add textbox through string like

StringBuilder sb = new StringBuilder();
         StringWriter tw = new StringWriter(sb);
         HtmlTextWriter hw = new HtmlTextWriter(tw);
         TextBox txtuname = new TextBox();
         txtuname.ID = "txtuname";
         txtuname.RenderControl(hw);
         String tb=<table width=""><tr><td>"+sb.toString()+"</td></tr></table>";



您可以在任何需要的地方使用该字符串

最好的



you can use that string wherever you want

All the Best


这篇关于从asp.net中的代码将文本框添加到html表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆