• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2042
  • Last Modified:

Linkbutton - Execute Code and Navigate to URL

Hi there

I have a GridView with a custom template field with an item Template of a LinkButton.

I *was* populating the postbackURL of the link button in the rowboundevent and this was all working fine!  I now need to add some code before I do the navigate (with an opt out if possible if the code errors).  I have tried looking at the click and command options of the linkbutton and I am getting in a mess, it either seems to be posting back to itself and ignoring the function I have created or not doing anything at all!

Can someone please help and tell me what I am doing wrong and how I can get at the very least an apperance of a hyperlink in a gridview, which when clicked I can 1) get details of the row in the gridview (i.e values in other columns), run code (a sproc, using these values) and then navigate to another page.

Thanks in advance!!!

protected void Numbers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
 
            //change link when clicked
            LinkButton ib3 = (LinkButton)e.Row.FindControl("lnk_Number");
 
 
            //This code was working fine, but I need to run code before redirect
            //ib3.PostBackUrl = "~/insert_To_Basket.aspx?Value=" + e.Row.Cells[col_Value].Text + "&Type=" + Range_Type;
 
            ib3.Text = e.Row.Cells[col_Phonenumber].Text;
            ib3.PostBackUrl = "";
 
        }
 
    }
 
    private void LinkButton_Click(System.Object sender, System.EventArgs e) 
    {
        // Just in here to see if code is firing
        string i;
        i = sender.ToString(); 
    }

Open in new window

0
DavidGreenfield
Asked:
DavidGreenfield
  • 3
  • 3
  • 2
1 Solution
 
OBonioCommented:
I would use the CommandName and CommandArgument attributes of the LinkButton and then handle the OnRowCommand event of the Gridview.  Set the CommandArgument to the id of the record it represents.  In the RowCommand event handler, e.CommandArgument will be the id needed to pull data.
0
 
David RobitailleAnalyst ProgrammerCommented:
You could try to response.redirect in your LinkButton_Click instead of PostBackUrl , you could that pass some value in the query string.
0
 
DavidGreenfieldAuthor Commented:
OBonio - do you have an example of how this works?  I have tried using CommandName and Argument and I couldn't get a command event to fire, I was creating it in the rowbound i.e. ib3.Command = += CommadEvent .....

DavRob60 - My problem is that I can't get LinkButton_Click to fire, my plan was to use response.redirect but if click doesn't fire it won't work.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
David RobitailleAnalyst ProgrammerCommented:
i think the fact you use PostBackUrl maks the click event. is you leave the PostBackUrl blank, the event should fire.
of has OBonio said use CommandName and CommandArgument , but you should set PostBackUrl blank, because all event occurs in the postback of the same page.
0
 
David RobitailleAnalyst ProgrammerCommented:
I don`t know if you understand what i was trying to said in my last post.
Event occurs during post back, but you have to postback to the same page so they cannot be handled by the page. that why setting PostBackUrl to an other page diden`t trigger any event.
0
 
DavidGreenfieldAuthor Commented:
DavRob60 - The postbackurl that was in the code was commented out, it wasn't being set so that wasn't the problem.

Thank you anyway.

OBonio - You were right, I have managed to get the code working. I have attached the code to help anyone else in the same boat!!
    
set in RowBound...
 
            ib3.CommandArgument = a value;
            ib3.CommandName = "Command";
 
protected void Row_Command(Object sender, GridViewCommandEventArgs e)
    {
 
            if (e.CommandName == "Command")
            {
                // Convert the row index stored in the CommandArgument
                int index = e.CommandArgument.ToString();
}
}

Open in new window

0
 
OBonioCommented:
I think the fact you're setting the CommandName dynamically might be confusing things.  Set it in the mark up so that it gets bound at instanciation.



<asp:GridView ID="GridView1" runat="server" OnRowDataBound="gvAuraFiles_RowDataBound" OnRowCommand="gvAuraFiles_RowCommand" MouseOverColor="Transparent">
    <Columns>
        <asp:BoundField DataField="AuraFileId" HeaderText="AuraFileId" ReadOnly="True" SortExpression="AuraFileId" />
        <asp:BoundField DataField="DateCreated" HeaderText="DateCreated" ReadOnly="True"
            SortExpression="DateCreated" />
        <asp:BoundField DataField="Filename" HeaderText="Filename" SortExpression="Filename" />
        <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
        <asp:TemplateField InsertVisible="False" ShowHeader="False">
            <itemtemplate>
                <asp:LinkButton runat="server" Text="Edit" CommandName="Edit" CommandArgument='<%# Eval("AuraFileId") %>' CausesValidation="False" id="btnEdit"></asp:LinkButton>&nbsp;
                <asp:LinkButton runat="server" Text="Delete" CommandName="Delete" CausesValidation="False" id="btnDelete"></asp:LinkButton>
            </itemtemplate>
            <controlstyle cssclass="ControlStyle" />
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
In code behind :
 
protected void gvAuraFiles_RowCommand(object sender, GridViewCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case "Edit" :
            ...e.CommandArgument has the id...
            break;
    }
}

Open in new window

0
 
DavidGreenfieldAuthor Commented:
Would have liked a little example, but managed to work it out based on the comments!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now