如何在Gridview的itemTemplate中获取动态创建的文本框的值 [英] how to get value of dynamic created textbox in itemtemplate of Gridview

查看:70
本文介绍了如何在Gridview的itemTemplate中获取动态创建的文本框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi ..

我有一个gridview.我在gridview中创建了两个项目TemplateField.

使用文本框的TemplateField在gridview中动态添加其他两列或更多列.

hi..

i have a gridview. i created two item TemplateField in gridview.

other two or more columns are added in gridview Dynamically using TemplateField of textbox.

<asp:GridView ID="grdProductFields" runat="server" AllowPaging="false"

            AutoGenerateColumns="false" Width="100%" CssClass="mGrid" PageSize="50"

            AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr"

            onrowdatabound="grdProductFields_RowDataBound"

            onrowcommand="grdProductFields_RowCommand" >
            <PagerSettings  Mode="NumericFirstLast" PageButtonCount="5"  FirstPageText="First" LastPageText="Last"/>
            <Columns>
                <asp:TemplateField HeaderText="S. No." HeaderStyle-Width="40" ItemStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <asp:Label ID="lblSNo" runat="server" Text='<%# ((int)DataBinder.Eval(Container, "RowIndex"))+1 %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Order No." HeaderStyle-Width="100">
                    <ItemTemplate>
                        
                        <asp:TextBox ID="txtSerialno" runat="server" class="textbox" style="width:90px; vertical-align:top; margin-bottom:5px;"></asp:TextBox>
                        
                    </ItemTemplate>
                </asp:TemplateField>

                
            </Columns>
        </asp:GridView>





我正在使用以下代码在文本框中添加动态列:





i am adding dynamic column with textbox using follwing code :

//Iterate through the columns of the datatable to set the data bound field dynamically.
            foreach (DataColumn col in dtTemp.Columns)
            {
                //Declare the bound field and allocate memory for the bound field.
                TemplateField bfield = new TemplateField();

                //Initalize the DataField value.
                bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col.ColumnName);

                //Initialize the HeaderText field value.
                bfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, GridViewTemplate.ItemType.Textbox, col.ColumnName);

                //Add the newly created bound field to the GridView.
                grdProductFields.Columns.Add(bfield);
            }



            //Initialize the DataSource
            grdProductFields.DataSource = dt;
            //Bind the datatable with the GridView.
            grdProductFields.DataBind();




使用GridViewTemplate类创建模板字段.





using GridViewTemplate class for create template field.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


//A customized class for displaying the Template Column
public class GridViewTemplate : ITemplate
{
    //A variable to hold the type of ListItemType.
    ListItemType _templateType;

   
    //A variable to hold the column name.
    string _columnName;


    public GridViewTemplate(ListItemType type, string colname)
    {
        //Stores the template type.
        _templateType = type;


        //Stores the column name.
        _columnName = colname;
    }

    //Constructor where we define the template type and column name.
    public GridViewTemplate(ListItemType type, ItemType itemtype , string colname)
    {
        //Stores the template type.
        _templateType = type;

        //Stores the column name.
        _columnName = colname;
    }

    void ITemplate.InstantiateIn(System.Web.UI.Control container)
    {
        switch (_templateType)
        {
            case ListItemType.Header:
                //Creates a new label control and add it to the container.
                Label lbl = new Label();            //Allocates the new label object.
                lbl.Text = _columnName;             //Assigns the name of the column in the lable.
                container.Controls.Add(lbl);        //Adds the newly created label control to the container.
                break;

            case ListItemType.Item:
                //Creates a new text box control and add it to the container.
                        TextBox txt = new TextBox();                            //Allocates the new text box object.
                        txt.DataBinding += new EventHandler(txt_DataBinding);   //Attaches the data binding event.
                        txt.Width = 90;
                        txt.CssClass = "textbox";
                        container.Controls.Add(txt);                            //Adds the newly created textbox to the container.
                break;
            case ListItemType.EditItem:
                //As, I am not using any EditItem, I didnot added any code here.
                break;

            case ListItemType.Footer:
                CheckBox chkColumn = new CheckBox();
                chkColumn.ID = "Chk" + _columnName;
                container.Controls.Add(chkColumn);
                break;
        }
    }
}




单击按钮..,如何获取每个文本框值以存储在数据库中.
我尝试了一些代码,但是,这给了错误..

此代码无法识别后面代码中动态添加的列..
通常,可以通过此代码访问单元格索引0或1,但是当我们访问动态添加的下一个单元格时会出错.





on button click.., how to get the each textbox value to store in database..

i try some code but, this give error..

this code not recognize dynamically added columns in code behind..
normally cell index 0 or 1 are accessible this code but when we going to access next cell who is added dynamically give error.


foreach (GridViewRow grdrow in grdProductFields.Rows)
        {
            TextBox txt1 = null;
            foreach (Control c in grdrow.Cells[2].Controls)
            {
                if (c != null)
                {
                    if (c.GetType() == typeof(TextBox))
                    {
                        txt1 = (TextBox)c;


                        string str = txt1.Text;
                    }

                    string strtype = c.GetType().ToString();
                }
            }

        }



请给我解决方案..



please give me solution..

推荐答案

如果您没有在Page_Init事件期间添加新字段,请将以下代码移至页面的Init方法.

Incase you are not adding the New field During Page_Init event, move the following code to Init Method of page.

foreach (DataColumn col in dtTemp.Columns)
            {
                //Declare the bound field and allocate memory for the bound field.
                TemplateField bfield = new TemplateField();
 
                //Initalize the DataField value.
                bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col.ColumnName);
 
                //Initialize the HeaderText field value.
                bfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, GridViewTemplate.ItemType.Textbox, col.ColumnName);
 
                //Add the newly created bound field to the GridView.
                grdProductFields.Columns.Add(bfield);
            }



只有这样,该字段才会在下一次回发中出现.动态添加控件时,如果希望该控件出现在下一个回发中,则应在Page_Init(有时Page_load也可以工作,但仅适用于子控件)方法中(确切地说,您应在控件中的LoadpostBackData事件之前的事件中添加该控件)页面生命周期,然后IPostBachDataHandler才能够使用回发的数据填充您的控件,请参阅此文章).

希望这会有所帮助.



Only then will the field be present during the next postback. When dynamically adding controls it should be during the Page_Init(Sometimes Page_load also works but for child controls only) method if you want that control to be present in the next postback(To be exact you have add the control in an event before LoadpostBackData event in page life cycle and only then will be IPostBachDataHandler be able to fill your control with posted back data, refer this Article).

Hope this helps.


这篇关于如何在Gridview的itemTemplate中获取动态创建的文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆