GridView control and a dynamically added DropDownList control!

  • Thread starter Thread starter ata
  • Start date Start date
A

ata

Hi folks,
Consider the following code:

protected void gridView_RowDataBound(object sender,
GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType != DataControlRowType.DataRow)
return;

if (this.gridView.EditIndex != e.Row.RowIndex)
return;

if(someConditionsApply)
{
DropDownList ddl = new DropDownList();
ddl.ID = "cbOtherAttribs" + row.RowIndex;

for (Int32 i = 0; i < 5; i++)
ddl.Items.Add(new ListItem(i.ToString(), "0", true));

row.Cells[3].Controls.Add(ddl);
}
}

This way, I've dynamically added a DropDownList (DDL) control to the
GridView.
However, I need to get the value selected in the DDL when the row is
updating
and add it to the e.NewValues collection:

protected void gridView_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
GridViewRow row = this.gridView.Rows[e.RowIndex];
Control c = row.Cells[3].FindControl("cbOtherAttribs" +
row.RowIndex);
}

However, I've just noticed that the DDL Control I'm trying to retrieve
here is always null! The Cells collection only contains those controls
that's been added to the gridview declaratively.

Would you please let me know what's going on and how I'm supposed to
solve this?

Thanks.
 
Two things you may want to try
the first given you keep all as is :
on the row updating are you using FindControl("ddlname") on the row to get
the ddl? This may solve the problem.
Another way is to convert the column to a template and add a dropdownlist.
You would then bind the value, text, ... to it. Then you will not need to use
findcontrol.

I hope this helps(?)
 
sorry i forgot an example for 1
you will need to do something like this i did not test it so not sure if
it's totally correct:
e.NewValues["MydbUpdateField"] =
((DropDownList)MyGridView.Rows[e.RowIndex].FindControl("myddl")).SelectedValue;
 
Two things you may want to try
the first given you keep all as is :
on the row updating are you using FindControl("ddlname") on the row to get
the ddl? This may solve the problem.
Another way is to convert the column to a template and add a dropdownlist.
You would then bind the value, text, ... to it. Then you will not need to use
findcontrol.

I hope this helps(?)
--
Share The Knowledge. I need all the help I can get and so do you!

Hi folks,
Consider the following code:
protected void gridView_RowDataBound(object sender,
GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType != DataControlRowType.DataRow)
return;
if (this.gridView.EditIndex != e.Row.RowIndex)
return;
if(someConditionsApply)
{
DropDownList ddl = new DropDownList();
ddl.ID = "cbOtherAttribs" + row.RowIndex;
for (Int32 i = 0; i < 5; i++)
ddl.Items.Add(new ListItem(i.ToString(), "0", true));
row.Cells[3].Controls.Add(ddl);
}
}

This way, I've dynamically added a DropDownList (DDL) control to the
GridView.
However, I need to get the value selected in the DDL when the row is
updating
and add it to the e.NewValues collection:
protected void gridView_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
GridViewRow row = this.gridView.Rows[e.RowIndex];
Control c = row.Cells[3].FindControl("cbOtherAttribs" +
row.RowIndex);
}
However, I've just noticed that the DDL Control I'm trying to retrieve
here is always null! The Cells collection only contains those controls
that's been added to the gridview declaratively.
Would you please let me know what's going on and how I'm supposed to
solve this?

Well, I've just found the problem. If the control is created in the
RowCreated event handler, everything works fine. However, there's
still one more problem. When the page is loaded for the first time,
the row that has got the panel will display the panel, very fast! and
then, the panel is gone. therefore, the row's height is modified,
and this is making me crazy.

Here's the new code, any idea?

HyperLink link = new HyperLink();
link.ID = "link" + row.RowIndex;
link.Text = "Other Options";
link.ForeColor = System.Drawing.Color.Blue;
link.Font.Underline = true;
row.Cells[3].Controls.Add(link);

Panel pnl = new Panel();
pnl.ID = "pnl" + row.RowIndex;
pnl.Style.Add(HtmlTextWriterStyle.ZIndex, "100");

RadioButtonList rbl = new RadioButtonList();
rbl.Items.Add(new ListItem("a", "", true));
rbl.Items.Add(new ListItem("b", "", true));
rbl.Items.Add(new ListItem("c", "", true));

Panel inner = new Panel();
inner.Controls.Add(rbl);

pnl.Controls.Add(inner);

AjaxControlToolkit.PopupControlExtender pce = new
AjaxControlToolkit.PopupControlExtender();
pce.BehaviorID = "bhv" + row.RowIndex;
pce.Position = AjaxControlToolkit.PopupControlPopupPosition.Right;
pce.ID = "pce" + row.RowIndex;
pce.Controls.Add(pnl);

pce.TargetControlID = link.ID;
pce.PopupControlID = pnl.ID;

pnl.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
row.Cells[3].Controls.Add(pce);
 
Back
Top