【问题标题】:Hyperlink in Gridview binding to wrong row by 1Gridview中的超链接绑定到错误的行1
【发布时间】:2011-05-10 18:03:26
【问题描述】:

我四处寻找,还没有找到解决方案。

我有一个由 DropdownList 调用的存储过程填充的 Gridview。该查询工作正常,并为我提供了一个包含值的表。您会看到我在第一列中编写了要转换为超链接的文本。

除了第一行(不是标题行)没有链接外,一切正常。事实上,超链接地址被应用到下一行。因此,它会将其余的链接撞到一排。

我确实发现在调试时,似乎应该是第一个超链接的单元格出现了一个空 ("") 值。当我在 IntelliSense 中遍历时,我确实发现该行确实显示了正确的属性(DataItem > Row > ItemArray 中有一个文本值)。

客户端

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

服务器端 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;
    }
}

我只能假设它与我将超链接绑定到网格视图的顺序有关。或者它与第一行出现空值有关,即使那里有数据。

【问题讨论】:

  • 难道 nameLink 是指整个列而不是您所在行的特定列?也就是说,nameLink.NavigateUrl不应该是nameHl.NavigateUrl吗?
  • 改变了它,就成功了。我很高兴就这么简单!非常感谢。
  • 很高兴为您提供帮助。看起来您还试图通过用 + 替换空格来抑制换行,对吗?最好使用 NoWrap 设置列的样式。
  • 那部分不是我做的,所以我必须遵循我提供的地址方案。但感谢您的额外帮助。 :)

标签: c# asp.net gridview hyperlink


【解决方案1】:
string linkSuffix = Datainder.Eval(e.DataItem, "Name").ToString().Replace(" ", "+");
nameLink.NavigateUrl = linkPath + linkSuffix; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多