I am populating a gridview and when the user clicks on select it will populate the textboxes on the page. Everything is being populated except the data hyperlink column in my gridview. Any ideas why?
Some more code would really be helpful in trying to help you resolve this. I assume you have a hyperlink you're tyring to populate based on text in a gridview?
Where is this "select" that is being clicked? Is it an item template field in the gridview? Basically we need to know how your gridview is being populated. What exactly are you clicking on to populate your other field? Where are these fields you're trying
to populate?
I tried the code below, but now I am getting the error Specified argument was out of the range of valid values.
Parameter name: index?
Private Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
For Each dr In GridView1.Rows
If e.CommandName = "Select" Then
Dim index As Integer = Convert.ToString(e.CommandArgument)
Dim row As GridViewRow = GridView1.Rows(index)
Me.NameTextBox.Text = Server.HtmlDecode(row.Cells(1).Text)
Me.Product1DropDown.SelectedValue = Server.HtmlDecode(row.Cells(2).Text)
Me.Product2DropDown.SelectedValue = Server.HtmlDecode(row.Cells(3).Text)
Me.JobNumberTextBox.Text = Server.HtmlDecode(row.Cells(4).Text)
Me.AdTextBox.Text = Server.HtmlDecode(row.Cells(5).Text)
Me.MediaTypeDropDown.SelectedValue = Server.HtmlDecode(row.Cells(6).Text)
Me.AuthorDropDown.SelectedValue = Server.HtmlDecode(row.Cells(7).Text)
Me.PublicationDropDown.SelectedValue = Server.HtmlDecode(row.Cells(8).Text)
Me.RunDatesTextBox.Text = Server.HtmlDecode(row.Cells(9).Text)
Me.CostTextBox.Text = Server.HtmlDecode(row.Cells(10).Text)
Me.CostCenterTextBox.Text = Server.HtmlDecode(row.Cells(11).Text)
Dim myHyperLink As String = GridView1.DataKeys(dr.RowIndex)("PDFFile")
' do something for each row in the gridview.
Me.HyperLink1.Text = myHyperLink
Me.HyperLink1.NavigateUrl = CType((row.Cells(13).Controls(13)), HyperLink).Text
Me.CommentsTextBox.Text = Server.HtmlDecode(row.Cells(12).Text)
Me.TextBox1.Text = Server.HtmlDecode(row.Cells(14).Text)
Label14.Text = "Update"
End If
Next
End Sub
I think you are doing this on the wrong event. Try it in the row Select event...
Also you do not need to use the For Each stmt if you are already on the Row index selected. It is for when you do not know which row was selected or you want to search each row and perform some action each row.
ProtectedSub GridView1_SelectedIndexChanging(ByVal
sender AsObject,
ByVal e
As System.Web.UI.WebControls.GridViewSelectEventArgs)
Handles GridView1.SelectedIndexChanging
' add you code in here. This is when the "Select" is chosen.
MKozlowski
Member
500 Points
573 Posts
Unable to read link from gridview?
Apr 27, 2012 07:34 PM|LINK
Hi,
I am populating a gridview and when the user clicks on select it will populate the textboxes on the page. Everything is being populated except the data hyperlink column in my gridview. Any ideas why?
Me.HyperLink1.Text = Server.HtmlDecode(row.Cells(13).Text) Me.HyperLink1.NavigateUrl = Server.HtmlDecode(row.Cells(13).Text)march11
Contributor
3001 Points
1361 Posts
Re: Unable to read link from gridview?
Apr 27, 2012 08:46 PM|LINK
Add the hyper link name to DataKeyNames property of the Gridview then call it this way.
Dim myHyperLink As String = GridView1.DataKeys(dr.RowIndex)("gvHyperLink")MKozlowski
Member
500 Points
573 Posts
Re: Unable to read link from gridview?
Apr 27, 2012 08:52 PM|LINK
What is dr? It tells me it is not declared Thanks!
Loganix77
Participant
1351 Points
412 Posts
Re: Unable to read link from gridview?
Apr 27, 2012 09:03 PM|LINK
Some more code would really be helpful in trying to help you resolve this. I assume you have a hyperlink you're tyring to populate based on text in a gridview?
Where is this "select" that is being clicked? Is it an item template field in the gridview? Basically we need to know how your gridview is being populated. What exactly are you clicking on to populate your other field? Where are these fields you're trying to populate?
MKozlowski
Member
500 Points
573 Posts
Re: Unable to read link from gridview?
Apr 30, 2012 12:35 PM|LINK
Below is my code, any ideas?
Private Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand If e.CommandName = "Select" Then Dim index As Integer = Convert.ToString(e.CommandArgument) Dim row As GridViewRow = GridView1.Rows(index) Me.NameTextBox.Text = Server.HtmlDecode(row.Cells(1).Text) Me.Product1DropDown.SelectedValue = Server.HtmlDecode(row.Cells(2).Text) Me.Product2DropDown.SelectedValue = Server.HtmlDecode(row.Cells(3).Text) Me.JobNumberTextBox.Text = Server.HtmlDecode(row.Cells(4).Text) Me.AdTextBox.Text = Server.HtmlDecode(row.Cells(5).Text) Me.MediaTypeDropDown.SelectedValue = Server.HtmlDecode(row.Cells(6).Text) Me.AuthorDropDown.SelectedValue = Server.HtmlDecode(row.Cells(7).Text) Me.PublicationDropDown.SelectedValue = Server.HtmlDecode(row.Cells(8).Text) Me.RunDatesTextBox.Text = Server.HtmlDecode(row.Cells(9).Text) Me.CostTextBox.Text = Server.HtmlDecode(row.Cells(10).Text) Me.CostCenterTextBox.Text = Server.HtmlDecode(row.Cells(11).Text) Dim myHyperLink As String = GridView1.DataKeys(13)("PDFFile") Me.HyperLink1.Text = myHyperLink Me.HyperLink1.NavigateUrl = CType((row.Cells(13).Controls(13)), HyperLink).Text Me.CommentsTextBox.Text = Server.HtmlDecode(row.Cells(12).Text) Me.TextBox1.Text = Server.HtmlDecode(row.Cells(14).Text) Label14.Text = "Update" End If End Submarch11
Contributor
3001 Points
1361 Posts
Re: Unable to read link from gridview?
Apr 30, 2012 12:38 PM|LINK
dr is grdiview row.
Define it like this.
For Each dr In GridView1.Rows
' do something for each row in the gridview.
Next
MKozlowski
Member
500 Points
573 Posts
Re: Unable to read link from gridview?
Apr 30, 2012 12:44 PM|LINK
I tried the code below, but now I am getting the error Specified argument was out of the range of valid values.
Parameter name: index?
Private Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand For Each dr In GridView1.Rows If e.CommandName = "Select" Then Dim index As Integer = Convert.ToString(e.CommandArgument) Dim row As GridViewRow = GridView1.Rows(index) Me.NameTextBox.Text = Server.HtmlDecode(row.Cells(1).Text) Me.Product1DropDown.SelectedValue = Server.HtmlDecode(row.Cells(2).Text) Me.Product2DropDown.SelectedValue = Server.HtmlDecode(row.Cells(3).Text) Me.JobNumberTextBox.Text = Server.HtmlDecode(row.Cells(4).Text) Me.AdTextBox.Text = Server.HtmlDecode(row.Cells(5).Text) Me.MediaTypeDropDown.SelectedValue = Server.HtmlDecode(row.Cells(6).Text) Me.AuthorDropDown.SelectedValue = Server.HtmlDecode(row.Cells(7).Text) Me.PublicationDropDown.SelectedValue = Server.HtmlDecode(row.Cells(8).Text) Me.RunDatesTextBox.Text = Server.HtmlDecode(row.Cells(9).Text) Me.CostTextBox.Text = Server.HtmlDecode(row.Cells(10).Text) Me.CostCenterTextBox.Text = Server.HtmlDecode(row.Cells(11).Text) Dim myHyperLink As String = GridView1.DataKeys(dr.RowIndex)("PDFFile") ' do something for each row in the gridview. Me.HyperLink1.Text = myHyperLink Me.HyperLink1.NavigateUrl = CType((row.Cells(13).Controls(13)), HyperLink).Text Me.CommentsTextBox.Text = Server.HtmlDecode(row.Cells(12).Text) Me.TextBox1.Text = Server.HtmlDecode(row.Cells(14).Text) Label14.Text = "Update" End If Next End Submarch11
Contributor
3001 Points
1361 Posts
Re: Unable to read link from gridview?
Apr 30, 2012 01:04 PM|LINK
I think you are doing this on the wrong event. Try it in the row Select event...
Also you do not need to use the For Each stmt if you are already on the Row index selected. It is for when you do not know which row was selected or you want to search each row and perform some action each row.
Protected Sub GridView1_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView1.SelectedIndexChanging
' add you code in here. This is when the "Select" is chosen.
End Sub
MKozlowski
Member
500 Points
573 Posts
Re: Unable to read link from gridview?
Apr 30, 2012 01:34 PM|LINK
Everything is working fine just not the hyperlink so do I still need to change the event?
march11
Contributor
3001 Points
1361 Posts
Re: Unable to read link from gridview?
Apr 30, 2012 02:44 PM|LINK
Well, have you updated the DataKeyNames property of the Gridview?
Something like this....
<asp:GridView ID="GridView4" DataKeyNames="myHyperlink" runat="server" Width="648px" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" EmptyDataText="No Data to Display." AutoGenerateColumns="False">