Hyperlink in Gridview binding to wrong row by 1
I've searched around and haven't found a solution yet.
I have a Gridview populated by a Stored Procedure that is called by a DropdownList. The query works fine and gives me a table with values. You'll see that I programmed text in the first column to be converted to Hyperlinks.
Everything works fine with the exception that the first row (that's not the Header row) doesn't have a link. In fact, the hyperlink address is applied to the next row instead. And so it bumps the rest of the links down one row.
I did find that when debugging, it seems that the cell in what's supposed to be the first hyperlink comes up with a null ("") value. When I traverse in the IntelliSense, I do find that the row does show the correct properties (there is a tex开发者_JAVA技巧t value in the DataItem > Row > ItemArray).
Client Side
<asp:TableCell>
<asp:Label ID="ddlLabel" runat="server" Text="Choose a Group Folder: " />
<asp:DropDownList ID="ddlFolders" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlFolders_SelectedIndexChanged">
</asp:DropDownList>
</asp:TableCell>
<asp:TableCell ColumnSpan="2">
<asp:GridView ID="gvReportList" runat="server" AutoGenerateColumns="false"
CellPadding="5" OnRowDataBound="gvReportList_RowDataBound" Width="98%">
<Columns>
<asp:HyperLinkField HeaderText="Name" DataTextField="Name" Target="_blank" />
<asp:BoundField HeaderText="Description" DataField="Description" />
</Columns>
</asp:GridView>
</asp:TableCell>
Server Side C#
// hyperlink binding by row for first column in gridview
protected void gvReportList_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Changes text in the first column into HyperLinks
HyperLinkField nameLink = gvReportList.Columns[0] as HyperLinkField;
string linkPath = "http://inserted-address-here";
if (e.Row.RowType == DataControlRowType.DataRow)
{
//applies a unique suffix to the address depending on the link name
HyperLink nameHl = (HyperLink)e.Row.Cells[0].Controls[0];
string nameText = nameHl.Text;
string linkSuffix = nameText.Replace(" ", "+");
nameLink.NavigateUrl = linkPath + linkSuffix;
}
}
I can only assume it has something to do with the order in which I'm binding the hyperlinks to the gridview. OR that it has something to do with the first row coming up with a null value even though there's data there.
string linkSuffix = Datainder.Eval(e.DataItem, "Name").ToString().Replace(" ", "+");
nameLink.NavigateUrl = linkPath + linkSuffix;
精彩评论