Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I even do this? ASP.NET controls and dynamic access 2

Status
Not open for further replies.

xcalibre

Programmer
Oct 27, 2005
12
0
0
CA
Hi everyone,
I am stumpped...

I have a long list of checkboxes that I have saved their states to a database. If a record for our particular date is in the database, I want to load up all the states to each checkbox.

Instead of manually doing
Code:
cbItem1.value = CSTR(rdr("Item1))
for each item I was kind of hoping to do the following:

Each column in the DB has the same name as the control name. Can I "find" the control by name and then change it's value.

eg.
Code:
For x = 0 to rdr.FieldCount -1
    ctrol = Page.FindControl(rdr.GetName(x))
    ctrol.selected = rdr(x)
Next x

Any direction/help is appreciated. I know this would help if the list of checkboxes grew.

Thanks.
Xcalibre
 
xcalibre: This code has been posted a few times from time to time but could not locate a thread on point in the Search. Try also a search using Google Groups and see what you might come up with. I am pasting a snippet of code in which I loop through a series of checkboxes inside a DataGrid so is not exactly on point but perhaps therein may be an insight to help you along, e.g., the FindControl method. Just a thought. Someone else will probably drop by and give you thier insight on this; just thought I'd post this in case it might help.
Code:
For i = 0 to dgInsects.Items.Count - 1
       blnIsChecked = DirectCast(dgInsects.Items(i).Cells(1).FindControl("chkT"), Web.UI.WebControls.CheckBox).Checked           
       If blnIsChecked = True Then        
         If k=0 Then
          strURL = dgInsects.Items(i).Cells(0).Text
          strdd = DirectCast(dgInsects.Items(i).Cells(3).FindControl("ddType"), Web.UI.WebControls.DropDownList).SelectedIndex
         Else
          strURL = strURL & "," & dgInsects.Items(i).Cells(0).Text
          strdd = strdd & "," & DirectCast(dgInsects.Items(i).Cells(3).FindControl("ddType"), Web.UI.WebControls.DropDownList).SelectedIndex
         End If
         k += 1
       End If
     Next
 
Isadore, thanks for the post. I have done a few searches (here and on Google) but to no avail. I'll take a look at this DirectCast command as well as see if I find a FindControl command for the Form/Page.

Certainly if others could give their insight, I'd appreciate it. So far, I'm doing it the long way.

Thanks
Xcalibre
 
Ok... Found it... the FindControl was the clue I needed. Then a few examples/searches later, here's the working code (in case anyone else needs it).

Assuming you've already done a datareader.read (my rdr):
Code:
dim x as integer
for x = 0 to rdr.FieldCount - 1
    Dim c as Checkbox
    If rdr.GetName(x).StartsWith("chk") Then
        c = CType(Page.FindControl(rdr.GetName(x), Checkbox)
        c.Checked = CBool(rdr(x))
    End If
Next x

Thanks for your help.
Xcalibre
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top