Accessing dynamically added control from GridView Footer row

N

neeraj

Hi,
I have GridView control in my asp.net page with auto generated fields
there is only footer template is present under asp:TemplateField.

I can bind this gridview with any of databae table as depend on user
selection. Now want to add new record in database so I have added text
boxes on footer template cells at runtime depend on number of columns
on table. But when I accessing these text boxes from footer template
on gridview_RowCommand event its not retriving textbox control.

Plese suggest me how can I get footer control textboxes. So that I can
save data in database.

this is the code

Aspx
---------------------------------------------------------------------------------------------------------------------------------------------------------------------

asp:GridView ID="gvTableData" runat="server" Width="100%"
AllowPaging="True" PageSize="22"

OnPageIndexChanging="gvTableData_PageIndexChanging"
AllowSorting="True" OnSorting="gvTableData_Sorting"
AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True"
OnRowCancelingEdit="gvTableData_RowCancelingEdit"

OnRowDataBound="gvTableData_RowDataBound"
OnRowDeleting="gvTableData_RowDeleting"
OnRowEditing="gvTableData_RowEditing"
OnRowUpdating="gvTableData_RowUpdating"
EmptyDataText="No Data Found"
ShowFooter="True" OnRowCommand="gvTableData_RowCommand">
<RowStyle BackColor="#EEEEEE"
VerticalAlign="Bottom" CssClass="frmField" Font-Bold="False"
ForeColor="Black" />
<AlternatingRowStyle
BackColor="Gainsboro" VerticalAlign="Bottom" CssClass="frmField" />
<HeaderStyle CssClass="gridHeader" />
<PagerSettings
Mode="NumericFirstLast" />
<PagerStyle Font-Bold="True" Font-
Size="Small" />
<Columns>
<asp:TemplateField HeaderStyle-
HorizontalAlign="center" FooterStyle-HorizontalAlign="center"
ItemStyle-
HorizontalAlign="center">
<FooterTemplate>
<asp:ImageButton
ID="btnAddNew" ImageUrl="~/Images/add.png" runat="server"
ValidationGroup="ADDGROUP" AlternateText="Add New Record"
CommandName="ADD" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataRowStyle Font-Bold="True"
Font-Size="X-Large" ForeColor="Red" HorizontalAlign="Center"
VerticalAlign="Middle" />

</asp:GridView>

--------------------------------------------------------------------------
Code
Behind------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}

}



protected void gvTableData_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{


for (int i = 2; i < e.Row.Cells.Count; i++)
{
tc = e.Row.Cells;

txtBox = new TextBox();
txtBox.Width = new Unit(100, UnitType.Pixel);
txtBox.ID = string.Format("ctltxt0{0}", i -
1);
txtBox.CssClass = "frmField";
tc.Controls.Add(txtBox);

}
}

}


protected void gvTableData_RowCommand(object sender,
GridViewCommandEventArgs e)
{
TextBox txt;
string s;
if (e.CommandName == "ADD")
{
txt = gvTableData.FooterRow.Cells[3].Controls[0];
// text box getting null
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top