[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Linkbutton - Execute Code and Navigate to URL

Posted on 2008-11-05
8
Medium Priority
?
2,020 Views
Last Modified: 2013-12-17
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
Comment
Question by:DavidGreenfield
  • 3
  • 3
  • 2
8 Comments
 
LVL 8

Accepted Solution

by:
OBonio earned 2000 total points
ID: 22886277
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22886335
You could try to response.redirect in your LinkButton_Click instead of PostBackUrl , you could that pass some value in the query string.
0
 

Author Comment

by:DavidGreenfield
ID: 22886403
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:David Robitaille
ID: 22886482
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22886671
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
 

Author Comment

by:DavidGreenfield
ID: 22886736
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
 
LVL 8

Expert Comment

by:OBonio
ID: 22886743
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
 

Author Closing Comment

by:DavidGreenfield
ID: 31513515
Would have liked a little example, but managed to work it out based on the comments!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 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