GridView control and a dynamically added DropDownList control!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ata@mailinator.com

    GridView control and a dynamically added DropDownList control!

    Hi folks,
    Consider the following code:

    protected void gridView_RowDat aBound(object sender,
    GridViewRowEven tArgs e)
    {
    GridViewRow row = e.Row;
    if (row.RowType != DataControlRowT ype.DataRow)
    return;

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

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

    for (Int32 i = 0; i < 5; i++)
    ddl.Items.Add(n ew ListItem(i.ToSt ring(), "0", true));

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

    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_RowUpd ating(object sender,
    GridViewUpdateE ventArgs e)
    {
    GridViewRow row = this.gridView.R ows[e.RowIndex];
    Control c = row.Cells[3].FindControl("c bOtherAttribs" +
    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.
  • =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?=

    #2
    RE: GridView control and a dynamically added DropDownList control!

    Two things you may want to try
    the first given you keep all as is :
    on the row updating are you using FindControl("dd lname") 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!


    "ata@mailinator .com" wrote:
    Hi folks,
    Consider the following code:
    >
    protected void gridView_RowDat aBound(object sender,
    GridViewRowEven tArgs e)
    {
    GridViewRow row = e.Row;
    if (row.RowType != DataControlRowT ype.DataRow)
    return;
    >
    if (this.gridView. EditIndex != e.Row.RowIndex)
    return;
    >
    if(someConditio nsApply)
    {
    DropDownList ddl = new DropDownList();
    ddl.ID = "cbOtherAttribs " + row.RowIndex;
    >
    for (Int32 i = 0; i < 5; i++)
    ddl.Items.Add(n ew ListItem(i.ToSt ring(), "0", true));
    >
    row.Cells[3].Controls.Add(d dl);
    }
    }
    >
    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_RowUpd ating(object sender,
    GridViewUpdateE ventArgs e)
    {
    GridViewRow row = this.gridView.R ows[e.RowIndex];
    Control c = row.Cells[3].FindControl("c bOtherAttribs" +
    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.
    >

    Comment

    • =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?=

      #3
      RE: GridView control and a dynamically added DropDownList control!

      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["MydbUpdateFiel d"] =
      ((DropDownList) MyGridView.Rows[e.RowIndex].FindControl("m yddl")).Selecte dValue;
      --
      Share The Knowledge. I need all the help I can get and so do you!


      "ata@mailinator .com" wrote:
      Hi folks,
      Consider the following code:
      >
      protected void gridView_RowDat aBound(object sender,
      GridViewRowEven tArgs e)
      {
      GridViewRow row = e.Row;
      if (row.RowType != DataControlRowT ype.DataRow)
      return;
      >
      if (this.gridView. EditIndex != e.Row.RowIndex)
      return;
      >
      if(someConditio nsApply)
      {
      DropDownList ddl = new DropDownList();
      ddl.ID = "cbOtherAttribs " + row.RowIndex;
      >
      for (Int32 i = 0; i < 5; i++)
      ddl.Items.Add(n ew ListItem(i.ToSt ring(), "0", true));
      >
      row.Cells[3].Controls.Add(d dl);
      }
      }
      >
      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_RowUpd ating(object sender,
      GridViewUpdateE ventArgs e)
      {
      GridViewRow row = this.gridView.R ows[e.RowIndex];
      Control c = row.Cells[3].FindControl("c bOtherAttribs" +
      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.
      >

      Comment

      • ata@mailinator.com

        #4
        Re: GridView control and a dynamically added DropDownList control!

        On Jun 6, 5:37 pm, Yankee Imperialist Dog
        <YankeeImperial ist...@discussi ons.microsoft.c omwrote:
        Two things you may want to try
        the first given you keep all as is :
        on the row updating are you using FindControl("dd lname") 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!
        >
        "a...@mailinato r.com" wrote:
        Hi folks,
        Consider the following code:
        >
        protected void gridView_RowDat aBound(object sender,
        GridViewRowEven tArgs e)
        {
        GridViewRow row = e.Row;
        if (row.RowType != DataControlRowT ype.DataRow)
        return;
        >
        if (this.gridView. EditIndex != e.Row.RowIndex)
        return;
        >
        if(someConditio nsApply)
        {
        DropDownList ddl = new DropDownList();
        ddl.ID = "cbOtherAttribs " + row.RowIndex;
        >
        for (Int32 i = 0; i < 5; i++)
        ddl.Items.Add(n ew ListItem(i.ToSt ring(), "0", true));
        >
        row.Cells[3].Controls.Add(d dl);
        }
        }
        >
        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_RowUpd ating(object sender,
        GridViewUpdateE ventArgs e)
        {
        GridViewRow row = this.gridView.R ows[e.RowIndex];
        Control c = row.Cells[3].FindControl("c bOtherAttribs" +
        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.Under line = true;
        row.Cells[3].Controls.Add(l ink);

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

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

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

        pnl.Controls.Ad d(inner);

        AjaxControlTool kit.PopupContro lExtender pce = new
        AjaxControlTool kit.PopupContro lExtender();
        pce.BehaviorID = "bhv" + row.RowIndex;
        pce.Position = AjaxControlTool kit.PopupContro lPopupPosition. Right;
        pce.ID = "pce" + row.RowIndex;
        pce.Controls.Ad d(pnl);

        pce.TargetContr olID = link.ID;
        pce.PopupContro lID = pnl.ID;

        pnl.Style.Add(H tmlTextWriterSt yle.Visibility, "hidden");
        row.Cells[3].Controls.Add(p ce);

        Comment

        Working...