【问题标题】:How to populate dataset value in table row?如何在表格行中填充数据集值?
【发布时间】:2014-12-11 08:56:30
【问题描述】:

我有一个Bind_Project_details()的函数如下:

string[] Parameters = new string[3] { "@User_ID", "@PAGE_NO", "@PAGE_SIZE" };
        string[] DbTypes = new string[3] { "int","int","int" };
        string[] ParameterTypes = new string[3] { "input", "input", "input" };
        object[] values = new object[3] { iUserID, pageno, pagesize };
        string[] Lengths = new string[3] { "5","5","5" };

        P2ERefrence.Service du = new P2ERefrence.Service();

        ds = du.returnDataSet("proc", "BIND_PROJECT_GRID", Parameters, DbTypes, ParameterTypes, values, Lengths);

在数据集项目中,详细信息来自数据库。我想将这些数据集值绑定在表体中,如下所示:

<table id="table1" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th><input type="checkbox" id="" name=""></th>
            <th>User</th>
            <th>Role</th>
            <th>Project Name</th>
        </tr>
    </thead>

    <tbody>
    .........
    .........
    .........
    </tbody>

</table>

【问题讨论】:

  • 所有你需要做的就是打开一个网络浏览器,去谷歌然后搜索以下术语......“asp.net gridview dataset tutorial”,“asp.net databound controls tutorials”.. .您将为每个受众和每种语言获得数百万个搜索结果...试一试
  • 我已将其绑定在 aspx 网格中,但我想将其绑定在表格主体下,客户端不关注 aspx 网格我有固定的表格头部部分并希望在表格行中绑定数据..跨度>

标签: c# asp.net dataset


【解决方案1】:

您想将 DataTable 绑定到 htmlTable。为此,

aspx.cs

dt.Columns.AddRange(new DataColumn[] { new DataColumn("User", Type.GetType("System.String")), new DataColumn("Role", Type.GetType("System.String")) });
        dt.Rows.Add(new object[] { "raju", "1" });
        dt.Rows.Add(new object[] { "mahesh", "1" });

aspx

 <table>
            <tbody id="tbody" runat="server">
                <%foreach (System.Data.DataRow item in dt.Rows)
                  { %>
                <tr>
                    <td><%: item[0].ToString() %></td>
                    <td><%: item[1].ToString() %></td>
                </tr>
                <% } %>
            </tbody>
        </table>

【讨论】:

    【解决方案2】:

    根据您想要自定义数据绑定服务器控件的 html 输出的 cmets。如果是这种情况,您可以使用Repeater 服务器控件或ListView。我会解释这两个...

    使用中继器控件

    Repeater 提供了几个数据模板,您可以使用它们来设置输出样式。您感兴趣的是:

    • 标头模板
    • 项目模板
    • AlternatingItemTemplate(可选)
    • 页脚模板

    您可以一起绑定数据并自定义 html 输出。比如……

    <asp:Repeater ID="rp" runat="server">
       <HeaderTemplate>
           <table id="table1" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th></th>
                    <th>User</th>
                    <th>Role</th>
                    <th>Project Name</th>
                </tr>
            </thead>
               <tbody>
       </HeaderTemplate>
       <ItemTemplate>
           <tr>
               <td><input type="checkbox" id="" name=""></td>
               <td><%# Eval("User") %></td>
               <td><%# Eval("Role") %></td>
               <td><%# Eval("ProjectName") %></td>
           </tr>
       </ItemTemplate>
       <FooterTemplate>
           </tbody>
         </table>
       </FooterTemplate>
    </asp:Repeater>
    

    ItemTemplate 是您定义数据绑定表达式的地方...确保您根据自己的数据使用正确的数据绑定表达式

    使用 ListView 控件

    ListView 控件非常相似,并且与Repeater 控件的工作方式几乎相同。但是,ListView 在功能方面更强大。因为它有很多模板可供您使用,并且只专注于您需要的模板:

    • 项目模板
    • AlternatingItemTemplate(可选)
    • 布局模板

    在LayoutTemplate 中,您可以将任何您想要的从html 元素放入Web 控件,但是,它至少需要一个具有指定runat=server 属性和唯一ID 的元素。它使用它作为占位符,以便对数据绑定项进行数据绑定(添加)。然后你需要将ItemPlaceholderID 属性设置为这个占位符项的id。见下文...

    <asp:ListView ID="lst" runat="server" ItemPlaceholderID="tblBody">
       <LayoutTemplate>
           <table id="table1" class="display"  cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th></th>
                    <th>GivenName</th>
                    <th>DOB</th>
                    <th>Phone</th>
                </tr>
            </thead>
               <tbody id="tblBody" runat="server">
               </tbody>
            </table>
       </LayoutTemplate>
       <ItemTemplate>
           <tr>
               <td><input type="checkbox" id="" name=""></td>
               <td><%# Eval("GivenName") %></td>
               <td><%# Eval("DOB") %></td>
               <td><%# Eval("PhoneMobile") %></td>
           </tr>
       </ItemTemplate>
     </asp:ListView>
    

    Repeater 和ListView 都可以通过使用它们的DataSource 属性或将DataSourceID 属性设置为有效的DataSource 控件来进行数据绑定

    lst.DataSource = dataSet1.Tables[0];
    lst.DataBind();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 2017-01-25
      相关资源
      最近更新 更多