在gridview单元格内创建的动态文本字段在访问时给出null值

时间:2014-09-17 20:34:25

标签: c# asp.net gridview dynamic

我有一个4列的gridview,其中前两列有ItemTemplate文件,即dropdownlist和textfield剩下1列有动态创建的标签,在最后一列有两个textfields在单元格内 以下是我的代码 在Page_Load()函数中,我在gridview单元格中动态添加文本字段和标签。

protected void Page_Load(object sender, EventArgs e)
    {
        int subrows = 2;
        int rows = 2;
        if (!IsPostBack)
        {
            DataTable Dt = new DataTable();
            Dt.Columns.Add("index", typeof(string));
            for (int i = 0; i < rows; i++)
            {
                DataRow Dr = Dt.NewRow();
                Dr["index"] = i;
                Dt.Rows.Add(Dr);
            }
            GridView1.DataSource = Dt;
            GridView1.DataBind();

            int c = 2;

            for (int j = 0; j < rows; j++)
            {
                for (int k = 0; k < subrows; k++)
                {
                    Label l = new Label();
                    l.ID = "lbl" + k;
                    l.Text = "" + "<br/>";
                    l.Attributes.Add("runat", "server");

                    GridView1.Rows[j].Cells[2].Controls.Add(l);

                    Label lbl = (Label)GridView1.Rows[j].Cells[2].FindControl("lbl" + k);
                    lbl.Text = "jay" + "<br/>";
                    lbl.Attributes.Add("runat", "server");

                    TextBox t = new TextBox();
                    t.ID = "txt" + k;
                    t.Attributes.Add("runat", "server");

                    GridView1.Rows[j].Cells[3].Controls.Add(t);

                }
            }
        }
    }

在Button2_Click()函数上我想用gridview访问字段的值。

protected void Button2_Click(object sender, EventArgs e)
    {
        int subrows1 = 2;
        int rows1 = 2;

        for (int i = 0; i < rows1; i++)
        {
            DropDownList dl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
            TextBox tx = (TextBox)GridView1.Rows[i].FindControl("TextBox1");

            for (int j = 0; j < subrows1; j++)
            {
                 Label l1 = (Label)GridView1.Rows[i].Cells[2].FindControl("lbl" + j);
                 TextBox t = (TextBox)GridView1.Rows[i].Cells[3].FindControl("txt" + j);

            }
        }
    }

但我只能从前两列访问dropdownlist和textbox的值,这两列是使用编辑模板添加的ItemTemplate字段。当我尝试访问在gridview中动态添加的字段的值时,给出 null 值。

标记文件:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
            CellPadding="3" CssClass=" table table-bordered" 
            RowStyle-HorizontalAlign="Center" Width="100%" >
            <Columns>
                <asp:TemplateField HeaderText="Field office">
                    <ItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server">
                            <asp:ListItem>--Select Field--</asp:ListItem>
                            <asp:ListItem>Ahmedabad</asp:ListItem>
                            <asp:ListItem>Ahmedabad-2</asp:ListItem>

                        </asp:DropDownList>
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Center" />
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Field Center ">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="SS ">
                    <ItemTemplate></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Productivity">
                    <ItemTemplate></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Rate">
                    <ItemTemplate></ItemTemplate>
                </asp:TemplateField>
            </Columns>

        </asp:GridView>

使用标记文件

进行更新

0 个答案:

没有答案