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

DavidGreenfieldAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.