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? -
-
public Sub SetTextBoxBackColor(ByVal Page As Control, ByVal clr As color)
-
For Each ctrl As Control In Page.Controls
-
If TypeOf ctrl Is checkbox Then
-
CType(ctrl, checkBox).BackColor = clr
-
'txt = txt & CType(ctrl, checkBox).id
-
Else
-
If ctrl.Controls.Count > 0 Then
-
SetTextBoxBackColor(ctrl, clr)
-
End If
-
End If
-
Next
-
End Sub
-
-
'Anywhere in the code_behind:
-
SetTextBoxBackColor(Me, color.blue) 'VB.NET
-
-
7 1437
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 -
-
public Sub SetTextBoxBackColor(ByVal Page As Control, ByVal clr As color)
-
dim txt as string For Each ctrl As Control In Page.Controls
-
If TypeOf ctrl Is checkbox Then
-
CType(ctrl, checkBox).BackColor = clr
-
'txt = txt & CType(ctrl, checkBox).id Else
-
If ctrl.Controls.Count > 0 Then
-
-
SetTextBoxBackColor(ctrl, clr)
-
End If
-
End If
-
Next
-
-
usermsgbox (txt)
-
End Sub
-
-
-
-
'Anywhere in the code_behind:
-
SetTextBoxBackColor(Me, color.blue) 'VB.NET
-
-
-
-
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
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
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
Hi,
Thanks to Joedeene I found the answer myself.
The messagebox in Asp.net can be programmed by the following code: -
Public Sub ASPNET_MsgBox(ByVal Message As String)
-
-
System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)
-
-
System.Web.HttpContext.Current.Response.Write("alert(""" & Message & """)" & vbCrLf)
-
-
System.Web.HttpContext.Current.Response.Write("</SCRIPT>")
-
-
End Sub
-
And then I had to add the method: 'tostring' -
strId = strId & checkbox.Id.tostring & ", "
-
And not:
-
strId = strId & checkbox.Id & ", "
-
But I don't understand the else code in the following routine. Is it because of the postback-function? -
public Function fncMakeSQLCtrlNames(ByVal Page As Control)
-
dim ctrlCheckboxlist as checkboxlist
-
dim ctrlCheckbox as checkbox
-
dim ctrlRadioButton as radiobutton
-
dim ctrlRadiobuttonList as RadioButtonlist
-
Dim i as Integer
-
-
For Each ctrl As Control In Page.Controls
-
If TypeOf ctrl is checkBoxList Then
-
ctrlCheckBoxList = ctrl
-
if ctrlCheckBoxList.Items.count >0 then
-
For i = 0 To ctrlCheckBoxList.Items.Count - 1
-
strSQLNames = strSQLNames & ctrl.id.tostring & (i+1) & ", "
-
Next
-
End if
-
Elseif Typeof ctrl is Radiobuttonlist then
-
ctrlRadiobuttonlist = ctrl
-
if ctrlradiobuttonlist.Items.count >0 then
-
For i = 0 To ctrlRadiobuttonlist.Items.Count - 1
-
strSQLNames = strSQLNames & ctrl.id.tostring & (i+1) & ", "
-
Next
-
End if
-
Elseif Typeof Ctrl is checkBox then
-
strSQLNames = strSQLNames & ctrl.id.tostring & ", "
-
Elseif Typeof ctrl is radiobutton then
-
strSQLNames = strSQLNames & ctrl.id.tostring & ", "
-
Elseif Typeof Ctrl is TextBox then
-
strSQLNames = strSQLNames & ctrl.id.tostring & ", "
-
Else
-
If ctrl.Controls.Count > 0 Then
-
fncMakeSQLCtrlNames(ctrl)
-
End If
-
End If
-
Next
-
-
fncMakeSQLCtrlNames = left(strSQLNames,(strSQLNames.length)-2)
-
End Function
-
-
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: -
Protected Sub mySubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mySubmitButton.Click
-
-
Dim fly As Boolean = myCheckBoxList.Items(1).Selected
-
Dim bus As Boolean = myCheckBoxList.Items(2).Selected
-
Dim car As Boolean = myCheckBoxList.Items(3).Selected
-
-
Dim connectionString As String = "<Connection String To My DB>"
-
-
Dim dbCon As SqlConnection
-
dbCon = New SqlConnection(connectionString)
-
Dim sqlCom As New SqlCommand
-
-
sqlCom.Connection = dbCon
-
sqlcom.CommandType = CommandType.Text
-
sqlCom.CommandText = "INSERT INTO table (chkFly, chkBus, chkCar) VALUES(@Fly,@Bus,@Car)"
-
-
sqlCom.Parameters.Add("@Fly", SqlDbType.Boolean).Value = fly
-
sqlCom.Parmaeters.Add("@Bus", SqlDbType.Boolean).Value = bus
-
sqlCom.Parameters.Add("@Car",SqlDbType.Boolean).Value= car
-
-
dbCon.Open()
-
sqlcom.ExecuteNonQuery
-
dbCon.Close()
-
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)
Thanks. And for the articles.
Jelle
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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,
|
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...
|
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...
|
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...
|
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>
...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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: 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...
|
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,...
| |