RowDatabound missing checkbox


D

DavidC

I have a GridView with a RowDatabound event that checks for a checked
checkbox but for some reason it is not finding it. I tried tracing the code
while debugging but it always came up false even on rows that have the
checkbox checked. Can anyone see what is wrong with the code? The last line
of "If ck.checked ..." is not finding the checked box. Thanks.

If e.Row.RowType = DataControlRowType.DataRow Then
Dim bolUpdate As Boolean = False
Dim dblClearedAmount As Double = 0
Dim ck As CheckBox = e.Row.FindControl("ckReconciled")
Dim tb As TextBox = e.Row.FindControl("txtClearedAmount")
Dim intCheckID As Int32 = _
Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "CheckID"))
Dim dblClearedAmountDB As Double = _
Convert.ToDouble(DataBinder.Eval(e.Row.DataItem,
"ClearedAmount"))
If ck.Checked Then
 
Ad

Advertisements

M

Mark Rae [MVP]

Can anyone see what is wrong with the code?

Have you tried finding the control from within its individual Cell rather
than in the entire Row? E.g.
e.Row.Cells(0).FindControl("chkReconciled")

It should not be necessary to do this, but I have noticed this behaviour in
the past.

If that doesn't work, check how many controls are actually in the Cell where
you think the CheckBox should be, e.g.
e.Row.Cells(0).Controls.Count

It may be that you need to drill down into one or more parent containers
before you arrive at the control you're interested in.
 
D

DavidC

That is the strange thing, it is finding the textbox but not finding the
value. Currently the CheckBox is bound to a datafield but I also tried with
an unbound checkbox and it still gave me a false value in RowDatabound. I
will try the Cells(n) option and see if that works.
 
D

DavidC

Mark Rae said:
Have you tried finding the control from within its individual Cell rather
than in the entire Row? E.g.
e.Row.Cells(0).FindControl("chkReconciled")

It should not be necessary to do this, but I have noticed this behaviour in
the past.

If that doesn't work, check how many controls are actually in the Cell where
you think the CheckBox should be, e.g.
e.Row.Cells(0).Controls.Count

It may be that you need to drill down into one or more parent containers
before you arrive at the control you're interested in.


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

.

The Cells(n) did not work either. What is more confusing is that if I view
the html source it shows the td cell as "checked". Not sure what else I can
try as I continue to be puzzled.

David
 
M

Mark Rae [MVP]

The Cells(n) did not work either. What is more confusing is that if I view
the html source it shows the td cell as "checked". Not sure what else I
can
try as I continue to be puzzled.

Set a breakpoint on the FindControl line and then in the immediate window
(or the locals / watch / autos window - whatever you like to use for
debugging) and enumerate and identify the controls.

E.g. if you are certain that the CheckBox is in the second Cell (including
those you might be hiding or whatever), do this:

e.Row.Cells(1).Controls.Count

How many are there?

Assuming there are some, check what type of control they are e.g.

e.Row.Cells(1).Controls(0).GetType()
 
D

DavidC

Mark Rae said:
Set a breakpoint on the FindControl line and then in the immediate window
(or the locals / watch / autos window - whatever you like to use for
debugging) and enumerate and identify the controls.

E.g. if you are certain that the CheckBox is in the second Cell (including
those you might be hiding or whatever), do this:

e.Row.Cells(1).Controls.Count

How many are there?

Assuming there are some, check what type of control they are e.g.

e.Row.Cells(1).Controls(0).GetType()

e.rows.cells(1) came back with count of 3 and the types came back as
Literal, Checkbox, Literal.

David
 
Ad

Advertisements

M

Mark Rae [MVP]

e.rows.cells(1) came back with count of 3 and the types came back as
Literal, Checkbox, Literal.

OK, so try to reference the second control directly. Not quite sure how you
do it in VB as I never go anywhere near it (is it called DirectCast, or
something similar), but in C# it would be:

bool blnChecked = ((CheckBox)e.Row.Cells[1].Controls[1]).Checked;

At any rate, cast / convert the second control in the second cell to a
CheckBox webcontrol object and inspect its Checked property.

This doesn't, of course, explain why FindControl isn't working, but should
at least get you back in business...
 
D

DavidC

Mark Rae said:
e.rows.cells(1) came back with count of 3 and the types came back as
Literal, Checkbox, Literal.

OK, so try to reference the second control directly. Not quite sure how you
do it in VB as I never go anywhere near it (is it called DirectCast, or
something similar), but in C# it would be:

bool blnChecked = ((CheckBox)e.Row.Cells[1].Controls[1]).Checked;

At any rate, cast / convert the second control in the second cell to a
CheckBox webcontrol object and inspect its Checked property.

This doesn't, of course, explain why FindControl isn't working, but should
at least get you back in business...
Yes, it is DirectCast and I have never used it but will give it a try. I
added code in the RowDatabound to accumulate the values.tostring in a textbox
during postback and the odd thing is that is stays the same. I get a value
the first time the GridView is databound, but not after a postback. I have
never run into this before and have done similar things with a GridView in
the past. I also thought that having the GridView inside a Panel would
matter, but I even tried removing the panel and the results were the same.
If DirectCast does not solve it then I may have to go to a ListView control
and use something I know works. Either that or have a LinkButton with the
record ID attached and do something in the RowCommand. I intend to
incorporate Ajax for this but it needs to work without Ajax first, right?

David
 
Ad

Advertisements

M

Mark Rae [MVP]

Yes, it is DirectCast and I have never used it but will give it a try.

I'm interested to know what happens...
I intend to incorporate Ajax for this but it needs to work without Ajax
first, right?

I suppose so, though very often things which work "normally" don't work with
AJAX without modification...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top