我有一个DataList,它们被加载到搜索结果中(url,description,title)。我需要知道用户点击搜索结果的URL。我这样做是使用linkbutton。但是如果我在LinkButton Control中使用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;
}
答案 0 :(得分: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
}
}
答案 1 :(得分: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);
}
}