2

I want a button click event inside a Datalist But I am not able to get so this is my code

                    <asp:DataList ID="OwnArticle" runat="server" OnItemDataBound="OwnArticle_ItemDataBound">
                        <ItemTemplate>
                            <table width="100%" cellpadding="3px" cellspacing="0">
                                <tr>
                                    <td width="20%">
                                        <asp:Image ID="img" runat="server" Height="50px" Width="45px" ImageUrl='<%#"~/UsingID.ashx?id="+ Eval("ID")%>' />
                                    </td>
                                    <td width="80%">
                                        <table width="100%" cellpadding="0" cellspacing="0">
                                            <tr>
                                                <td width="100%">
                                                    <asp:HyperLink ID="hlink" runat="server">
                                                    </asp:HyperLink>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <asp:DataList ID="innerRepeater" runat="server">
                                                        <ItemTemplate>
                                                            <asp:Label ID="lbldesc" runat="server" Text='<%# Eval("Key").ToString()%>'></asp:Label>
                                                        </ItemTemplate>
                                                    </asp:DataList>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:DataList>
                    <table cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td width="10%">

                            </td>
                            <td colspan="5" width="90%">
                                <asp:Label ID="lblArticleName" CssClass="lblCaptionGrey" runat="server" Text='<%# Eval("Article_Name").ToString()%>'></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td width="10%">

                            </td>
                            <td colspan="5" width="90%">
                                <asp:Label ID="Label2" CssClass="lblPara" runat="server" Text='<%# Eval("Article_Desc").ToString()%>'></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Button ID="btnLike" CommandName="isLike" runat="server"
                                    Text="Like" />
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:DataList>

and codebehind is

protected void ArticleRep_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "isLike")
            {
                Response.Write(@"<script language="'javascript'">alert('success.');</script>");
            }
            else
                Response.Write(@"<script language="'javascript'">alert('Error.');</script>");
        }

how to get a button click event for the button inside a datalist?

checkout the above code!!??

4

1 回答 1

1

您的数据列表名称是OwnArticle,并且您正在编写 Itembound 事件ArticleRep_ItemCommand

这是实际问题。你应该把它写在OwnArticle_ItemCommand事件下

所以写函数如下:

protected void OwnArticle_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "isLike")
            {
                Response.Write(@"<script language="'javascript'">alert('success.');</script>");
            }
            else
                Response.Write(@"<script language="'javascript'">alert('Error.');</script>");
        }

ArticleRep可能是您不同的数据列表。

于 2013-08-05T13:07:18.770 回答