Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > GridView control and a dynamically added DropDownList control!

Reply
Thread Tools

GridView control and a dynamically added DropDownList control!

 
 
ata@mailinator.com
Guest
Posts: n/a
 
      06-06-2008
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.
 
Reply With Quote
 
 
 
 
Yankee Imperialist Dog
Guest
Posts: n/a
 
      06-06-2008
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.
>

 
Reply With Quote
 
 
 
 
Yankee Imperialist Dog
Guest
Posts: n/a
 
      06-06-2008
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.
>

 
Reply With Quote
 
ata@mailinator.com
Guest
Posts: n/a
 
      06-06-2008
On Jun 6, 5:37 pm, Yankee Imperialist Dog
<YankeeImperialist...@discussions.microsoft.com> 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!
>
> "a...@mailinator.com" 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);

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Solution for posterity: GridView, Datakeys, and "Item has already been added. Key in dictionary: 'CategoryID' Key being added: 'CategoryID'" ASP .Net 2 11-02-2006 04:48 AM
reference web control in dynamically added user control, ASP.NET 2 =?Utf-8?B?U2FtdWVs?= ASP .Net 7 08-10-2005 02:12 AM
Inserting Controls Dynamically - with Validators Also Added Dynamically Jeffrey Todd ASP .Net 1 06-02-2005 04:33 PM
how come user control added to page doesn't get added to codebehind file? Bennett Haselton ASP .Net 1 11-08-2004 09:26 PM
How to use dynamically added dropdownlist controls? =?Utf-8?B?RGltaXRyaXMgUGFudGF6b3BvdWxvcw==?= ASP .Net 2 07-10-2004 01:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57