1

I'm trying to find a control in a page. The Id is available as a server control (CheckBox) This throws exception "not able to convert string to double"

Dim taskId As HtmlInputCheckBox
i =10
taskId = Me.FindControl("chkTaskOption_" + i)
taskId.Checked = True

Can any one tell me where i'm wrong.

jcolebrand
  • 15,889
  • 12
  • 75
  • 121
selvaraj
  • 889
  • 2
  • 16
  • 29

2 Answers2

3

Your problem is that you need to use & instead of + to concatenate two strings in VB.NET. Change this line:

taskId = Me.FindControl("chkTaskOption_" & i)

For further reading, there's a good discussion about this in the answers to this question.

Community
  • 1
  • 1
patmortech
  • 10,139
  • 5
  • 38
  • 50
  • I'm pretty sure it's the cast no? Sorry, just saw the "double" part – jcolebrand Jan 07 '11 at 04:44
  • Once the string part is resolved, I'm sure a cast will be needed to set the Checked to True. – s_hewitt Jan 07 '11 at 04:47
  • VB.NET does not actually require the explicit type conversion in the above situation -- it will work fine as long as the control it finds actually is an HtmlInputCheckBox (or some derivative of it). He's setting the Checked property on his local variable which is of the right type, so there will be no error on that line. – patmortech Jan 07 '11 at 04:50
  • Unless you have Option Strict turned on, that is (which it is not by default). Then you would need the explicit conversion. – patmortech Jan 07 '11 at 04:54
2

You might just be missing a cast of the type returned from FindControl. Or on the variable i. I can't remember if VB.net will convert for you.

i =10
Dim taskId As HtmlInputCheckBox
taskId = CType(Me.FindControl("chkTaskOption_" & i.ToString()), HtmlInputCheckBox)
taskId.Checked = True
s_hewitt
  • 4,252
  • 24
  • 24