请教OnItemCreated和OnItemDataBound的问题?

zl13 2004-10-28 08:37:20
我在OnItemCreated里
Dim myMake
myMake = e.Item.Cells(2).Controls(0)
就没问题,但我在OnItemDataBound里也
Dim myMake
myMake = e.Item.Cells(2).Controls(0)
就告诉我超过边界?不明白,请高手解释一下
...全文
173 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zl13 2004-10-28
  • 打赏
  • 举报
回复
成功了,谢谢大家,今天我提了很多问题,同时也学会了很多问题,多谢大家了
活靶子哥哥 2004-10-28
  • 打赏
  • 举报
回复
我测试了一下
两个方法内 只要
if(e.Item.ItemIndex >= 0)判断后在操作就正常
看来当ItemIndex= -1 的时候 这两个方法也是执行的
你试试用
if(e.Item.ItemIndex >= 0)
判断一下,再进行你的操作



<%@ Page Language="C#" Debug="true" %>
<%@ Import NameSpace="System.Data"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="Content-Language" content="gb2312" />
<meta content="all" name="robots" />
<meta name="Author" content="huobazi@aspxboy.com ,活靶子" />
<meta name="Copyright" content="www.AspxBoy.Com,自由版权,任意转载." />
<meta name="Description" content="www.AspxBoy.Com" />
<meta name="Keywords" content="www.AspxBoy.Com,活靶子,huobazi" />
<title> New New Document</title>
<script language="c#" runat="server">

protected void Item_DataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
/*
if(e.Item.ItemIndex >= 0)
{
Response.Write( e.Item.Cells[0].Controls[0].GetType() + "<br />");
Response.Write( e.Item.Cells[0].Controls[1].GetType() + "<br />");
}*/
}

protected void Item_Created(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemIndex >= 0)
{
Response.Write( e.Item.Cells[0].Controls[0].GetType() + "<br />");
Response.Write( e.Item.Cells[0].Controls[1].GetType() + "<br />");
}
}

protected void BindGrid()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
for (int i = 0; i < 10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i+1);
dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
dg.DataSource = dv;
dg.DataBind();
}

protected void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}

</script>
</head>


<body>
<form id="aa" runat="server">

<asp:datagrid id="dg" runat="server"
OnItemDataBound="Item_DataBound"
OnItemCreated="Item_Created"
AutoGenerateColumns="false"
ShowHeader="false"
ShowFooter="false"
>
<columns>
<asp:TemplateColumn HeaderText="TemplateColumn">
<ItemTemplate>
<asp:TextBox runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "IntegerValue") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:datagrid>

</form>
</body>
</html>
seawavefj 2004-10-28
  • 打赏
  • 举报
回复
你应该先判断行是否是header,如是ITEM,则
Dim myMake as textbox

myMake = ctype(e.Item.Cells(2).Controls(0),textbox)
swzlxm 2004-10-28
  • 打赏
  • 举报
回复
还是 (...) aaa=(...)e.Item.FindControls("controlID");
if(aaa!=null)
{


.....
}
----------------
活靶子哥哥 2004-10-28
  • 打赏
  • 举报
回复
不是吧
应该在ItemCreated内
Control都还在创建所以Controls(0)索引超出index啊
xiaohutushen 2004-10-28
  • 打赏
  • 举报
回复
up

62,242

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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