Hyperlink in Gridview binding to wrong row by 1

被刻印的时光 ゝ 提交于 2019-12-13 03:16:53

问题


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 text 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.


回答1:


string linkSuffix = Datainder.Eval(e.DataItem, "Name").ToString().Replace(" ", "+");
nameLink.NavigateUrl = linkPath + linkSuffix; 


来源:https://stackoverflow.com/questions/5954361/hyperlink-in-gridview-binding-to-wrong-row-by-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!