views:

159

answers:

3

Hi my brains hurting trying to think of how to do this.

I have a list of asp:hyperlinks created by a repeater. When i click on one of these hyperlinks i want it to pass the value set in the hyperlink into the code behind so i know which hyperlink has been clicked. so...

<asp:Repeater id="rpt1" runat="server">
    ...some rows of hyperlinks..
   <li><asp:HyperLink ID="hlValue" CssClass='example8' NavigateUrl="#" 
       OnClick="passavalue(books)" >books</asp:hyperlink></li>
    ...
</asp:repeater>

c#

static void passavalue(string value)
{
    response.Write(vlaue);
}

I know this wont work I'm just trying to explain what I'm trying to do.

A: 

Your best bet in my opinion would be to use a QueryString or Route parameter in the NavigationUrl you set for each hyperlink... Your repeater could set the custom value for each link using the DataBinder behavior <%# Eval("") %> etc...

fdfrye
when they click the hyperlink its opening a colorbox where i have another repeater, depending on what they have clicked i want to order the repeater in the colorbox so that the value they have clicked appears first. If i use the navigateurl this would take me off the page to apply the querystrin, which i dont want to do. i need it to run a method when clicked.
phil crowe
Ah, in that case, you are going to have to either go with the approach from below by Peter, or you will need to do your manipulation in javascript using a similar function call..like your "passavalue()"..
fdfrye
A: 

One option is to use an asp:linkbutton instead of the asp:hyperlink in your ItemTemplate.

<asp:LinkButton runat="server" ID="DoSomethingLink" CommandName="DoSomething" CommandArgument='<%# Eval("Name") %>'><%# DataBinder.Eval(Container.DataItem, "Name") %></asp:LinkButton>

You would then handle the Repeater's ItemCommand event.

protected void YourRepeater_ItemCommand(object source, RepeaterCommandEventArgs e) 
{ 
    if (e.CommandName == "DoSomething") 
    { 
        var arg = e.CommandArgument; 
        // do something ...
    } 
} 
Peter
I would add that LinkButton is a regular hyperlink that uses JavaScript to POST to the server (to simulate a real button), and thus has Accessibility and SEO issues. I would avoid it's use on a public facing site, but an internal/intranet site is fine.
Sunday Ironfoot
A: 

You can do this by binding to the CommandArgument some value (probably an ID I would guess). Here is an example of a LinkButton that you could add to your Repeater ItemTemplate:

<ItemTemplate>
    <div>
        <asp:LinkButton ID="lbTest" runat="server"
            CommandArgument='<%# Eval("ID") %>' Text='<%# Eval("Text") %>' 
            OnClick="lbTest_Click" />
    </div>
</ItemTemplate>

You can then grab the value server side by implementing the OnClick for the LinkButton. You can you the same even for all buttons in the repeater if you want and not have to bother with the RowCommand event.

protected void lbTest_Click(object sender, EventArgs e)
{
    LinkButton lb = (LinkButton)(sender);
    string valueYouWanted = lb.CommandArgument;
    // do something with it
}

Edit: Based on reading your code added, if you want to stick to using a asp:HyperLink and not a link button, the OnClick will fire javascript, not server side so you would need to use the <%# Eval("ID") %> (or whatever field you want get use) to add the unique identifier to your javascript inline. Example:

<asp:HyperLink ID="hlValue" CssClass='example8' NavigateUrl="#"  
    OnClick='passavalue(<%# Eval("YourBookID") %>)' >books</asp:hyperlink>
Kelsey
@phil crowe did any of these answers work for you? If not, edit your question and we can try and come up with some better answers.
Kelsey