首页新闻找找看学习计划

为Repeater控件的行设置onclick事件,为什么事件执行的都是奇数行?

0
悬赏园豆:80 [已关闭问题] 关闭于 2011-11-07 20:35

前台是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);");
            
            }
        }

执行效果是:单击奇数行的都可以执行,偶数行的没反应?????

Jbp的主页 Jbp | 初学一级 | 园豆:141
提问于:2011-11-07 20:27
< >
分享
所有回答(1)
0

哦,找到原因了,后代代码应该改为:

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);");
            
            }
        }

Jbp | 园豆:141 (初学一级) | 2011-11-07 20:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册