2

我有一个 DataList,它加载在搜索结果(url、描述、标题)中。我需要知道用户点击搜索结果的 URL。我使用链接按钮来做到这一点。但是,如果我在 LinkBut​​ton 控件中使用 PostBackUrl 属性,则 OnClick 处理程序无法调用。

//Default.aspx
    <asp:Panel ID="panResults" runat="server" Height="236px">
        <p style="text-align: left" align="left"><asp:Label runat="server" ID="lblTotal" Font-Bold="true" /></p>
        <asp:DataList ID="dlstResults" runat="server" CellPadding="4" 
            ForeColor="#333333" Width="100%" OnItemDataBound="DataList_ItemDataBound" >
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
            <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <ItemTemplate>
                <asp:LinkButton ID="lnkResultUrl" runat="server" OnClick="Link_Click" CommandArgument='<%# Eval("Url") %>'
                    PostBackUrl='<%# Eval("Url") %>' Text='<%# Eval("Title") %>' />
                <br />
                <asp:Label ID="lblResultUrl" runat="server" Font-Bold="true"
                    Text='<%# Eval("Url") %>' />
                <br />
                <asp:Label ID="lblResultSummay" runat="server" 
                    Text='<%# Eval("Description") %>' />
            </ItemTemplate>     
        </asp:DataList>   
    </asp:Panel>

//Code-behind
protected void Link_Click(object sender, EventArgs e)
{
    LinkButton btn = (LinkButton)(sender);       
    string myValue = btn.CommandArgument;
}
4

2 回答 2

3

您告诉 DataList 每次在 DataList 中的控件中发生某些事情(单击或更改某些内容)以调用该方法dlstResults_ItemCommand

您可以通过将属性OnItemCommand="dlstResults_ItemCommand"放在 DataList 中来完成此操作。

然后构建您在上述属性中键入的方法。在这种情况下,它被称为dlstResults_ItemCommand. 然后在那里您通过检查命令名称来检查哪个控件引发了事件(这是由您的 CommandArgument 属性放置的属性)。如果它是正确的控制,拉出命令参数,那就是你的网址。

<asp:DataList ID="dlstResults" runat="server" CellPadding="4" OnItemCommand="dlstResults_ItemCommand"
        ForeColor="#333333" Width="100%" OnItemDataBound="DataList_ItemDataBound" >
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
        <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <ItemTemplate>
            <asp:LinkButton ID="lnkResultUrl" runat="server"  CommandArgument='<%# Eval("Url") %>' CommandName="SomeName"
                %>' Text='<%# Eval("Title") %>' />
            <br />
            <asp:Label ID="lblResultUrl" runat="server" Font-Bold="true"
                Text='<%# Eval("Url") %>' />
            <br />
            <asp:Label ID="lblResultSummay" runat="server" 
                Text='<%# Eval("Description") %>' />
        </ItemTemplate>     
    </asp:DataList> 


protected void dlstResults_ItemCommand(Object sender, DataListCommandEventArgs e) 
{
   if(e.CommandName ="SomeName")
   {
      string url = e.CommandArgument.ToString();
         //url now holds the url of the clicked on link
   }

}
于 2013-05-17T16:13:38.107 回答
1

您可以在代码隐藏中使用 Response.Redirect 并从链接按钮中删除 PostBackUrl

<asp:DataList ID="dlstResults" runat="server" CellPadding="4" OnItemCommand="dlstResults_ItemCommand"
    ForeColor="#333333" Width="100%" OnItemDataBound="dlstResults_ItemDataBound">
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
    <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <ItemTemplate>
        <asp:LinkButton ID="lnkResultUrl" runat="server" CommandArgument='<%# Eval("Url") %>'
            CommandName="SomeName" Text='<%# Eval("Title") %>' />
        <br />
        <asp:Label ID="lblResultUrl" runat="server" Font-Bold="true" Text='<%# Eval("Url") %>' />
        <br />
        <asp:Label ID="lblResultSummay" runat="server" Text="desc" />
    </ItemTemplate>
</asp:DataList>

和代码隐藏

protected void dlstResults_ItemCommand(Object sender, DataListCommandEventArgs e)
{
    if (e.CommandName == "SomeName")
    {
        string url = e.CommandArgument.ToString();
        //do some stuff with url before you redirect
        Response.Redirect("//" + url);
    }
}
于 2013-05-17T17:09:42.883 回答