datagrid dynamic column template

J

Jacob

hi everyone,

i've built a datagrid template that displays different cell controls depending
on the data bound to it. eg: in the first row, the control may be a textbox,
and in the next row, the control may be a checkbox. currently i go about
doing this by adding the control when databind is called.

public class MyTemplate : System.Web.UI.ITemplate
{
private void DataBindItem(object sender, EventArgs e)
{
PlaceHolder placeHolder = (PlaceHolder)sender;
DataGridItem dataGridItem = (DataGridItem)placeHolder.NamingContainer;

string rowType = Convert.ToString(DataBinder.Eval(dataGridItem.DataItem,
"RowType"));

if(rowType == "TextBox") {
TextBox myTextBox = new TextBox();
myTextBox.ID = "myTextBox";

Label rowTypeLabel = new Label();
rowTypeLabel.ID = "rowTypeLabel";
rowTypeLabel.Visible = false;


placeHolder.Controls.Add(myTextBox);
placeHolder.Controls.Add(rowTypeLabel);
return;
}

if(rowType == "CheckBox") {
CheckBox myCheckBox = new CheckBox();
myCheckBox.ID = "myCheckBox";

Label rowTypeLabel = new Label();
rowTypeLabel.ID = "rowTypeLabel";
rowTypeLabel.Visible = false;

placeHolder.Controls.Add(myTextBox);
placeHolder.Controls.Add(rowTypeLabel);
return;
}

}

public void InstantiateIn(Control container)
{
PlaceHolder placeHolder = new PlaceHolder();
placeHolder.DataBinding += new EventHandler(DataBindItem);

container.Controls.Add(placeHolder);
}

}

the datagrid is displayed correctly with the textboxes and checkboxes. however,
when i iterate throught the datagrid rows, i can't reference the textbox
or checkbox.

private void OnClickUpdateButton(object sender, System.Web.UI.ImageClickEventArgs
e)
{
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top