怎样将数据绑定到DataGrid中的一ID为ItemTitle的Label,但是执行后报索引超出范围的错误。

1studio 2005-04-05 04:06:10
1、我做了一个留言板,表单里面有Email,Homepage,QQ,MSN这些输入框,留言的人当然不一定会填写QQ、MSN这些信息,所以在留言板的列表中会有些数据的QQ、MSN数段没有数据,这是很正常的。所以我想在留言板列表页中的每条记录显示时,只显示填写了的部分数据,例如:

2、--------------------------------------------------
填写了OICQ的记录就显示成:
Email Homepage Oicq Msn
没填写OICQ的则不显示出来这个title
Email Homepage Msn
如以下网址每条记录右上侧显示的那样:
http://www.1studio.net/gbook/01/index.asp?MEMBER_ID=1

3、--------------------------------------------------
程序的Datagrid结构如下,因为有table,我只能做成模版列插入html的table,里面有个
<asp:label Runat="server" ID="ItemTitle" asp:Label>
用来显示此记录显示的 Oicq Msn这个title,

<asp:datagrid id="DataGrid1"....>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<table width="470" border="0" cellspacing="0" cellpadding="0">
<tr align="right">
<td height="18">
<asp:label Runat="server" ID="ItemTitle"></asp:Label>
</td>
</tr>
</table>
......
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle Mode="NumericPages"></PagerStyle>
</asp:datagrid>

4、--------------------------------------------------
.cs文件中是这样的:

SqlDataAdapter DataAdapter1 = new SqlDataAdapter(SqlCmd, myConnection);
DataSet ds = new DataSet();
DataAdapter1.Fill(ds,"GBookList");
try
{
DataTable GBookList = ds.Tables["GBookList"];
for(int i=0;i<GBookList.Rows.Count;i++)
{
string strTmpTitle = "";
string strEmail = GBookList.Rows[i]["GM_EMAIL"].ToString().Trim();
string strHomePage = GBookList.Rows[i]["GM_HOMEPAGE"].ToString().Trim();
string strOICQ = GBookList.Rows[i]["GM_OICQ"].ToString().Trim();
string strMsn = GBookList.Rows[i]["GM_MSN"].ToString().Trim();
if (strEmail!="")
strTmpTitle = "<a href='mailto:"+ strEmail +"'>Email</a> ";
if (strHomePage!="")
strTmpTitle += "<a href='"+ strHomePage +"' target='_blank'>Homepage</a> ";
if (strOICQ!="")
strTmpTitle += "<a title='OICQ:"+ strOICQ +"' target='_blank'>Oicq</a> ";
if (strMsn!="")
strTmpTitle += "<a href='mailto:"+ strMsn +"'>MSN</a> ";
// 问题出在下面这句
((Label)DataGrid1.Items[i].Cells[0].FindControl("ItemTitle")).Text = strTmpTitle;
// 下面这句输入是正确的,显示此记录的Email Homepage Oicq Msn 的代码
// Response.Write(strTmpTitle + "<br>");
}
DataGrid1.DataSource = GBookList;
DataGrid1.CurrentPageIndex = intPageNumber;
DataGrid1.DataBind();
}

5、-------------------------------------------------------------
在4中//掉的那两行中:
((Label)DataGrid1.Items[i].Cells[0].FindControl("ItemTitle")).Text = strTmpTitle;
这句就是为了把strTmpTitle字符串绑定到2、中的名为 ItemTitle 的 label 中去

执行后报错:
索引超出范围。必须为非负值并小于集合大小。参数名: index

我不晓得怎么解决。

6、-------------------------------------------------------------
我将4中的((Label)DataGrid1.Items[i].Cells[0].FindControl("ItemTitle")).Text = strTmpTitle;
这句注释掉,在.cs程序中加入:

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType itemType = e.Item.ItemType;
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
((Label)e.Item.Cells[0].FindControl("ItemTitle")).Text = strTmpTitle;
}
}

7、-------------------------------------------------------------
因为6、中不能运行,那么我在.cs文件中加入了:
public string strTmpTitle;
再将4中的string strTmpTitle = ""; 这句改成 strTmpTitle = "";
将3中的
<asp:label Runat="server" ID="ItemTitle" ></asp:Label>
改成:
<asp:label Runat="server" ID="ItemTitle" Text="<%# strTmpTitle%>"></asp:Label>
再执行时,则:
记录的Email Homepage Oicq Msn  所显示出来的数据都是最后一条记录的值,晕晕。不能一条绑定一条了。请问怎么解决。



则接收不到strTmpTitle这个值。请问怎么办!!!
我想应该就是一两句语句的问题。请解答,谢谢!
...全文
48 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧