views:

232

answers:

3

In reference to http://stackoverflow.com/questions/1975229/link-loaded-into-my-gridview-try-to-navigate-to-my-local-server. The columns I have in the datagrid are Customer #, Description, Link.

I've got a function set up that's called on rowDataBound, but how do I retrieve what link is in the row so that I can edit it, and then rebind it to the datagrid?

protected void grdLinks_RowDataBound( object sender, GridViewRowEventArgs e )
{
    if ( e.Row.RowIndex == 2 )
    {

    }
}

And here is my gridview code

<asp:GridView ID="grdLinks" runat="server" AutoGenerateColumns="False" DataSourceID="ldsCustomerLinks"
            OnRowDataBound="grdLinks_RowDataBound" EmptyDataText="No data was returned."
            DataKeyNames="ID" OnRowDeleted="grdLinks_RowDeleted" Width="80%" BackColor="White"
            HorizontalAlign="Center" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
            CellPadding="3" GridLines="Vertical">
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <Columns>
                <asp:BoundField DataField="CustomerNumber" HeaderText="Customer Number" SortExpression="CustomerNumber" />
                <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                <asp:HyperLinkField DataTextField="Link" HeaderText="Link" SortExpression="Link" DataNavigateUrlFields="Link" Target="_blank" />
            </Columns>
</asp:GridView>

<asp:LinqDataSource ID="ldsCustomerLinks" runat="server" ContextTypeName="ComplianceDataContext"
            TableName="CustomerLinks" EnableDelete="true">
</asp:LinqDataSource>
+1  A: 

If I'm understanding you correctly, you want to get the value of a data item called Link. If so, then something like this should work:

EDIT: I think what you're saying is that you want to grab the value Link from the database, manipulate it and then set the Url of the HyperLink to the new, manipulated value, if so then it would look like this:

ASPX Page (Updated to reflect posted code)

<Columns>
    <asp:BoundField DataField="CustomerNumber" HeaderText="Customer Number" SortExpression="CustomerNumber" />
    <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
    <asp:TemplateField HeaderText="Link"> 
        <ItemTemplate>
            <asp:HyperLink ID="hlParent" runat="server" Text='<% #(Eval("Link")) %>' />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

I modified the ASP from your original question by adding an ID and removing the reference to the NavigateUrl attribute from the HyperLink control.

Code

protected void grdLinks_RowDataBound( object sender, GridViewRowEventArgs e )
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string link = DataBinder.Eval(e.Row.DataItem, "Link") as string;
        if (link != null && link.Length > 0)
        {
            // "FindControl" borrowed directly from DOK
            HyperLink hlParent = (HyperLink)e.Row.FindControl("hlParent");
            if (hlParent != null)
            {
                // Do some manipulation of the link value 
                string newLink = "https://" + link

                // Set the Url of the HyperLink  
                hlParent.NavigateUrl = newLink;
            }
        }
    }
}
CAbbott
Yes, that's what I wanted. However, how would I then show the updated link in the datagrid after I've modified it?
Justen
Assuming the link control is defined within the grid, then you'd want to do a FindControl that DOK mentions and set that with the value you grabbed.
CAbbott
The FindControl is being set equal to a HyperLink variable. How would I convert the HyperLink variable to a string, then back to a HyperLink?
Justen
I've added my gridview code if it clears up anything. hlParent continues to return null, although I changed it to HyperLink Link = (HyperLink).e.Row.FindControl( "Link" )
Justen
You'll want to add the Template field to your grid that I have since you're trying to override the data binding behavior. Also, hlParent is null because you need an `ID` of "hlParent" on the control.
CAbbott
Ah I get it. So if I ever want to edit the grid data, it needs to be in a TemplateField. Good to know. Thanks for all your help, I really appreciate it.
Justen
+1  A: 

RowDataBound is called for every row in the GridView, including headers, footers, etc. Therefore, you should start by examining only the rows containing data.

Once you're on a row, there are several ways to examine the cells. One is just to use the cell index (2 here). That seems simple in your situation, but will lead to frustration if you ever rearrange the columns.

Here's an example of that from MSDN:

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Display the company name in italics.
      e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";

    }

A better way is to use FindControl with the item's ID.

protected void gvBarcode_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
     HyperLink hlParent = (HyperLink)e.Row.FindControl("hlParent");
   }
}
DOK
I'm not quite sure how I would edit it from the FindControl() method. What would I do to go from Hyperlink to String and back to Hyperlink?
Justen
In the code snippet above, you have a reference to the HyperLink object hlParent. You can now write code such as hlParent.NavigateURL = "someURL". In your code, just type hlParent and a dot and leet ntellisense show you your options.
DOK
I tried that and it gives me an error:"Object reference not set to an instance of an object."
Justen
I've added my gridview code if it clears up anything
Justen
Thanks for you help, I got it now.
Justen
A: 

You may also want to look into letting the gridview do it for you.

You can use the datanavigateurlformatstring property to insert querystring parameters if that's what you are trying to do.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlinkfield.datanavigateurlformatstring.aspx

Brian Schmitt
Well it's not just inserting parameters, I have to check first. Like if the user entered "google.com", I need to format that to "http://www.google.com" or if they enter "www.stackoverflow.com", I need that to be formatted to "http://www.stackoverflow.com" If there is a way to do that with datanaviagteformatstring, then I'd be pleased to know.
Justen
well, this site autoformats it, but I need to add http:// and www. to links where they are not entered by the user so that it becomes an absolute URL and not a relative URL
Justen
Ahh I see now - Do you modify it when the user types it in? or only for display?
Brian Schmitt
I keep what the user typed in and display that, but I modify the navigate URL.
Justen