添加ID来GridView的行 [英] Add ID to GridView Row

查看:76
本文介绍了添加ID来GridView的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何ID添加到GridView的行(ID应该呈现)?

How to add ID to GridView rows (IDs should be rendered)?

我使用的.NET(C#)。我有GridView控件。

I am using .NET (C#). I have GridView control.

我具有被操纵表行一些JavaScript功能,但有必要具有用于这些行编号:

I have some javascript functions that are manipulating table rows, but it is necessary to have IDs for those rows:

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

我的GridView控件从数据从数据库genereted。不要有FAKE行ID,但真的行ID是很重要的,从DB(也有一些AJAX JavaScript函数根据这些ID和用户操作与表更新DB)。

My GridView is genereted from Data from DataBase. It is important not to have FAKE ROW IDS, but really row ids from DB (there are some ajax javascript function that updates DB based on those IDs and user manipulations with table).

我的GridView的一部分是以下内容:

Part of my GridView is the following:

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

我曾尝试以下内容:

I have tried the following:

 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。

This gives e.Row.ID the correct value, but this doesn't render this ID.

那么,如何使从数据库ID的行中的GridView?

So, how to render IDs from DataBase for Rows in GridView?

推荐答案

尝试以下....

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();


      }
 } 

这篇关于添加ID来GridView的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆