Knowledgebase Home Page  >  SearchUnit
Search the Knowledge Base
How can I handle result click events (or how to use a LinkButton in results)? (C#, VB.NET)
https://keyoti.com/kb/Default.aspx?ToDo=view&questId=134&catId=54

Options

Print this page
Email this to a friend
If you use a LinkButton control in the search result template to show the link, instead of the default <A> tag, you will be able to handle the event in code behind.

Open the ASPX holding the SearchResult control and look at our default template, you'll see this section

<ResultItemTemplate>
<DIV class="SEResultItem">
<TABLE border="0">
<TR>
<TD class="SEResultItemLink" style="FONT-SIZE: 10pt; FONT-FAMILY: sans-serif, Verdana, Arial, Helvetica">
<A href="<%# Container.Uri %>"><%# Container.Title %></A>
</TD>
</TR>
<TR>
<TD class="SEResultItemSummary" style="FONT-SIZE: 9pt; FONT-FAMILY: sans-serif, Verdana, Arial, Helvetica"><%# Container.Summary%></TD>
</TR>
<TR>
<TD class="SEResultItemURL" style="FONT-SIZE: 8pt; COLOR: green; FONT-FAMILY: sans-serif, Verdana, Arial, Helvetica"><%# Container.Uri %></TD>
</TR>
</TABLE>
</DIV>
<BR>
</ResultItemTemplate>

The part in bold (<A>) needs to be removed and replaced with this:

<asp:LinkButton id="LinkButton1" runat="server" OnClick="linkClick" CommandArgument="<%# Container.Uri %>"
><%# Container.Title %></asp:LinkButton>
 
 
So it will of course call 'linkClick' when it is clicked.

 
linkClick is an event handler like this:


//C#
public void linkClick(object sender, System.EventArgs e)
{
    LinkButton link = sender as LinkButton;
    string URL = link.CommandArgument;
    //do whatever you need here
    //---
    Page.Response.Redirect(URL, false);
}


'VB
Public Sub linkClick(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim link As LinkButton = CType(sender,LinkButton)
    Dim URL As String = link.CommandArgument
    'do whatever you need here
    '---
    Page.Response.Redirect(URL, false)
End Sub


You see that the target URL is stored in the CommandArgument property, and then this event handler redirects to the URL when finished.

Related Questions:

Attachments:

No attachments were found.