Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
need to set the Text in a gridview column to something other than what shows up in gridview with this code:
ASP.NET
<asp:LinkButton ID="lnkCustomer" runat="server"
     OnClick="lnkCustomer_Click" Text='<%# Eval("ImgLnk") %>'></asp:LinkButton>

Of course what shows up is the contents of "ImgLnk" in string form which is a hyper link ie "http://...." I would rather it say "Picture" in gridview.

I am trying to use this combo:

Default aspx:
ASP.NET
<asp:TemplateField HeaderText="StoreImg">
                        <ItemTemplate><asp:LinkButton ID="LinkButton1" runat="server"
                        OnClick="lnkCustomer_Click" Text='Picture'
                            CommandName="CustomerLink" CommandArgument='<%# Eval("ImgLnk") %>'></asp:LinkButton>

Code behind:
C#
protected void lnkCustomer_Click(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "CustomerLink")
        {
            Response.Redirect(e.CommandArgument.ToString());

       }

I get this error message:

No overload for 'lnkCustomer_Click' matches delegate 'System.EventHandler'	Default.aspx		

I have tried and tried, but I can't seem to make the correction. Please help.

Thanks!
Posted
Updated 10-Aug-12 10:12am
v2
Comments
[no name] 10-Aug-12 16:07pm    
Try protected void lnkCustomer_Click(object sender, EventArgs e)
Member 8759797 10-Aug-12 16:36pm    
Thanks everyone for your help to this point! When I have tried your solution of (...EventArgs e)I get this message which has added to my confusion: the 'System.EventArgs' does not contain a definition for 'CommandName' and no extension method 'CommandName' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)

Please continue to offer suggestions...I will try them all!
Member 13928033 3-Oct-18 5:19am    
i tried nd tried but this code didnt work yet.so please post proper solution

As Wes has pointed out in his comment, the definition of your method is incorrect.
If you are responding to a click event on a LinkButton as you are in this case, then the method must have a definition that matches protected void MethodName(object sender, EventArgs e). Yours is using GridViewCommandEventArgs which is used to respond to Command events from a DataGrid.

Update: If the LinkButton is used from within the GridView, then you won't want to have the onclick handler setup as part of the LinkButton html element, but rather in the oncommand handler for the grid itself, in which case your original code will work as is.
 
Share this answer
 
v2
Comments
Member 8759797 10-Aug-12 16:36pm    
Thanks everyone for your help to this point! When I have tried your solution of (...EventArgs e)I get this message which has added to my confusion: the 'System.EventArgs' does not contain a definition for 'CommandName' and no extension method 'CommandName' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)

Please continue to offer suggestions...I will try them all!
fjdiewornncalwe 10-Aug-12 16:38pm    
You can ignore the if statement you have. You don't need to know the command name because the control firing the command is known to the handler already. Just remove the if statement and it will work as you want it to.
Sergey Alexandrovich Kryukov 10-Aug-12 16:38pm    
Correct, a 5.
--SA
fjdiewornncalwe 10-Aug-12 16:57pm    
Thanks, Sergey.
Wendelius 10-Aug-12 16:46pm    
Indeed, 5+
The System.EventHandler delegate expects you to have a method with a specific signature present. See: EventHandler Delegate[^]

So your code should probably be like:
C#
protected void lnkCustomer_Click(object sender, System.EventArgs e)
    {
        ...
    }


Addition:
Casting the sender to link button:
C#
protected void lnkCustomer_Click(object sender, System.EventArgs e)
    {
        LinkButton button = sender as LinkButton;
        if (button.CommandName == "CustomerLink")
           ...
    }
 
Share this answer
 
v2
Comments
Member 8759797 10-Aug-12 16:36pm    
Thanks everyone for your help to this point! When I have tried your solution of (...System.EventArgs e)I get this message which has added to my confusion: the 'System.EventArgs' does not contain a definition for 'CommandName' and no extension method 'CommandName' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)

Please continue to offer suggestions...I will try them all!
Wendelius 10-Aug-12 16:45pm    
The EventArgs is a 'generic' class and doesn't hold this information. However, if you cast the sender to a linkbutton, you'll be able to fetch the CommandName.

See the updated answer.
Sergey Alexandrovich Kryukov 10-Aug-12 16:38pm    
Right, a 5.
--SA
Wendelius 10-Aug-12 16:46pm    
Thanks SA :)
Member 8759797 10-Aug-12 17:36pm    
So this technique works wonderfully. The image was showing in a modal popup up window and now it is not. However, I think there must be a problem that is differently related so...

This is the answer for me! Thanks so much each of you. If anyone has a suggestion of a book that might help as I learn, I will be most grateful.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900