473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

dynamically adding controls to a datagrid

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
Nov 19 '05 #1
4 3592
Just to vld.ControlToValidated = tb.ID no need to do a FindControl on it,
you already found it :P 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" <so********@yahoo.com> wrote in message
news:35*************@individual.net...
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

Nov 19 '05 #2
Karl Seguin wrote:
Just to vld.ControlToValidated = tb.ID no need to do a FindControl on it,
you already found it :P 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
Nov 19 '05 #3
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" <so********@yahoo.com> wrote in message
news:35*************@individual.net...
Karl Seguin wrote:
Just to vld.ControlToValidated = tb.ID no need to do a FindControl on it, you already found it :P 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

Nov 19 '05 #4
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
Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: sameer mowade via .NET 247 | last post by:
Hello All, I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically...
7
by: Grant | last post by:
I have this table on my form which gets populated with data from my database at runtime. I would like to place some controls (linkbutton, textbox etc.) beneath the table when it is complete - at...
2
by: Praveen Balanagendra via .NET 247 | last post by:
here is the source code private void AddRow() { TableCell tc = new TableCell(); tc.Controls.Add(new LiteralControl("NewRow")); DataGridItem di = new...
2
by: Quentin Huo | last post by:
Hi: I have a user control and I want to dynamically create a Label control and TextBox control when the button "add more author name" is clicked. I did like this: 1. there is a...
5
by: Aaron Ackerman | last post by:
I have a bound combobox the appears on a cell within the column of my bound grid when the user clicks on a cell n(In my vb.net WinForm app). I am trying to allow the adding of an item to that bound...
4
by: sydney.luu | last post by:
Hello, I would greatly appreciate if someone can show me how to dynamically build a Repeater with unknown number of columns at design time. I have looked various threads in this newsgroup,...
2
by: Pete Moss | last post by:
During a postback event, I am having trouble retrieving the state of a CheckBox Control that I am dynamically adding to a DataGrid Control using ASP.NET 1.1. I have no trouble adding the...
7
by: rsaffy | last post by:
I am having trouble with my dynamically created button's event handling. I read that the buttons need to be recreated on every trip to the server, but how exactly do you do that when the datagrid...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.