请教:为什么repeater中的ItemTemplate项内的单元格不能动态的创建表格HTML:<asp:repeater id="Repeater1" runat="server">
<ItemTemplate>
<TR>
<TD width="5%" style="HEIGHT: 19px"><input type="hidden" id="SelectedID" runat="server" value='<%# DataBinder.Eval(Container.DataItem, "ID")%>' NAME="SelectedID"/></TD>
<TD valign="top"><%# DataBinder.Eval(Container.DataItem, "Content")%></TD>
td runat="server" id="sss"></td>
</TR>
</ItemTemplate>
</asp:repeater>CS:public static string id;
static string Description;
protected System.Web.UI.WebControls.Repeater Repeater1;
static int Num;private void Page_Load(object sender, System.EventArgs e)
{if(!this.IsPostBack)
{
BindData();
}
Table t = new Table();
TableRow tr = new TableRow();
for (int i=1;i < Num;i++)
{
TableCell td = new TableCell();
HtmlInputRadioButton Radio = new HtmlInputRadioButton();
Radio.ID = "Radio" + i.ToString();
Radio.Name = "RadioGroup";
td.Controls.Add(Radio);
tr.Cells.Add(td);
t.Rows.Add(tr);
}
sss.Controls.Add(t);
}
出错提示:C:\Inetpub\wwwroot\dcbg\Survey.aspx.cs(55): 找不到类型或命名空间名称“sss”(是否缺少 using 指令或程序集引用?)
<ItemTemplate>
<TR>
<TD width="5%" style="HEIGHT: 19px"><input type="hidden" id="SelectedID" runat="server" value='<%# DataBinder.Eval(Container.DataItem, "ID")%>' NAME="SelectedID"/></TD>
<TD valign="top"><%# DataBinder.Eval(Container.DataItem, "Content")%></TD>
td runat="server" id="sss"></td>
</TR>
</ItemTemplate>
</asp:repeater>CS:public static string id;
static string Description;
protected System.Web.UI.WebControls.Repeater Repeater1;
static int Num;private void Page_Load(object sender, System.EventArgs e)
{if(!this.IsPostBack)
{
BindData();
}
Table t = new Table();
TableRow tr = new TableRow();
for (int i=1;i < Num;i++)
{
TableCell td = new TableCell();
HtmlInputRadioButton Radio = new HtmlInputRadioButton();
Radio.ID = "Radio" + i.ToString();
Radio.Name = "RadioGroup";
td.Controls.Add(Radio);
tr.Cells.Add(td);
t.Rows.Add(tr);
}
sss.Controls.Add(t);
}
出错提示:C:\Inetpub\wwwroot\dcbg\Survey.aspx.cs(55): 找不到类型或命名空间名称“sss”(是否缺少 using 指令或程序集引用?)
并没有出现在
public static string id;
static string Description;
protected System.Web.UI.WebControls.Repeater Repeater1;
这个地方......
加上protected System.Web.UI.HtmlControls.HtmlTableCell sss;
试试
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 55: t.Rows.Add(tr);
行 56: }
行 57: sss.Controls.Add(t);-------sss这里出错
行 58:
行 59: }
DataColumn dc=new DataColumn("name",Typeof(string))
t.Columns.Add(dc)
TableRow tr = t.NewRow()
Table t = new Table();
TableRow tr = new TableRow();
for (int i=1;i < Num;i++)
{
TableCell td = new TableCell();
HtmlInputRadioButton Radio = new HtmlInputRadioButton();
Radio.ID = "Radio" + i.ToString();
Radio.Name = "RadioGroup";
td.Controls.Add(Radio);
tr.Cells.Add(td);
t.Rows.Add(tr);
}如果不是在ItemTemplate项内的单元格内动态创建表而是在其它一单元格中则正常显示生成的radio。。
你需要遍历 repeater ,查找 id = sss 的控件。reperter.Items[i].FindControls("sss");
找到这个 sss 后,再
sss.Controls.Add(t);
所以会产生重复的<td runat="server" id="sss"></td>吧
我想应该不会吧。。repeater会动态给他们编号的如同sss_XXX
如何在repeater的ItemTemplate中动态的生成HtmlInputRadioButton
的实现方法。
<ItemTemplate>
<TR>
<TD width="5%" style="HEIGHT: 19px"><input type="hidden" id="SelectedID" runat="server" value='<%# DataBinder.Eval(Container.DataItem, "ID")%>' NAME="SelectedID"/></TD>
<TD valign="top"><%# DataBinder.Eval(Container.DataItem, "Content")%></TD>
<td runat="server" id="sss"></td>
</TR>
</ItemTemplate>
</asp:repeater>
好比如在 DataGrid 中查找 checkbox 一样。这是我在 DataGrid 中查找checkbox 的一段代码:System.Web.UI.WebControls.CheckBox cb; // 创建 CheckBox 对象
// 遍历查找复选框。以便获取 id 号来删除数据。
for( int i = 0 ;i < DataGrid1.Items.Count; i ++ )
{
cb = (System.Web.UI.WebControls.CheckBox)DataGrid1.Items[i].FindControl("CheckBox1");
if(cb != null) // 控件不为空。
{
if( cb.Checked )
{
// 删除选择的数据。
string id = DataGrid1.Items[i].Cells[0].Text; // 获取数据行的 id 值。
pkIdAL.Add(id);
}
}
}
我想请教的是:
如何在repeater的ItemTemplate中动态的生成HtmlInputRadioButton
的实现方法。CS:public static string id;
static string Description;
protected System.Web.UI.WebControls.Repeater Repeater1;
static int Num;private void Page_Load(object sender, System.EventArgs e)
{if(!this.IsPostBack)
{
BindData();
}
Table t = new Table();
TableRow tr = new TableRow();
for (int i=1;i < Num;i++)
{
TableCell td = new TableCell();
HtmlInputRadioButton Radio = new HtmlInputRadioButton();
Radio.ID = "Radio" + i.ToString();
Radio.Name = "RadioGroup";
td.Controls.Add(Radio);
tr.Cells.Add(td);
t.Rows.Add(tr);
}
sss.Controls.Add(t);
}在CS中编译好了后,再通过repeater输出。。我的想法有问题吗。。<asp:repeater id="Repeater1" runat="server">
<ItemTemplate>
<TR>
<TD width="5%" style="HEIGHT: 19px"><input type="hidden" id="SelectedID" runat="server" value='<%# DataBinder.Eval(Container.DataItem, "ID")%>' NAME="SelectedID"/></TD>
<TD valign="top"><%# DataBinder.Eval(Container.DataItem, "Content")%></TD>
<td runat="server" id="sss"></td> --------关健在这里---------
</TR>
</ItemTemplate>
</asp:repeater>但是提示:
C:\Inetpub\wwwroot\dcbg\Survey.aspx.cs(54): 找不到类型或命名空间名称“sss”(是否缺少 using 指令或程序集引用?)
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem){
TableCell cell=e.Item.FindControl("sss") as TableCell;
if(cell!=null){
//在这创建Table并添加到cell的Controls集合中
}
}
}
再 sss.Controls.Add(t);
<HTML>
<HEAD>
<title>BBS_View</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="./css/main.css" type="text/css" rel="stylesheet">
</HEAD>
<body bottomMargin="0" leftMargin="0" topMargin="0" rightMargin="0">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" cellSpacing="0" cellPadding="0" width="700" align="center" border="0">
<TR>
<TD width="100%" colSpan="6">
<DIV id="DIV1_Subject" style="DISPLAY: inline; WIDTH: 98%; HEIGHT: 15px" runat="server"
ms_positioning="FlowLayout"></DIV>
</TD>
<TR>
<TD width="100%" colSpan="6">
<TABLE id="Table2" cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
<TR>
<TD style="WIDTH: 35px"><FONT face="宋体">序号</FONT></TD>
<TD style="WIDTH: 566px"><FONT face="宋体">项目</FONT></TD>
<TD>
<TABLE id="Table3" cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
<TR>
<TD align="center" colSpan="4"><FONT face="宋体">满意度</FONT></TD>
</TR>
<TR>
<TD style="WIDTH: 38px" align="center"><FONT face="宋体">满意</FONT></TD>
<TD align="center"><FONT face="宋体">基本满意</FONT></TD>
<TD align="center"><FONT face="宋体">不满意</FONT></TD>
<TD align="center"><FONT face="宋体">不了解</FONT></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</TD>
</TR>
<asp:repeater id="Repeater1" runat="server">
<ItemTemplate>
<TR>
<TD width="5%" style="HEIGHT: 19px"><input type="hidden" id="SelectedID" runat="server" value='<%# DataBinder.Eval(Container.DataItem, "ID")%>' NAME="SelectedID"/></TD>
<TD valign="top"><%# DataBinder.Eval(Container.DataItem, "Content")%></TD>
<TD id="tdd" runat="server"></TD>
</TR>
</ItemTemplate>
</asp:repeater>
<TR>
<TD width="5%"></TD>
<TD></TD>
<TD width="5%"></TD>
<TD width="7%"></TD>
<TD width="5%"></TD>
<TD width="5%"></TD>
</TR>
<TR>
<TD colSpan="6">
<P><FONT face="宋体"><BR>
</FONT> </P>
</TD>
</TR>
<TR>
<TD colSpan="6"><asp:textbox id="TextBox_text" runat="server" Rows="4" Width="772px" TextMode="MultiLine" CssClass="input"></asp:textbox></TD>
</TR>
<TR>
<TD vAlign="middle" align="center" colSpan="6" height="20"><INPUT class="button" id="input_text" type="button" value="提交发表" name="input_text" runat="server">
<INPUT class="button" id="Reset1" type="reset" value="清空重填" name="Reset1"><BR>
<FONT face="宋体">
<BR>
<BR>
</FONT>
</TD>
</TR>
</TABLE>
</form>
</body>
</HTML>
<td><asp:panel id="sss" runat="server"/></td>
webdiyer(陕北吴旗娃) 和xrascal(横刀夺爱)两位兄台的想法应是一路的吧。
只是 webdiyer(陕北吴旗娃)兄的详细些。
但是我按照兄台的方法却不生成单选按钮
private void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
TableCell cell=e.Item.FindControl("tdd") as TableCell;
if(cell != null)
{
Table t = new Table();
TableRow tr = new TableRow();
for (int i=1;i<Num;i++)
{
TableCell td = new TableCell();
HtmlInputRadioButton Radio = new HtmlInputRadioButton();
Radio.ID = "Radio" + i.ToString();
Radio.Name = "RadioGroup";
td.Controls.Add(Radio);
tr.Cells.Add(td);
t.Rows.Add(tr);
}
tdd.Controls.Add(t);
//在这创建Table并添加到cell的Controls集合中
}
}
}
也就是public class Survey : System.Web.UI.Page
里不自动生成:protected System.Web.UI.HtmlControls.HtmlTableCell tdd;
手动增加了。。其它也是找不到。。
TableCell cell=e.Item.FindControl("tdd") as TableCell;
if(cell != null)也是不成立的
还请那位兄台帮看看:
<%@ Page language="c#" Codebehind="Survey.aspx.cs" AutoEventWireup="false" Inherits="LUOXP.BBS.Survey" %>
<HTML>
<HEAD>
<title>BBS_View</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="./css/main.css" type="text/css" rel="stylesheet">
</HEAD>
<body bottomMargin="0" leftMargin="0" topMargin="0" rightMargin="0">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" cellSpacing="0" cellPadding="0" width="700" align="center" border="0">
<TR>
<TD width="100%" colSpan="6">
<DIV id="DIV1_Subject" style="DISPLAY: inline; WIDTH: 98%; HEIGHT: 15px" runat="server"
ms_positioning="FlowLayout"></DIV>
</TD>
<TR>
<TD width="100%" colSpan="6">
<TABLE id="Table2" cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
<TR>
<TD style="WIDTH: 35px"><FONT face="宋体">序号</FONT></TD>
<TD style="WIDTH: 566px"><FONT face="宋体">项目</FONT></TD>
<TD>
<TABLE id="Table3" cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
<TR>
<TD align="center" colSpan="4"><FONT face="宋体">满意度</FONT></TD>
</TR>
<TR>
<TD style="WIDTH: 38px" align="center"><FONT face="宋体">满意</FONT></TD>
<TD align="center"><FONT face="宋体">基本满意</FONT></TD>
<TD align="center"><FONT face="宋体">不满意</FONT></TD>
<TD align="center"><FONT face="宋体">不了解</FONT></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</TD>
</TR>
<asp:repeater id="Repeater1" runat="server">
<ItemTemplate>
<TR>
<TD width="5%" style="HEIGHT: 19px"><input type="hidden" id="SelectedID" runat="server" value='<%# DataBinder.Eval(Container.DataItem, "ID")%>' NAME="SelectedID"/></TD>
<TD valign="top"><%# DataBinder.Eval(Container.DataItem, "Content")%></TD>
<td id=tdd runat=server></td>
</TR>
</ItemTemplate>
</asp:repeater>
<TR>
<TD width="5%"></TD>
<TD></TD>
<TD width="5%"></TD>
<TD width="7%"></TD>
<TD width="5%"></TD>
<TD width="5%"></TD>
</TR>
<TR>
<TD colSpan="6">
<P><FONT face="宋体"><BR>
</FONT> </P>
</TD>
</TR>
<TR>
<TD colSpan="6"><asp:textbox id="TextBox_text" runat="server" Rows="4" Width="772px" TextMode="MultiLine" CssClass="input"></asp:textbox></TD>
</TR>
<TR>
<TD vAlign="middle" align="center" colSpan="6" height="20"><INPUT class="button" id="input_text" type="button" value="提交发表" name="input_text" runat="server">
<INPUT class="button" id="Reset1" type="reset" value="清空重填" name="Reset1"><BR>
<FONT face="宋体">
<BR>
<BR>
</FONT>
</TD>
</TR>
</TABLE>
</form>
</body>
</HTML>
为什么我的<TD id="tdd" runat="server"></TD> 中的tdd就是找不到
也就是public class Survey : System.Web.UI.Page
里不自动生成:protected System.Web.UI.HtmlControls.HtmlTableCell tdd;
手动增加了。。其实也是找不到。。
所以
TableCell cell=e.Item.FindControl("tdd") as TableCell;
if(cell != null)也是不成立的
还请那位兄台帮看看:private void Repeater1_ItemCreated(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
TableCell cell=e.Item.FindControl("tdd") as TableCell;
if(cell != null)
{
Table t = new Table();
TableRow tr = new TableRow();
for (int i=1;i<Num;i++)
{
TableCell td = new TableCell();
HtmlInputRadioButton Radio = new HtmlInputRadioButton();
Radio.ID = "Radio" + i.ToString();
Radio.Name = "RadioGroup";
td.Controls.Add(Radio);
tr.Cells.Add(td);
t.Rows.Add(tr);
}
cell.Controls.Add(t);
//在这创建Table并添加到cell的Controls集合中
}
}
}
<%@ import namespace="System.Data"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>CSharpTemp</title>
<meta name=vs_defaultClientScript content="JavaScript">
<SCRIPT LANGUAGE="JavaScript">
<!--
//-->
</SCRIPT> <script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
if( !IsPostBack )
{
BindData(); System.Web.UI.HtmlControls.HtmlTableCell htc; for(int i=0;i < Repeater1.Items.Count;i ++)
{
htc = (System.Web.UI.HtmlControls.HtmlTableCell)Repeater1.Items[i].FindControl("tdd");
if(htc != null)
{
Table t = new Table();
TableRow tr = new TableRow();
for(int j=0;j < 3;j ++)
{
TableCell td = new TableCell();
HtmlInputRadioButton Radio = new HtmlInputRadioButton();
Radio.ID = "Radio" + j.ToString();
Radio.Name = "RadioGroup";
td.Controls.Add(Radio);
tr.Cells.Add(td);
t.Rows.Add(tr);
}
htc.Controls.Add(t);
}
}
}
} override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
} private void BindData()
{
DataTable dt = new DataTable();
DataColumn dc ;
DataRow dr; dc = new DataColumn();
dc.ColumnName = "ID";
dc.DataType = System.Type.GetType("System.Int32");
dt.Columns.Add(dc); dc = new DataColumn();
dc.ColumnName = "Content";
dc.DataType = System.Type.GetType("System.String");
dt.Columns.Add(dc); dr = dt.NewRow();
dr["ID"] = 1;
dr["Content"] = "内容一";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["ID"] = 2;
dr["Content"] = "内容二";
dt.Rows.Add(dr); Repeater1.DataSource = dt;
Repeater1.DataBind();
}
</script> </head>
<body>
<form id="Form1" method="post" runat="server">
<asp:repeater id="Repeater1" runat="server">
<ItemTemplate>
<table>
<TR>
<TD width="5%" style="HEIGHT: 19px"><input type="hidden" id="SelectedID" runat="server" value='<%# DataBinder.Eval(Container.DataItem, "ID")%>' NAME="SelectedID"/></TD>
<TD valign="top"><%# DataBinder.Eval(Container.DataItem, "Content")%></TD>
<TD id="tdd" runat="server"></TD>
</TR></table><br>
</ItemTemplate>
</asp:repeater>
</form>
</body>
</html>
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
TableCell cell=e.Item.FindControl("tdd") as TableCell;
if(cell != null)
{
Table t = new Table();
TableRow tr = new TableRow();
for (int i=1;i<Num;i++)
{
TableCell td = new TableCell();
HtmlInputRadioButton Radio = new HtmlInputRadioButton();
Radio.ID = "Radio" + i.ToString();
Radio.Name = "RadioGroup";
td.Controls.Add(Radio);
tr.Cells.Add(td);
t.Rows.Add(tr);
}
cell.Controls.Add(t);
//在这创建Table并添加到cell的Controls集合中
}
}
}