程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> GridView控件中加入動態生成的控件

GridView控件中加入動態生成的控件

編輯:.NET實例教程
前台代碼如下:
<ASP:GridVIEw ID="testDatagrid" runat="server" AutoGenerateColumns="False" OnRowDataBound="testDatagrid_RowDataBound" Width="294px" Height="134px">
            <Columns>
                <ASP:CheckBoxFIEld HeaderText="可選" />
   
                <ASP:BoundField DataFIEld="ToolName" HeaderText="text" />
                <asp:TemplateFIEld HeaderText="數量"></ASP:TemplateFIEld>
            </Columns>
        </ASP:GridVIEw>
        <ASP:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="數據的更新" />
 
 
後台代碼實現如下:
  #region GridVIEw綁定動態生成的控件
    protected void testDatagrid_RowDataBound(object sender, GridVIEwRowEventArgs e)
    {

        if (e.Row.RowIndex > -1)
        {

            CheckBox check = new CheckBox();
            check.ID = e.Row.RowIndex.ToString();
            check.AutoPostBack = true;
            check.CheckedChanged += new EventHandler(check_CheckedChanged);

            e.Row.Cells[0].Controls.Add(check);

            TextBox text = new TextBox();
            text.Width = 15;
            text.ID = "txt" + e.Row.RowIndex.ToString();
            text.Enabled = false;

            e.Row.Cells[2].Controls.Add(text);
            HiddenField hidden = new HiddenFIEld();
            hidden.Value = da.Tables[0].Rows[e.Row.RowIndex][0].ToString();
            hidden.ID = "Hidd" + e.Row.RowIndex.ToString();
            e.Row.Cells[2].Controls.Add(hidden);
        }

    }
    #endregion
    #region 處理checkbox所觸發的事件
    void check_CheckedChanged(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(((CheckBox)sender).ID);
        TextBox box = this.testDatagrid.Rows[i].Cells[2].FindControl("txt" + i.ToString()) as TextBox;
        box.Enabled = true;
    }
    #endregion
    #region 將客戶所選的數據插入數據庫
    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < this.testDatagrid.Rows.Count; i++)
        {
            CheckBox chB = this.testDatagrid.Rows[i].Cells[0].FindControl(i.ToString()) as CheckBox;
            TextBox txB = this.testDatagrid.Rows[i].Cells[2].FindControl("txt" + i.ToString()) as TextBox;
            HiddenField hid = this.testDatagrid.Rows[i].Cells[2].FindControl("Hidd" +i.ToString()) as HiddenFIEld;
            int ii = -1;
            if (chB.Checked)
            {
                if (txB.Text != null)
                {
                    try
                    {
                            //引用 DataBase.dll 
                    &nbsp;   ii = new ZHXKDatabase(connStr).RunSQLCommandReturnInt("insert into Relation (ToolsID,TagID,[Desc]) values (" + hid.Value + "," + 12 + "," + int.Parse(txB.Text.Trim()) + ")");
                    }
                    catch (Exception exc)
                    {
                        Response.Write(exc.Message);

                    }
                }
                if (ii == 1)
                {
&nbsp;                   Response.Write("OK!");
                }
            }
        }
    }
    #endregion 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved