加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 向GridView Row添加ID

发布时间:2020-12-16 04:02:01 所属栏目:asp.Net 来源:网络整理
导读:如何向GridView行添加ID(应该呈现ID)? 我正在使用.NET(C#).我有GridView控件. 我有一些操作表行的javascript函数,但是必须为这些行设置ID: table tr id=1 ... tr id=2 ... //id should come from database.. 我的GridView源自DataBase中的Data.重要的是不
如何向GridView行添加ID(应该呈现ID)?

我正在使用.NET(C#).我有GridView控件.

我有一些操作表行的javascript函数,但是必须为这些行设置ID:

<table>
    <tr id=1> ...  
    <tr id=2> ...  //id should come from database
..

我的GridView源自DataBase中的Data.重要的是不要有FAKE ROW IDS,而是真正来自DB的行ID(有一些ajax javascript函数可以根据这些ID和用户对表的操作来更新DB).

我的部分GridView如下:

<asp:GridView ID="grdNews" runat="server" BorderStyle="None" RowStyle-BorderStyle="None"
                GridLines="None" CssClass="table" Style="white-space: nowrap" AutoGenerateColumns="False"
                DataKeyNames="ID" AllowSorting="True" AllowPaging="true" OnSorting="grdNews_Sorting" OnRowDataBound="grdNews_RowDataBound">
                <RowStyle BorderStyle="None" />
                <HeaderStyle CssClass="nodrag" />
                <Columns>
                ....

我尝试过以下方法:

protected void grdNews_RowDataBound(object sender,GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
         e.Row.ID = grdNews.DataKeys[e.Row.RowIndex].Value.ToString();
     }
}

这为e.Row.ID提供了正确的值,但这不会呈现此ID.

那么,如何在GridView中从DataBase for Rows呈现ID?

解决方法

试试以下….
protected void grdNews_RowDataBound(object sender,GridViewRowEventArgs e)
 {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
        GridViewRow row = e.Row;
        row.Attributes["id"] =grdNews.DataKeys[e.Row.RowIndex].Value.ToString();


      }
 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读