Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modifying DataNavigateUrlField or DataNavigateUrlFormatString in Grid 1

Status
Not open for further replies.

AndyH1

Programmer
Jan 11, 2004
350
GB
I have a datagrid declared, in which I have a hyperlink

<asp:HyperLinkColumn HeaderText="Title" DataNavigateUrlField="txtFileName" DataNavigateUrlFormatString=" DataTextField="txtTitle" Target="_blank">

I can't change this column from a hyperlink column as other code (written by someone else) relies on it being of this type to extract the filename, however at the moment the is hardwired in the code (its a different site where it gets the docs from)

I want to put this site URL in the web.config:

<appSettings>
<add key="strDocDirectoryURL" value=" />
</appSettings>

and insert it in the DataNavigateUrlFormatString during the databind. I'm guess I'd have to use something like
Private Sub dgDocuments_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgDocuments.ItemCommand

but am not sure how to use this and how I can get the value and 'append' the url to the '/doc{0}' of the DataNavigateUrlFormatString (or append it to the DataNavigateUrlField which might be better). Is there maybe some simpler wayI'm missing?

The whole datagrid is below

<asp:datagrid id="dgDocuments" runat="server" DataKeyField="ID">
Columns>
<asp:templateColumn HeaderText="&nbsp;">
<itemtemplate>
<a href="/Downloading/FileDownload.aspx?file=<%#DataBinder.Eval(Container.DataItem, "txtFileName")%>" target=_self>
<img src="/images/media/<%#DataBinder.Eval(Container.DataItem, "txtImage")%>" width="17" height="20" border="0" alt="*"/></a>
</itemtemplate>
</asp:templateColumn>
<asp:HyperLinkColumn HeaderText="Title" DataNavigateUrlField="txtFileName" DataNavigateUrlFormatString=" DataTextField="txtTitle" Target="_blank">
</asp:HyperLinkColumn>
<asp:BoundColumn DataField="dteDateUploaded" ReadOnly="True" HeaderText="Uploaded" DataFormatString="{0:dd/MM/yy}">
<HeaderStyle Width="80px"></HeaderStyle>
</asp:BoundColumn>
<asp:templateColumn HeaderText="&nbsp;">
<itemtemplate>
<a href="UpdateDocumentForm.aspx?ID=<%#DataBinder.Eval(Container.DataItem, "ID")%>">Edit&nbsp;Details</a>
</itemtemplate>
</asp:templateColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>
 
I put together an example, however this is using a gridview, not a datagrid, but I think you can convert it easily:
Code:
  Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
        'You will have to change the cell index in the next line to match the column you want to reference.
            Dim hl As HyperLink = e.Row.Cells(4).Controls(0)
            hl.NavigateUrl = "You Text From Web.Config Here"
        End If
    End Sub
 
Thanks JBenson,

Have tried something similar using

Private Sub dgDocuments_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgDocuments.ItemDataBound

Dim link As HyperLink

If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
link = dgDocuments.Items(CInt(e.Item.ItemIndex)).Cells(1).Controls(0)
link.NavigateUrl = " End If
End Sub

But am getting

[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]

on the
link = dgDocuments.Items(CInt(e.Item.ItemIndex)).Cells(1).Controls(0)

line

Have tried Cells(0), Cells(1), Cells(2)etc but still get same error. Even tried replaceing e.Item.ItemIndex with 0, 1, 2 etc to see if I can at least do it to one row - same problem

Can't figure what I'm doing wrong:-(


AndyH1
 
JBenson,

Got it! Needed to use

link = e.Item.Cells(1).Controls(0)

instead of

link = dgDocuments.Items(CInt(e.Item.ItemIndex)).Cells(1).Controls(0)

Thanks for your help
Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top