2

I'm currently working on an in house project, I've created a GridView connected to a SQL table which looks like this:

GridView1

Example image of GridView1

I created the view content buttons using the following code:

<Columns>
    <asp:ButtonField ButtonType="Button" Text="View Content" CommandName="Select" />
    <asp:BoundField DataField="ContentID" HeaderText="ContentID" InsertVisible="False" ReadOnly="True" SortExpression="ContentID" />
    <asp:BoundField DataField="Code" HeaderText="Code" SortExpression="Code" />
    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
    <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
    <asp:BoundField DataField="URL" HeaderText="URL" Visible="true" SortExpression="URL" />                
</Columns>

But this is where I am now stuck. I would like to click on the view content button and have it navigate to the URL on the selected row.

The URL comes from the SQL Table and there is a string so I'd imagine, it would need to be converted first but I could be wrong.

I started to put my code in the following:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewCommandEventArgs x = (GridViewCommandEventArgs)e;
    if (x.CommandName == "Select")
    {
        GridViewRow row = GridView1.SelectedRow;
    }
}

2 Answers 2

1

Add your code in GridView1_RowCommand event of gridview:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
       if (e.CommandName == "Select")
        {
            GridViewRow row = (GridViewRow)(((BoundField)e.CommandSource).NamingContainer);
            int index = row.RowIndex;

            string url = (GridView1.Rows[index].FindControl("URL") as BoundField).Text;
            Response.Redirect(url); // url can be from your sql table

        }    
}

Note: Don't forget to add OnRowCommand event in GrindView <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" >

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the response. I have followed your advice and it now appears to want to do something, i get the following error though. Unable to cast object of type 'ASP.enquirehomepage_aspx' to type 'System.Web.UI.WebControls.GridViewRow'. Any ideas?
Cast your url from Label to BoundField.. see my edit
I have made the suggested change but I get the following error. cannot convert type 'system.web.ui.control' to 'system.web.ui.webcontrols.boundfield'. This is after changing Label to BoundField. Are you familiar with this error? I've tried to research it myself but have found nothing useful in this context.
use Control instead of BoundField
Control does not contain a definition for Text though.
1
            GridViewRow row = (GridViewRow)(((BoundField)e.CommandSource).NamingContainer);
            int index = row.RowIndex;

            string url = (GridView1.Rows[index].FindControl("URL") as BoundField).Text;
            Response.Redirect(url); // url can be from your sql table

So i made the change. Unfortunately, BoundField does not contain a definition for NamingContainer.

Also, GridView1.Rows[index].FindControl("URL") as BoundField fails due to the following error, cannot convert type 'system.web.ui.control' to 'system.web.ui.webcontrols.boundfield' via a reference conversion, boxing conversion, unboxing conversion or null type conversion.

1 Comment

so you have to convert your gridview in templatefield to work with my answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.