Reputation: 173
I have a DataList, which are loaded in the search results(url, description, title). I need to know URL for which the user click on the search results. I do this using linkbutton. But OnClick handler can't call, if I using PostBackUrl property in LinkButton Control.
//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;
}
Upvotes: 2
Views: 2684
Reputation: 3821
You tell your DataList that everytime something happens (something is clicked on, or changed) in the controls in your DataList to call the method dlstResults_ItemCommand
.
You accomplish this by placing the attribute OnItemCommand="dlstResults_ItemCommand"
in your DataList.
Then you build the method that you typed in the above attribute. In this case its called dlstResults_ItemCommand
. Then in there you check which control raised the event by checking the command name (This is an attribute placed by your CommandArgument attribute). If its the right control, pull out the commandargument and thats your url.
<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
}
}
Upvotes: 3
Reputation: 573
You can use a Response.Redirect in your code-behind and remove PostBackUrl from your linkbutton
<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>
and code-behind
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);
}
}
Upvotes: 1