473,587 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 RequiredFieldVa lidator

tb.Text = Convert.ToStrin g(e.Item.DataIt em("val"))
editCell.Contro ls.Add(tb)
If e.Item.DataItem ("req") Then
vld.ControlToVa lidate = FindControl(tb. ID.ToString)
vld.ErrorMessag e = "Required Field!"
editCell.Contro ls.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 3602
Just to vld.ControlToVa lidated = 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********@yah oo.com> wrote in message
news:35******** *****@individua l.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 RequiredFieldVa lidator

tb.Text = Convert.ToStrin g(e.Item.DataIt em("val"))
editCell.Contro ls.Add(tb)
If e.Item.DataItem ("req") Then
vld.ControlToVa lidate = FindControl(tb. ID.ToString)
vld.ErrorMessag e = "Required Field!"
editCell.Contro ls.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.ControlToVa lidated = 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 ControlToValida te property of '' cannot be blank.

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

tb.ClientID:
Unable to find control id 'dgGuest__ctl2_ _ctl0' referenced by the
'ControlToValid ate' 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********@yah oo.com> wrote in message
news:35******** *****@individua l.net...
Karl Seguin wrote:
Just to vld.ControlToVa lidated = 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 ControlToValida te property of '' cannot be blank.

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

tb.ClientID:
Unable to find control id 'dgGuest__ctl2_ _ctl0' referenced by the
'ControlToValid ate' 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
2464
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 added row it also removes the row at the end along with the added row. Plz tell me if, I am missing any thing. Code </asp:datagrid>
7
2425
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 the moment the controls just stay where I place them and the table gets built on top of them. I have tried searching the net for a solution but being...
2
1956
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 DataGridItem(DataGrid1.Items.Count+1,DataGrid1.Items.Count+1,ListItemType.Item); di.Cells.Add(tc); di.Cells.Add(tc1);
2
2200
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 "PlaceHolder" control "ph" in the user control; 2. create a Label control and TextBox control when in the "add" button
5
4651
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 combo by allowing the user to dynamically type in the new value directly in the combo box whcih in turn updates the lookup table and then when they...
4
5045
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, websites, MSDN and was not able to find something that would help me understand and code. I might not be searching for the right words or phrases. ...
2
2446
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 TemplateColumn dynamically. I persist a DataSet in my Session object and I bind the CheckBox to the DataSet. So far so good. The CheckBoxes show up and the...
7
3563
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 the button is added to is created at run time? here is code from my aspx page......
1
4884
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 set text boxes and labels inside the table rows. I then added a button. All of these are done through code. The problem that i am having is i can...
0
7849
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8347
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.