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)
{
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)
{