GridView control and a dynamically added DropDownList control!

Discussion in 'ASP .Net' started by ata@mailinator.com, Jun 6, 2008.

  1. Guest

    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.
    , Jun 6, 2008
    #1
    1. Advertising

  2. 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!


    "" wrote:

    > 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.
    >
    Yankee Imperialist Dog, Jun 6, 2008
    #2
    1. Advertising

  3. 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;
    --
    Share The Knowledge. I need all the help I can get and so do you!


    "" wrote:

    > 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.
    >
    Yankee Imperialist Dog, Jun 6, 2008
    #3
  4. Guest

    On Jun 6, 5:37 pm, Yankee Imperialist Dog
    <> wrote:
    > 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!
    >
    > "" wrote:
    > > 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.


    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);
    , Jun 6, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGltaXRyaXMgUGFudGF6b3BvdWxvcw==?=

    How to use dynamically added dropdownlist controls?

    =?Utf-8?B?RGltaXRyaXMgUGFudGF6b3BvdWxvcw==?=, Jul 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    373
    =?Utf-8?B?RGltaXRyaXMgUGFudGF6b3BvdWxvcw==?=
    Jul 10, 2004
  2. Bennett Haselton
    Replies:
    1
    Views:
    512
    John M Deal
    Nov 8, 2004
  3. Jeffrey Todd
    Replies:
    1
    Views:
    4,101
    Peter Blum
    Jun 2, 2005
  4. Replies:
    0
    Views:
    1,678
  5. =?Utf-8?B?QmlsbDQ0MDc3?=

    Dynamically added Checkbox in Gridview

    =?Utf-8?B?QmlsbDQ0MDc3?=, Aug 1, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    4,595
Loading...

Share This Page