Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Setting 'OnClick' event for asp .net 'LinkButton' in a 'Repeater'

Posted on 2009-02-11
3
Medium Priority
?
5,430 Views
Last Modified: 2013-11-07
Hello,

I have a LinkButton in a asp .net repeater. I am able to set the text as desired from the databound field however I am not being able to set the onclick event.

When a user clicks the button in the repeater it needs to call a function in the codebeind and pass along a value that it is also databound.

I have tried a few different syntax and mostly I've gotten errors such 'server tag is not well formed'. The closest I've gotten is given below, however the problem is that when the repeater controls are being rendered and it is rendering the 'onclick' value it actually executes the function that should be called when the user click the button

Any help would be greatly apprecaited.
Code for the linkbuttonn:
 
<asp:LinkButton ID="LinkButton1" OnClick='<%# auditReport(DataBinder.Eval(Container.DataItem, "erid"))%>'  Text='<%# formatTitle(DataBinder.Eval(Container.DataItem, "title"))%>'  runat="server">LinkButton</asp:LinkButton>
 
 
 
and the function being called:
 
protected string auditReport(object sender)
    {
        string result = "";
        Session["ERID"] = sender.ToString();
        Response.Redirect("auditreport.aspx");
        return result;
    }

Open in new window

0
Comment
Question by:OliShim
3 Comments
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23617328
If you are using a Repeater, you'll probably want to use the Repeater_ItemCommand function, and get the ID from there.  Could you paste the code for your repeater?  What are you binding the repeater to?
0
 
LVL 10

Accepted Solution

by:
jabcoco earned 2000 total points
ID: 23618647
Do it like this.
Use the Command action from the link button and pass id with the CommandArgument property.

hope this help.
.aspx
------------------ 
<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" CommandName="MyUpdate" CommandArgument='<%# Eval("erid") %>'>LinkButton</asp:LinkButton>
    </ItemTemplate>
</asp:Repeater>
 
 
.cs
------------------
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
    if (e.CommandName == "MyUpdate"){
        //e.CommandArgument --> contain the erid value
        //Do something
    }
}

Open in new window

0
 
LVL 8

Expert Comment

by:bedanand
ID: 23619593
Follow jabcoco's idea.

We cant pass parameter using OnClick event handler.
You must use OnCommand.


0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Loops Section Overview

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question