主頁 > 知識庫 > viewstate和datatable動態(tài)錄入數(shù)據(jù)示例

viewstate和datatable動態(tài)錄入數(shù)據(jù)示例

熱門標(biāo)簽:怎樣給陜西地圖標(biāo)注顏色 福州人工智能電銷機(jī)器人加盟 電銷機(jī)器人 數(shù)據(jù) 地圖標(biāo)注多少錢一張 云狐人工智能電話機(jī)器人 宿遷智能外呼系統(tǒng)排名 400電話辦理信任翰諾科技 廣州銷售外呼系統(tǒng)定制 ai電銷機(jī)器人對貸款有幫助嗎

復(fù)制代碼 代碼如下:

%@ 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)
    {
      //創(chuàng)建 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="添加內(nèi)容" OnClick="LinkButton1_Click">/asp:LinkButton>
  asp:LinkButton ID="LinkButton2" runat="server" Text="刪除內(nèi)容" OnClick="LinkButton2_Click">/asp:LinkButton>
  /form>
/body>
/html>

您可能感興趣的文章:
  • asp.net 生成靜態(tài)時的過濾viewstate的實現(xiàn)方法
  • asp.net 去除viewstate
  • asp.net 禁用viewstate在web.config里
  • asp.net viewstate 回發(fā)機(jī)制
  • asp.net生成靜態(tài)后冗余代碼,去掉viewstate生成的代碼
  • 使用正則Regex來移除網(wǎng)頁的EnableViewState實現(xiàn)思路及代碼
  • 狀態(tài)保存機(jī)制之ViewState概述及應(yīng)用
  • 禁止ViewState的3種解決方法
  • asp.net中ViewState的用法詳解
  • ASP.NET中控件的EnableViewState屬性及徹底禁用

標(biāo)簽:綿陽 新疆 宜春 焦作 大興安嶺 曲靖 黃南 延安

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《viewstate和datatable動態(tài)錄入數(shù)據(jù)示例》,本文關(guān)鍵詞  viewstate,和,datatable,動態(tài),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《viewstate和datatable動態(tài)錄入數(shù)據(jù)示例》相關(guān)的同類信息!
  • 本頁收集關(guān)于viewstate和datatable動態(tài)錄入數(shù)據(jù)示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章