Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > dynamically adding controls to a datagrid

Reply
Thread Tools

dynamically adding controls to a datagrid

 
 
MattB
Guest
Posts: n/a
 
      01-17-2005
Hello. I'm creating a page to edit a person's record (name address, etc)
using a DataGrid. I want the fields that can be edited to be created at
runtime based on a list of columns in web.config. This is working fine,
and I can add a control (TextBox or DropDownList) dynamically for the
user to fill in a value. This works well for just adding controls for
data entry.

Now, I want to add validators on the fly as well and it not going quite
as smoothly. I'm suing the following excerpt in the ItemDataBound event
for this DataGrid:

Dim tb As New TextBox, vld As New RequiredFieldValidator

tb.Text = Convert.ToString(e.Item.DataItem("val"))
editCell.Controls.Add(tb)
If e.Item.DataItem("req") Then
vld.ControlToValidate = FindControl(tb.ID.ToString)
vld.ErrorMessage = "Required Field!"
editCell.Controls.Add(vld)
End If

IntelliSense is flagging the FindControl part saying:

"System.Web.UI.Control" cannot be converted to string

Is this a reasonabler approach that I just need to fix the syntax of, or
am I taking the wrong approach? Any suggestions? Thanks!

Matt
 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      01-17-2005
Just to vld.ControlToValidated = tb.ID no need to do a FindControl on it,
you already found it it's tb...you created it...

You might need to use ClientId or UniqueId instead of just ID...not 100%
sure...but just a heads up...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


"MattB" <> wrote in message
news:...
> Hello. I'm creating a page to edit a person's record (name address, etc)
> using a DataGrid. I want the fields that can be edited to be created at
> runtime based on a list of columns in web.config. This is working fine,
> and I can add a control (TextBox or DropDownList) dynamically for the
> user to fill in a value. This works well for just adding controls for
> data entry.
>
> Now, I want to add validators on the fly as well and it not going quite
> as smoothly. I'm suing the following excerpt in the ItemDataBound event
> for this DataGrid:
>
> Dim tb As New TextBox, vld As New RequiredFieldValidator
>
> tb.Text = Convert.ToString(e.Item.DataItem("val"))
> editCell.Controls.Add(tb)
> If e.Item.DataItem("req") Then
> vld.ControlToValidate = FindControl(tb.ID.ToString)
> vld.ErrorMessage = "Required Field!"
> editCell.Controls.Add(vld)
> End If
>
> IntelliSense is flagging the FindControl part saying:
>
> "System.Web.UI.Control" cannot be converted to string
>
> Is this a reasonabler approach that I just need to fix the syntax of, or
> am I taking the wrong approach? Any suggestions? Thanks!
>
> Matt



 
Reply With Quote
 
 
 
 
MattB
Guest
Posts: n/a
 
      01-17-2005
Karl Seguin wrote:
> Just to vld.ControlToValidated = tb.ID no need to do a FindControl on it,
> you already found it it's tb...you created it...
>
> You might need to use ClientId or UniqueId instead of just ID...not 100%
> sure...but just a heads up...
>
> Karl
>


Thanks for the reply, but I was trying FindControl because tb.ID didn't
work (Intellisense said it was ok, but it failed at runtime).

So here's the runtime errors I get for each of those suggestions:

tb.ID:
The ControlToValidate property of '' cannot be blank.

tb.UniqueID:
Unable to find control id 'dgGuest:_ctl2:_ctl0' referenced by the
'ControlToValidate' property of ''.

tb.ClientID:
Unable to find control id 'dgGuest__ctl2__ctl0' referenced by the
'ControlToValidate' property of ''.

So could it be that in adding a second control to the same cell I'm
somehow overwriting the tb control? Any other ideas?

THANKS!

Matt
 
Reply With Quote
 
Karl Seguin
Guest
Posts: n/a
 
      01-17-2005
You can simply use ID if you give the tb an id before adding it to the cell

dim tb as new TextBox
tb.ID = "txt"


Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


"MattB" <> wrote in message
news:...
> Karl Seguin wrote:
> > Just to vld.ControlToValidated = tb.ID no need to do a FindControl on

it,
> > you already found it it's tb...you created it...
> >
> > You might need to use ClientId or UniqueId instead of just ID...not 100%
> > sure...but just a heads up...
> >
> > Karl
> >

>
> Thanks for the reply, but I was trying FindControl because tb.ID didn't
> work (Intellisense said it was ok, but it failed at runtime).
>
> So here's the runtime errors I get for each of those suggestions:
>
> tb.ID:
> The ControlToValidate property of '' cannot be blank.
>
> tb.UniqueID:
> Unable to find control id 'dgGuest:_ctl2:_ctl0' referenced by the
> 'ControlToValidate' property of ''.
>
> tb.ClientID:
> Unable to find control id 'dgGuest__ctl2__ctl0' referenced by the
> 'ControlToValidate' property of ''.
>
> So could it be that in adding a second control to the same cell I'm
> somehow overwriting the tb control? Any other ideas?
>
> THANKS!
>
> Matt



 
Reply With Quote
 
MattB
Guest
Posts: n/a
 
      01-17-2005
Karl Seguin wrote:
> You can simply use ID if you give the tb an id before adding it to the cell
>
> dim tb as new TextBox
> tb.ID = "txt"
>
>
> Karl
>


Cool. I actually just stumbled on that myself. Thanks again!

Matt
 
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
Adding Dynamic Controls to a DataGrid for use with the built in DataGrid Edit/Update functionality davidjgonzalez@gmail.com ASP .Net Datagrid Control 1 03-25-2006 06:55 AM
Inserting Controls Dynamically - with Validators Also Added Dynamically Jeffrey Todd ASP .Net 1 06-02-2005 04:33 PM
Efficiency of dynamically adding web user controls MC D ASP .Net 4 11-18-2003 07:27 PM
Dynamically adding different controls to different rows in DataGrid does not work Jonas ASP .Net Datagrid Control 0 09-23-2003 02:52 PM
Dynamically adding controls to page SOURCE file John Wallace ASP .Net 0 08-06-2003 08:14 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