复制代码 代码如下:
<%@ 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>

标签:
viewstate,datatable,录入数据

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

评论“viewstate和datatable动态录入数据示例”

暂无“viewstate和datatable动态录入数据示例”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。