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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP ... I'm in checkbox HECK!!! (didn't want to use the other word)

Status
Not open for further replies.

VBRookie

Programmer
May 29, 2001
331
US
I spent most of yesterday trying to figure this perplexing issue out that should be quite simple (SHOULD being the operative word).

I have a table that I'm generating in the code behind from a datatable. The table contains a bunch of checkboxes with the names of employees next to the them. The name of the employee is a linkbutton that navigates to another page (this works fine). I would've used a checkboxlist except I wasn't able to make the names links using that ... but anyway I digress ...

The problem is the checkboxes. I want certain checkboxes to be checked and others not according to information provided in the datatable. I figured that I would just used the .Checked property ... right?

My checkboxes have the id of cbx with the employeeID concatenated onto it (ex. cbx211, cbx987, etc). If I reference the checkbox this way it works:

ck = (CheckBox) FindControl ("cbx196");
ck.Checked = true;

If it reference it through the datatable it does not:

ck = (CheckBox) FindControl ("cbx" + projResData.Rows [0]["RESOURCE_ID"].ToString ());
ck.Checked = true;

When I step through the code projResData.Rows [0]["RESOURCE_ID"] comes out to be 196 so the id is correct as cbx196 but it will not check!!! Yet if I hard-code it ... IT WORKS!!!

Crazy! Yes! Or maybe I'm losing my mind. I have been scratching my head trying to figure this out for several hours now and have not been able to find much help on the web. Has anyone run into this before? If so ... PLEASE HELP!!!!

Desperately,
VBRookie

PS
Don't let the name fool you ... I'm coding this in C#.
 
not really a solution but a suggestion.
did you try to see what that expression is evaluating to ., may be u have spaces etc., while reading from db.
try trimming the string and then conct cbx.
projResData.Rows [0]"RESOURCE_ID".ToString ().Trim());
 
Yes I tried that and no spaces. From all outward appearances it evaluates to the correct control ID ... but alas, no check.

Thanks anyway though ...
 
i thinnk i run into somproblem like this before.

in my case, i was trying to find the control at worng place.
i.e , it was databounded and the contol could not be accessed in code untill the grid was in edit mode. and (Only)was accessabel only in the event/methods i specified for grid example:eek:nEditCommand , OnCancelCommand etc.,.,

i think the location as where you are trying to access hte control is also important.

hope this helps.
good lcuk
 
Thanks Serpico but I'm using a regular table webcontrol. I didn't use the datagrid for this one.

But thanks again ...

12 hours and counting ... and still no check [surprise]

- VB Rookie
 
I don't understand why should you do this
ck = (CheckBox) FindControl ("cbx" + projResData.Rows [0]"RESOURCE_ID".ToString ());
ck.Checked = true;
Assumed that you have your checkbox in your datagrid. Then the the exactly name of the checkbox. When you used ("cbx" + projResData.Rows [0]"RESOURCE_ID".ToString ()) that is not right. You should take a look at this:

Private Sub cmd_OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_OK.Click
Dim dg1 As DataGridItem
Dim chkPolis As CheckBox
Dim gvar_no as string

For Each dg1 In DataGrid1.Items
'*** The chk_Polis is the name of the textbox
chkPolis = dg1.FindControl("chk_Polis")
'chk_Polis is checkbox's name in datagrid
If chkPolis.Checked Then
'*** The row that which the textbox is located
gvar_No = Trim(dg1.Cells(1).Text)
End If
Next
End Sub


Hope it help :)
Huong,
 
Its not in a datagrid though. Its just a regular table. I didn't use a datagrid because of the way that I needed the data to display. I show each row in the datatable as a checkbox with the employees name as a link next to it. The checkboxes are displayed 4 to a row so 4 records are displayed in one row in the table.

I didn't know of a way to display the data like that using a datagrid because the datagrid displays 1 tablerow for each row in the datatable.

So I can't really use this logic but thanks anyway. I think that I just need to find another way to do it ... although I can't understand why it doesn't work!?

- VB Rookie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top