我有一个带有简单嵌套转发器的项目。当我单击该按钮时,不会调用OnItemCommand事件,但会调用OnItemCreated(但是,它不会调用Page_Load事件处理程序)。我在猜什么?
MARKUP
<table width="100%">
<tr>
<td>my row</td>
<td>my description</td>
</tr>
<asp:Repeater ID="rptMain" runat="server" OnItemCreated="rptMain_ItemCreated" OnItemCommand="rptMain_ItemCommand">
<ItemTemplate>
<tr style="background-color:#45abdc;">
<td><asp:LinkButton ID="ibtnDoSomething" runat="server" CommandArgument="SELECT" Text="Clicky" /></td>
<td><asp:Label ID="lblMainData" runat="server"></asp:Label></td>
</tr>
<asp:Repeater ID="rptChild" runat="server" OnItemCreated="rptChild_ItemCreated">
<ItemTemplate>
<tr style="background-color:#bcbbcb;">
<td>
</td>
<td>
<asp:Label ID="lblChildData" runat="server"></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</table>
代码隐藏 DataTable _childdata = new DataTable(); DataTable _data = new DataTable();
public void Page_Load()
{
_data.Columns.Add(new DataColumn("Text"));
_data.Columns.Add(new DataColumn("CommandValue"));
DataRow _row1 = _data.NewRow();
_row1["Text"] = "Butch";
_row1["CommandValue"] = "31";
DataRow _row2 = _data.NewRow();
_row2["Text"] = "Karl";
_row2["CommandValue"] = "2";
DataRow _row3 = _data.NewRow();
_row3["Text"] = "Suzie";
_row3["CommandValue"] = "8";
DataRow _row4 = _data.NewRow();
_row4["Text"] = "Kara";
_row4["CommandValue"] = "31";
_data.Rows.Add(_row1);
_data.Rows.Add(_row2);
_data.Rows.Add(_row3);
_data.Rows.Add(_row4);
_childdata.Columns.Add(new DataColumn("Text"));
_childdata.Columns.Add(new DataColumn("CommandValue"));
DataRow _crow1 = _childdata.NewRow();
_crow1["Text"] = "Butch";
_crow1["CommandValue"] = "Cupcakes";
DataRow _crow2 = _childdata.NewRow();
_crow2["Text"] = "Karl";
_crow2["CommandValue"] = "Cheese";
DataRow _crow3 = _childdata.NewRow();
_crow3["Text"] = "Suzie";
_crow3["CommandValue"] = "Pizaa";
DataRow _crow4 = _childdata.NewRow();
_crow4["Text"] = "Kara";
_crow4["CommandValue"] = "Tofu";
DataRow _crow5 = _childdata.NewRow();
_crow5["Text"] = "Butch";
_crow5["CommandValue"] = "Bacon";
DataRow _crow6 = _childdata.NewRow();
_crow6["Text"] = "Karl";
_crow6["CommandValue"] = "Ham";
_childdata.Rows.Add(_crow1);
_childdata.Rows.Add(_crow2);
_childdata.Rows.Add(_crow3);
_childdata.Rows.Add(_crow4);
_childdata.Rows.Add(_crow5);
_childdata.Rows.Add(_crow6);
rptMain.DataSource = _data;
rptMain.DataBind();
}
public void rptMain_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
LinkButton ibtn = e.Item.FindControl("ibtnDoSomething") as LinkButton;
Label mainLabel = e.Item.FindControl("lblMainData") as Label;
ibtn.CommandArgument = drv["CommandValue"].ToString();
mainLabel.Text = drv["Text"].ToString();
DataView _dv = new DataView(_childdata, "Text = '" + drv["Text"].ToString() + "'", "Text", DataViewRowState.CurrentRows);
Repeater _rptChild = e.Item.FindControl("rptChild") as Repeater;
_rptChild.DataSource = _dv;
_rptChild.DataBind();
}
}
public void rptChild_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label childLabel = e.Item.FindControl("lblChildData") as Label;
DataRowView drv = (DataRowView)e.Item.DataItem;
childLabel.Text = drv["Text"].ToString();
}
}
public void rptMain_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
Console.Write("fdasdfsa");
}
答案 0 :(得分:1)
在回发期间将重新创建转发器。这就是OnItemCreated事件被解雇的原因。 OnItemCommand未被调用的原因是因为OnItemCreated事件中发生了异常。物业
e.Item.DataItem
在回发中不可用并返回null。因此,当您尝试使用
访问它时ibtn.CommandArgument = drv["CommandValue"].ToString();
你最终得到NullReferenceException。
答案 1 :(得分:1)
这对我来说可以阻止Item_Created
的那些令人讨厌的回发攻击protected void On_Item_Created(Object sender, DataListItemEventArgs e)
{
if (!Page.IsPostBack)
{
//Do stuff
}
}
答案 2 :(得分:0)
您是否需要在linkbutton控件上指定CommandName属性?