473,404 Members | 2,137 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,404 software developers and data experts.

ASP.NET Building String of Control.ID's

8
I found this code on the Internet (Steve C. Orr). It works. But now I want to make a string of the id's like this:
strId = strId & checkbox.Id & ", " That does not work. Can anyone help me?

Expand|Select|Wrap|Line Numbers
  1.  
  2. public Sub SetTextBoxBackColor(ByVal Page As Control, ByVal clr As color)
  3.        For Each ctrl As Control In Page.Controls
  4.             If TypeOf ctrl Is checkbox Then
  5.                 CType(ctrl, checkBox).BackColor = clr
  6.                 'txt = txt & CType(ctrl, checkBox).id
  7.             Else
  8.                 If ctrl.Controls.Count > 0 Then
  9.                     SetTextBoxBackColor(ctrl, clr)
  10.                 End If
  11.             End If
  12.         Next
  13.     End Sub
  14.  
  15. 'Anywhere in the code_behind:
  16. SetTextBoxBackColor(Me, color.blue) 'VB.NET
  17.  
  18.  
Oct 30 '08 #1
7 1437
Jelle
8
Hello,
I found this code on the internet and it works. It colors the checkbox. But now I want to make a string of all the Id's, like
txt = txt & checkbox.Id & ", ". But that doesn't work. Can anybody tell me how it should be?
Thanks.
Jelle



Expand|Select|Wrap|Line Numbers
  1.  
  2. public Sub SetTextBoxBackColor(ByVal Page As Control, ByVal clr As color)
  3.     dim txt as string        For Each ctrl As Control In Page.Controls
  4.             If TypeOf ctrl Is checkbox Then
  5.                 CType(ctrl, checkBox).BackColor = clr
  6.                 'txt = txt & CType(ctrl, checkBox).id            Else
  7.                 If ctrl.Controls.Count > 0 Then
  8.  
  9.                     SetTextBoxBackColor(ctrl, clr)
  10.                 End If
  11.             End If
  12.         Next
  13.  
  14.     usermsgbox (txt)
  15.     End Sub
  16.  
  17.  
  18.  
  19. 'Anywhere in the code_behind:
  20.     SetTextBoxBackColor(Me, color.blue) 'VB.NET
  21.  
  22.  
  23.  
  24.  
Oct 31 '08 #2
joedeene
583 512MB
How are you returning the 'txt' string variable? You're dimming it in the sub and therefore not accessible by other subs/functions. But try returning it somehow, either set a textbox's .text property to that string('txt') or do a messagebox.show() just to see what you're returning.

joedeene
Oct 31 '08 #3
DrBunchman
979 Expert 512MB
Hi Jelle,

Welcome to Bytes.com! I hope you find the site useful.

You've posted your question in the ASP Forum which is for Classic ASP only - I've moved it for you but in future please post all ASP.NET questions in the .NET Forum.

What do you mean by it doesn't work? Does it throw an error and if so what? Or are the results not as expected and if so what are they?

Thanks,

Dr B
Oct 31 '08 #4
Jelle
8
Thank you Yoodeene en DrBunchman. (I will ask further questions in ASP .net.)

What I want is the string of control.Id's and control.values
In my aspwebsite I made the names of the checkboxen equal to the tablefieldnames.
P.e
I have a table with three fields named:
chkFly
chkBus
chkCar

The checkboxes on the page have got the same names. Now when I get returned the names (and the values) in the For.. Each routine I posted, then I easily can make a SQL-routine.

P.e.
txtNames = "chkFly, chkBus, chkCar, "
txtValues = "True, False, True, "

INSERT INTO table (txtNames) VALUES txtValues;

I often use such a routine in VBA (Access) and I want to do simular in ASP .net.

I hope I made myself clear.

Jelle
Oct 31 '08 #5
Jelle
8
Hi,
Thanks to Joedeene I found the answer myself.
The messagebox in Asp.net can be programmed by the following code:
Expand|Select|Wrap|Line Numbers
  1.     Public Sub ASPNET_MsgBox(ByVal Message As String)
  2.  
  3.         System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)
  4.  
  5.         System.Web.HttpContext.Current.Response.Write("alert(""" & Message & """)" & vbCrLf)
  6.  
  7.         System.Web.HttpContext.Current.Response.Write("</SCRIPT>")
  8.  
  9.     End Sub
  10.  
And then I had to add the method: 'tostring'
Expand|Select|Wrap|Line Numbers
  1. strId = strId & checkbox.Id.tostring & ", "
  2. And not:
  3. strId = strId & checkbox.Id & ", "
  4.  
But I don't understand the else code in the following routine. Is it because of the postback-function?
Expand|Select|Wrap|Line Numbers
  1.     public Function fncMakeSQLCtrlNames(ByVal Page As Control)
  2.     dim ctrlCheckboxlist        as checkboxlist
  3.     dim ctrlCheckbox            as checkbox
  4.     dim ctrlRadioButton         as radiobutton
  5.     dim ctrlRadiobuttonList     as RadioButtonlist
  6.     Dim i as Integer
  7.  
  8.     For Each ctrl As Control In Page.Controls
  9.         If TypeOf ctrl is checkBoxList Then
  10.             ctrlCheckBoxList = ctrl
  11.             if ctrlCheckBoxList.Items.count >0 then
  12.                 For i = 0 To ctrlCheckBoxList.Items.Count - 1
  13.                     strSQLNames = strSQLNames  & ctrl.id.tostring & (i+1) & ", "
  14.                 Next
  15.             End if
  16.         Elseif Typeof ctrl is Radiobuttonlist then
  17.             ctrlRadiobuttonlist = ctrl
  18.             if ctrlradiobuttonlist.Items.count >0 then
  19.                 For i = 0 To ctrlRadiobuttonlist.Items.Count - 1
  20.                     strSQLNames = strSQLNames  & ctrl.id.tostring & (i+1) & ", "
  21.                 Next
  22.             End if
  23.         Elseif Typeof Ctrl is checkBox then
  24.             strSQLNames = strSQLNames  & ctrl.id.tostring & ", "
  25.         Elseif Typeof ctrl is radiobutton then
  26.             strSQLNames = strSQLNames  & ctrl.id.tostring & ", "
  27.         Elseif Typeof Ctrl is TextBox then
  28.             strSQLNames = strSQLNames  & ctrl.id.tostring & ", "
  29.         Else
  30.             If ctrl.Controls.Count > 0 Then
  31.                 fncMakeSQLCtrlNames(ctrl)
  32.             End If
  33.         End If
  34.     Next
  35.  
  36.     fncMakeSQLCtrlNames = left(strSQLNames,(strSQLNames.length)-2)
  37.     End Function
  38.  
  39.  
Nov 4 '08 #6
Frinavale
9,735 Expert Mod 8TB
This seems to be extremely complicated for what you are trying to do!

It would be much more simple to add a checkboxlist to your page and then in your code that you implement for your "Submit" button click event check what the values are.

Not only that but the way you are creating your SQL statement leaves you open for an SQL Insertion attack. It is better to Parametrize your values when creating SQL statements.

Eg:
Expand|Select|Wrap|Line Numbers
  1. Protected Sub mySubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mySubmitButton.Click
  2.  
  3.   Dim fly As Boolean =  myCheckBoxList.Items(1).Selected
  4.   Dim bus As Boolean = myCheckBoxList.Items(2).Selected
  5.   Dim car As Boolean = myCheckBoxList.Items(3).Selected
  6.  
  7.   Dim connectionString As String = "<Connection String To My DB>"
  8.  
  9.   Dim dbCon As SqlConnection
  10.   dbCon = New SqlConnection(connectionString)
  11.   Dim sqlCom As New SqlCommand
  12.  
  13.   sqlCom.Connection = dbCon
  14.   sqlcom.CommandType = CommandType.Text
  15.   sqlCom.CommandText = "INSERT INTO table (chkFly, chkBus, chkCar) VALUES(@Fly,@Bus,@Car)"
  16.  
  17.   sqlCom.Parameters.Add("@Fly", SqlDbType.Boolean).Value = fly
  18.   sqlCom.Parmaeters.Add("@Bus", SqlDbType.Boolean).Value = bus
  19.   sqlCom.Parameters.Add("@Car",SqlDbType.Boolean).Value= car
  20.  
  21.   dbCon.Open()
  22.   sqlcom.ExecuteNonQuery
  23.   dbCon.Close()
  24. End Sub
See this article for a very quick over view on how to use a database in your application...and see this article for more information on how to use dynamic controls in ASP.NET (if you insist on using dynamic controls when there is no reason to)
Nov 4 '08 #7
Jelle
8
Thanks. And for the articles.
Jelle
Nov 5 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: sarunnio | last post by:
Hi, I 've tried to work around with this long times ago. I have a template and the page loaded inside. When loading the page,the control ID contained with the page will be automatically...
4
by: Neil Stevens | last post by:
Hi, Merry Christmas to you all. I have a problem with server controls, i am writing a C# ASP.NET web project and i am creating custom server controls for some of the more common element. One...
2
by: Andy Fish | last post by:
Hi, First some background: When you databind a repeater control, the controls within the template are given an id like Repeater1:_ctl<n>:Button1 where <n> increments for each repeater item. If...
1
by: shapper | last post by:
Hello, Can someone tell me where can I find a list of the characters which I can use in a web control ID? Can I for example do: MyLabel.ID = "Namespace#Control_Type" Thanks,
7
by: John Kotuby | last post by:
Hi all, I am trying to use a calendar server control which I have ID="Calendar1" in the source code. What I am trying to do is emulate a JavaScript calendar that was being used in an older ASP...
2
by: JJ | last post by:
I have a control that can be placed on any page 1 or more times (like a web part). The control loads text from database tables. Its the first time I've done this so I just need a pointer to...
3
by: Allan Ebdrup | last post by:
I get the error "Error creating control: ID property is not specified" when I view my custom web control in design view. I add the control that gives an error in OnInit like this: ----- foreach...
4
by: Leon | last post by:
Hi everybody, I am a beginer for Python, hope can get help from you guys. What I want to do is : Input an ID -find the ID in the file -copy the whole string <str id='xxx'>yyyyy</str> ...
6
by: sudhashekhar30 | last post by:
hi i am trying to make a general function which is accepting control id as argument. mine code is like this. but it's not working. Javascript function function check(var obj) {alert(id of...
0
by: juggerling | last post by:
I have a third party form open and wish to get the control id from the text box with the current caret focus; there are a great many text boxes on this form each containing a unique control id. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.