【问题标题】:Asp.net Grid View BoundFieldAsp.net Grid View BoundField
【发布时间】:2015-03-04 04:48:15
【问题描述】:

我正在使用 Asp.net Grid 视图绑定字段,在绑定字段中我使用锚标记在新选项卡中打开值。我的锚标记工作完美,因为它从数据库中获取值,但问题是我想显示数据库中的值到锚标记,比如我的 db 值 这是我的代码

<asp:BoundField DataField="uniId" ControlStyle-CssClass="bg-darkGreen"  HeaderText="ID"     ReadOnly="True" SortExpression="uniId" HtmlEncode="false" DataFormatString="<a target='_blank'     href='Details.aspx?uniId={0}'>uniId</a>" >
</asp:BoundField>

它在所有行中显示 uniId 而不是它们的值。

我也试过 但问题依旧。

【问题讨论】:

标签: c# asp.net gridview


【解决方案1】:

使用TemplateField 代替BoundField

<asp:TemplateField  HeaderText="ID">
    <ItemTemplate>
        <a target='_blank' href='Details.aspx?uniId=<%#Eval("uniId")%>'><%#Eval("uniId")%></a>
    </ItemTemplate>
</asp:TemplateField>

【讨论】:

    【解决方案2】:

    关于安全说明:您绝不应该通过将主键放置在 URL 中或嵌入到 griview 中作为可以通过任何浏览器的“查看页面源”选项查看的值来公开查看主键。

    你至少需要...

    1. 处理 Gridview 的 SelectedIndexChanged 事件
    2. 根据行选择从gridview DataKeys中获取uniID
    3. 使用 Details.aspx 将使用的 Key 值设置会话变量或类似变量

    【讨论】:

      【解决方案3】:

      使用TemplateField 而不是BoundField

              <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                  <ItemTemplate>
                      <HyperLink ID="RedirectBtn" runat="server"
                          OnClick="RedirectBtn_Click" />
      
                  </ItemTemplate>
                  <ItemStyle HorizontalAlign="Center"></ItemStyle>
              </asp:TemplateField>
      

      之后,您可以在网格的OnRowDataBound 事件中添加任何您想要的内容。如果您的 RowDataBound 事件被称为 Grid_RowDataBound

          protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
          {
              if (e.Row.DataItem == null)
                  return;
      
              DataRowView row = e.Row.DataItem as DataRowView;
      
              HyperLinkbtn = e.Row.FindControl("RedirectBtn") as HyperLink;
              b.NavigateUrl = "some text" + row["ColumnName"] + "other text";
              //if you want to open new tab
              b.Target="_blank";
      
          }
      

      您正在像这样将事件添加到网格中:

      OnRowDataBound="Grid_RowDataBound"
      

      【讨论】:

        猜你喜欢
        • 2010-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-23
        • 2011-03-27
        • 2012-09-16
        • 1970-01-01
        相关资源
        最近更新 更多