前台是aspx页面:
<body>
<form id="form1" runat="server">
<div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DeleteMethod="Delete" InsertMethod="Insert"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
TypeName="TestDemo.DAL.DataSetMessageTableAdapters.T_MessageTableAdapter"
UpdateMethod="Update">
<DeleteParameters>
<asp:Parameter Name="Original_Id" Type="Int64" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Message" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Message" Type="String" />
<asp:Parameter Name="Original_Id" Type="Int64" />
</UpdateParameters>
</asp:ObjectDataSource>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1"
onitemdatabound="Repeater1_ItemDataBound">
<--上面是数据源-->
<HeaderTemplate><table border="1"></HeaderTemplate>
<ItemTemplate>
<tr ID="tr1" runat="server"><td><%#Eval("Id") %>,<%#Eval("Message") %></td></tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
后台:执行Repeater控件的ItemDataBound事件
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item) {
(e.Item.FindControl("tr1") as HtmlTableRow).Attributes.Add("onclick", "alert(1);");
}
}
执行效果是:单击奇数行的都可以执行,偶数行的没反应?????
哦,找到原因了,后代代码应该改为:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
(e.Item.FindControl("tr1") as HtmlTableRow).Attributes.Add("onclick", "alert(1);");
}
}