1

我的 sql 数据库中有 10 个问题。我得到它们并使用 LinkBut​​ton 以链接形式打印它们。现在我想分别生成每个问题的点击事件。

如何在 ListView 中生成 LinkBut​​ton 的 OnClick 事件。请通过简单的步骤提供帮助。

  <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="userid" >
                    <AlternatingItemTemplate>
                        <tr style="">
                            <td>
                               <%-- <asp:Label ID="qtLabel" runat="server" Text='<%# Eval("qt") %>' />
                       --%>
                                 <h3> <asp:LinkButton CommandArgument='<%# Eval("userid") %>' ID="LinkButton1" runat="server"><%# Eval("qt") %></asp:LinkButton>  </h3>

                            </td>
                            <td>
                                <asp:Label ID="useridLabel" runat="server" Text='<%# Eval("userid") %>' />
                            </td>
                        </tr>
                    </AlternatingItemTemplate>
                    <EditItemTemplate>
                        <tr style="">
                            <td>
                                <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                            </td>
                            <td>
                                <asp:TextBox ID="qtTextBox" runat="server" Text='<%# Bind("qt") %>' />
                            </td>
                            <td>
                                <asp:Label ID="useridLabel1" runat="server" Text='<%# Eval("userid") %>' />
                            </td>
                        </tr>
                    </EditItemTemplate>
                    <EmptyDataTemplate>
                        <table runat="server" style="">
                            <tr>
                                <td>No data was returned.</td>
                            </tr>
                        </table>
                    </EmptyDataTemplate>
                    <InsertItemTemplate>
                        <tr style="">
                            <td>
                                <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
                            </td>
                            <td>
                                <asp:TextBox ID="qtTextBox" runat="server" Text='<%# Bind("qt") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="useridTextBox" runat="server" Text='<%# Bind("userid") %>' />
                            </td>
                        </tr>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <tr style="">
                            <td>
                               <h3> <asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("userid") %>' runat="server"><%# Eval("qt") %></asp:LinkButton>  </h3>
                            </td>
                            <td>
                                <asp:Label ID="useridLabel" runat="server" Text='<%# Eval("userid") %>' />
                            </td>
                        </tr>
                    </ItemTemplate>
                    <LayoutTemplate>
                        <table runat="server">
                            <tr runat="server">
                                <td runat="server">
                                    <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                                        <tr runat="server" style="">
                                            <th runat="server">qt</th>
                                            <th runat="server">userid</th>
                                        </tr>
                                        <tr id="itemPlaceholder" runat="server">
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr runat="server">
                                <td runat="server" style=""></td>
                            </tr>
                        </table>
                    </LayoutTemplate>
                    <SelectedItemTemplate>
                        <tr style="">
                            <td>
                                <asp:Label ID="qtLabel" runat="server" Text='<%# Eval("qt") %>' />

                            </td>
                            <td>
                                <asp:Label ID="useridLabel" runat="server" Text='<%# Eval("userid") %>' />
                            </td>
                        </tr>
                    </SelectedItemTemplate>
                </asp:ListView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>" SelectCommand="SELECT [qt], [userid] FROM [java]"></asp:SqlDataSource>
4

1 回答 1

0

ASPX:

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" 
      DataKeyNames="userid" onitemcommand="ListView1_ItemCommand" >
                <AlternatingItemTemplate>
                    <tr style="">
                        <td>
                           <%-- <asp:Label ID="qtLabel" runat="server" Text='<%# Eval("qt") %>' />
                   --%>
                             <h3> <asp:LinkButton CommandArgument='<%# Eval("userid") %>' ID="LinkButton1" runat="server"><%# Eval("qt") %></asp:LinkButton>  </h3>

                        </td>
                        <td>
                            <asp:Label ID="useridLabel" runat="server" Text='<%# Eval("userid") %>' />
                        </td>
                    </tr>
                </AlternatingItemTemplate>
                <EditItemTemplate>
                    <tr style="">
                        <td>
                            <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                        </td>
                        <td>
                            <asp:TextBox ID="qtTextBox" runat="server" Text='<%# Bind("qt") %>' />
                        </td>
                        <td>
                            <asp:Label ID="useridLabel1" runat="server" Text='<%# Eval("userid") %>' />
                        </td>
                    </tr>
                </EditItemTemplate>
                <EmptyDataTemplate>
                    <table id="Table1" runat="server" style="">
                        <tr>
                            <td>No data was returned.</td>
                        </tr>
                    </table>
                </EmptyDataTemplate>
                <InsertItemTemplate>
                    <tr style="">
                        <td>
                            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
                        </td>
                        <td>
                            <asp:TextBox ID="qtTextBox" runat="server" Text='<%# Bind("qt") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="useridTextBox" runat="server" Text='<%# Bind("userid") %>' />
                        </td>
                    </tr>
                </InsertItemTemplate>
                <ItemTemplate>
                    <tr style="">
                        <td>
                           <h3> <asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("userid") %>' runat="server"><%# Eval("qt") %></asp:LinkButton>  </h3>
                        </td>
                        <td>
                            <asp:Label ID="useridLabel" runat="server" Text='<%# Eval("userid") %>' />
                        </td>
                    </tr>
                </ItemTemplate>
                <LayoutTemplate>
                    <table id="Table2" runat="server">
                        <tr id="Tr1" runat="server">
                            <td id="Td1" runat="server">
                                <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                                    <tr id="Tr2" runat="server" style="">
                                        <th id="Th1" runat="server">qt</th>
                                        <th id="Th2" runat="server">userid</th>
                                    </tr>
                                    <tr id="itemPlaceholder" runat="server">
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr id="Tr3" runat="server">
                            <td id="Td2" runat="server" style=""></td>
                        </tr>
                    </table>
                </LayoutTemplate>
                <SelectedItemTemplate>
                    <tr style="">
                        <td>
                            <asp:Label ID="qtLabel" runat="server" Text='<%# Eval("qt") %>' />

                        </td>
                        <td>
                            <asp:Label ID="useridLabel" runat="server" Text='<%# Eval("userid") %>' />
                        </td>
                    </tr>
                </SelectedItemTemplate>
            </asp:ListView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>" 
            SelectCommand="SELECT [qt], [userid] FROM [java]"></asp:SqlDataSource>

代码背后:

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    string userid = e.CommandArgument.ToString();

    Response.Write("U:" + userid);
    if (userid == "USER_3730")
    {
        Response.Redirect("bnbnbnbnbn.aspx");
    }
    if (userid == "USER_b163")
    {
        Response.Redirect("home.aspx");
    }
}
于 2013-09-01T13:24:19.160 回答