You have AutoGenerateColumns = True don't you?
Could you please post the ASP code (in the Code view of your aspx page)?
I don't know why you're using Request to retrieve your text box values.
I don't know why you're creating ASP code either.............
The template dynamically generates the TextBoxes for you, and also dynamically creates a Header for you.
You should not be doing anything like you are on line 5 in your first section of code that you posted. That's defeating the purposes of your template. I'm not even sure how this is really working.
Here's the thing.
When you dynamically create controls in your C# code and add them to your page, you have to pretty much always do this in your Page Init event.
Why?
Because of the ASP.NET life cycle.
ASP.NET creates the C# Objects just before the Page Init event.
After the Page Init event it fills these objects with the data that the user has entered. It also creates any Events that these Objects have fired depending on what the user has done.
This means that if your controls are not dynamically created during the Page Init event, that they will not contain any text that the user has entered....they will not fire any event that either.
So, what I recommend is that you dynamically create your Grid during the Page Init event.
I recommend storing the number of columns that the user wants to display in a Hidden Field so that you can retrieve this number in the Page Init event:
-
/// <summary>
-
/// Initializes the dynamic controls within the Grid so that any events
-
/// that originate from these controls, and any values within these controls
-
/// can be accessed and handled in the rest of the page
-
/// </summary>
-
/// <param name="sender">The object that raised the event.</param>
-
/// <param name="e">The EventArgs for the event</param>
-
private void Page_Init(Object sender , System.EventArgs e )
-
{
-
Integer numColumns;
-
-
// Trying to retrieve the number of columns that the
-
// Grid had last time in order
-
// to recreate the Grid as it was before it was sent to the browser
-
// so that the dynamic controls within the Grid can be loaded correctly.
-
if(Integer.TryParse(Request.Params["numberOfColumns"], numColumns))
-
{
-
InitializeGrid(numColumns);
-
}
-
}
-
-
private void InitializeGrid(Integer numberOfColumns)
-
{
-
// Dynamically creating the Template Columns for the Grid
-
int i;
-
for(i=0; i<numberOfColumns; i++)
-
{
-
TemplateField objTf = new TemplateField();
-
objTf.ItemTemplate = new Gridviewtemplate("2");
-
GridView1.Columns.Add(objTf);
-
}
-
}
-
-
private void Page_PreRender(Object sender, System.EventArgs e)
-
{
-
// Saving the number of columns in the grid so that we can
-
// initialize it again next time
-
numberOfColumns.value = numberOfColumns.ToString();
-
// Please note that numberOfColumns is a HiddenField.
-
}
-
After this step ASP.NET will fill all of the TextBoxes dynamically generated by your template with the Text entered by the user.
The number of rows in the Grid is going to depend on the DataSource that the grid is bound to.