viewstate和datatable动态录入数据示例_实用技巧

复制代码 代码如下:

<%@ Page Language="C#" EnableViewState="true" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  private DataTable stoveTable = null;
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      //创建 EmptyDataTemplate
      this.GridView_list.DataBind();
    }
  }

  protected void GridView_list_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      String usage = DataBinder.Eval(e.Row.DataItem, "usage").ToString();
      String steelKind = DataBinder.Eval(e.Row.DataItem, "steelKind").ToString();
      String castingTon = DataBinder.Eval(e.Row.DataItem, "castingTon").ToString();
      DropDownList x1 = e.Row.FindControl("x1") as DropDownList;
      DropDownList x2 = e.Row.FindControl("x2") as DropDownList;
      TextBox x3 = e.Row.FindControl("x3") as TextBox;
      x3.Text = castingTon;
      ListItem xx1 = x1.Items.FindByValue(usage);
      if (xx1 != null) xx1.Selected = true;
      ListItem xx2 = x2.Items.FindByValue(steelKind);
      if (xx2 != null) xx2.Selected = true;
    }
  }

  protected void LinkButton1_Click(object sender, EventArgs e)
  {
    DropDownList x1, x2;
    TextBox x3;
    if (GridView_list.Rows.Count == 0)
    {
      x1 = GridView_list.Controls[0].Controls[0].FindControl("x1") as DropDownList;
      x2 = GridView_list.Controls[0].Controls[0].FindControl("x2") as DropDownList;
      x3 = GridView_list.Controls[0].Controls[0].FindControl("x3") as TextBox;
    }
    else
    {
      GridViewRow r = GridView_list.FooterRow;
      x1 = r.FindControl("x1") as DropDownList;
      x2 = r.FindControl("x2") as DropDownList;
      x3 = r.FindControl("x3") as TextBox;
    }
    if (ViewState["dt"] == null)
    {
      stoveTable = new DataTable();
      stoveTable.Columns.Add("usage", typeof(String));
      stoveTable.Columns.Add("steelKind", typeof(String));
      stoveTable.Columns.Add("castingTon", typeof(String));
    }
    else
    {
      stoveTable = (DataTable)ViewState["dt"];
    }
    DataRow newRow = stoveTable.NewRow();
    newRow["usage"] = x1.SelectedValue;
    newRow["steelKind"] = x2.SelectedValue;
    newRow["castingTon"] = x3.Text;
    stoveTable.Rows.Add(newRow);

    ViewState["dt"] = stoveTable;
    this.GridView_list.DataSource = stoveTable;
    this.GridView_list.DataBind();

  }
  protected void LinkButton2_Click(object sender, EventArgs e)
  {
    if (ViewState["dt"] == null)
    {
      return;
    }
    stoveTable = (DataTable)ViewState["dt"];
    if (stoveTable.Rows.Count < 1) return;
    stoveTable.Rows.RemoveAt(stoveTable.Rows.Count - 1);
    ViewState["dt"] = stoveTable;
    this.GridView_list.DataSource = stoveTable;
    this.GridView_list.DataBind();
  }

  protected void x1_SelectedIndexChanged(object sender, EventArgs e)
  {
    DropDownList x1 = sender as DropDownList;
    GridViewRow r = x1.Parent.Parent as GridViewRow;
    if (ViewState["dt"] == null)
    {
      Response.Write("Error");
      return;
    }
    stoveTable = (DataTable)ViewState["dt"];
    stoveTable.Rows[r.RowIndex]["usage"] = x1.SelectedValue;
    ViewState["dt"] = stoveTable;
    this.GridView_list.DataSource = stoveTable;
    this.GridView_list.DataBind();
  }

  protected void x2_SelectedIndexChanged(object sender, EventArgs e)
  {
    DropDownList x2 = sender as DropDownList;
    GridViewRow r = x2.Parent.Parent as GridViewRow;
    if (ViewState["dt"] == null)
    {
      Response.Write("Error");
      return;
    }
    stoveTable = (DataTable)ViewState["dt"];
    stoveTable.Rows[r.RowIndex]["steelKind"] = x2.SelectedValue;
    ViewState["dt"] = stoveTable;
    this.GridView_list.DataSource = stoveTable;
    this.GridView_list.DataBind();
  }

  protected void x3_TextChanged(object sender, EventArgs e)
  {

    TextBox x3 = sender as TextBox;
    GridViewRow r = x3.Parent.Parent as GridViewRow;
    if (ViewState["dt"] == null)
    {
      Response.Write("Error");
      return;
    }
    stoveTable = (DataTable)ViewState["dt"];
    stoveTable.Rows[r.RowIndex]["castingTon"] = x3.Text;
    ViewState["dt"] = stoveTable;
    this.GridView_list.DataSource = stoveTable;
    this.GridView_list.DataBind();
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <asp:GridView ID="GridView_list" runat="server" ShowFooter="true" AutoGenerateColumns="false"
    OnRowDataBound="GridView_list_RowDataBound">
    <EmptyDataTemplate>
      <table style="border-collapse: collapse; width: 100%" cellspacing="0" rules="all"
        border="1">
        <tr>
          <th scope="col">
            选择1
          </th>
          <th scope="col">
            选择2
          </th>
          <th scope="col">
            输入文字
          </th>
        </tr>
        <tr><td>
          <asp:DropDownList ID="x1" runat="server">
            <asp:ListItem>L0</asp:ListItem>
            <asp:ListItem>L1</asp:ListItem>
            <asp:ListItem>L2</asp:ListItem>
            <asp:ListItem>L3</asp:ListItem>
          </asp:DropDownList>
        </td><td>
          <asp:DropDownList ID="x2" runat="server">
            <asp:ListItem>10#</asp:ListItem>
            <asp:ListItem>20#</asp:ListItem>
            <asp:ListItem>30#</asp:ListItem>
            <asp:ListItem>40#</asp:ListItem>
          </asp:DropDownList>
        </td><td>
          <asp:TextBox ID="x3" runat="server"></asp:TextBox>
        </td></tr>
      </table>
    </EmptyDataTemplate>
    <Columns>
      <asp:TemplateField HeaderText="选择1">
        <ItemTemplate>
          <asp:DropDownList ID="x1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="x1_SelectedIndexChanged">
            <asp:ListItem>L0</asp:ListItem>
            <asp:ListItem>L1</asp:ListItem>
            <asp:ListItem>L2</asp:ListItem>
            <asp:ListItem>L3</asp:ListItem>
          </asp:DropDownList>
        </ItemTemplate>
        <FooterTemplate>
          <asp:DropDownList ID="x1" runat="server">
            <asp:ListItem>L0</asp:ListItem>
            <asp:ListItem>L1</asp:ListItem>
            <asp:ListItem>L2</asp:ListItem>
            <asp:ListItem>L3</asp:ListItem>
          </asp:DropDownList>
        </FooterTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="选择2">
        <ItemTemplate>
          <asp:DropDownList ID="x2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="x2_SelectedIndexChanged">
            <asp:ListItem>10#</asp:ListItem>
            <asp:ListItem>20#</asp:ListItem>
            <asp:ListItem>30#</asp:ListItem>
            <asp:ListItem>40#</asp:ListItem>
          </asp:DropDownList>
        </ItemTemplate>
        <FooterTemplate>
          <asp:DropDownList ID="x2" runat="server">
            <asp:ListItem>10#</asp:ListItem>
            <asp:ListItem>20#</asp:ListItem>
            <asp:ListItem>30#</asp:ListItem>
            <asp:ListItem>40#</asp:ListItem>
          </asp:DropDownList>
        </FooterTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="输入文字">
        <ItemTemplate>
          <asp:TextBox ID="x3" runat="server" AutoPostBack="true" OnTextChanged="x3_TextChanged"></asp:TextBox>
        </ItemTemplate>
        <FooterTemplate>
          <asp:TextBox ID="x3" runat="server"></asp:TextBox>
        </FooterTemplate>
      </asp:TemplateField>
    </Columns>
  </asp:GridView>
  <asp:LinkButton ID="LinkButton1" runat="server" Text="添加内容" OnClick="LinkButton1_Click"></asp:LinkButton>
  <asp:LinkButton ID="LinkButton2" runat="server" Text="删除内容" OnClick="LinkButton2_Click"></asp:LinkButton>
  </form>
</body>
</html>

时间: 2024-09-20 05:34:34

viewstate和datatable动态录入数据示例_实用技巧的相关文章

viewstate和datatable动态录入数据示例

 这篇文章主要介绍了viewstate和datatable动态录入数据示例,需要的朋友可以参考下    代码如下: <%@ Page Language="C#" EnableViewState="true" %> <%@ Import Namespace="System.Data" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E

.net下实现Word动态填加数据打印_实用技巧

今天研究了一下.net下实现Word动态填加数据打印的做法,觉得颇有收获~       以前做过Excel相关的东西,所以对OFFICE的COM有一些了解,很顺利的找到了需要引用的COM和其帮助文档~具体做法是在引用里添加 COM --------Microsoft word 11.0 object library, 然后引入命名空间: 复制代码 代码如下: using WordApplication = Microsoft.Office.Interop.Word.Application;  u

ASP.NET中上传并读取Excel文件数据示例_实用技巧

在CSDN中,经常有人问如何打开Excel数据库文件.本文通过一个简单的例子,实现读取Excel数据文件. 首先,创建一个Web应用程序项目,在Web页中添加一个DataGrid控件.一个文件控件和一个按钮控件. 复制代码 代码如下: <INPUT id="File1" type="file" name="File1" runat="server"> <asp:Button id="Button1&

litjson读取数据示例_实用技巧

1.下载并应用LitJson,DLL文件 2.建两个类: 复制代码 代码如下:    public class JsonData    {        public string result { get; set; }        public List<GameData> info { get; set; }    }       public GameData()        { }        #region Model        private int _id;     

DataSet 添加数据集、行、列、主键和外键等操作示例_实用技巧

前台代码:html <%@ Page language="c#" Codebehind="CodeUse.aspx.cs" AutoEventWireup="false" Inherits="DsAndXML.CodeUse" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML>  

asp.net 动态表单之数据分页_实用技巧

但是问题来了,不同科系的同学的科目是不一样的,那么我们在数据库设计的时候通常是把学生.某科成绩作为一条记录,那么这个时候我们就需要做一个行转列的逻辑处理了. 解决方法: 使用GridView来生成表单,这个实现起来会比较麻烦,如果要在列表里面显示链接就更不可能了: 生成html再输出到页面中,这个实现起来比较灵活.方便: 基本功能点: 动态生成表头: 数据进行分页: 查询数据: 对每个成绩进行超链接,查看明细:  页面代码 复制代码 代码如下: <div id="dataDiv1"

利用ASP.NET MVC和Bootstrap快速搭建个人博客之后台dataTable数据列表_实用技巧

jQuery dataTables 插件是一个优秀的表格插件,是后台工程师的福音!它提供了针对数据表格的排序.浏览器分页.服务器分页.查询.格式化等功能.dataTables 官网也提供了大量的演示和详细的文档进行说明,为了方便使用,这里进行详细说明. 去官网:https://www.datatables.net/ 下载最新版本是v1.10.12. 在页面引入: <link rel="stylesheet" href="~/Content_Admin/css/boots

asp.net DataTable相关操作集锦(筛选,取前N条数据,去重复行,获取指定列数据等)_实用技巧

本文实例总结了asp.net DataTable相关操作.分享给大家供大家参考,具体如下: #region DataTable筛选,排序返回符合条件行组成的新DataTable或直接用DefaultView按条件返回 /// <summary> /// DataTable筛选,排序返回符合条件行组成的新DataTable或直接用DefaultView按条件返回 /// eg:SortExprDataTable(dt,"Sex='男'","Time Desc&quo

DropDownList绑定数据表实现两级联动示例_实用技巧

场景一:平时我们在DropDownList控件下添加下拉选项时,都会使用它的Item.Add方法,直接在代码下添加.如果我们想添加或修改下拉选项,则必须去修改源代码.如果几个DropDownList控件的下拉选项相同,我们则需要重复添加好多次,后期的维护工作很不方便. 场景二:我们在12306网站买票时,肯定遇到过这么一种情景:我们需要先选定目的地的省份,选完省份后在城市选框中会自动加载该省份的城市,实现两级联动. 针对以上两个场景,我们可以用DropDownList直接绑定数据表,根据选择的省