Can't Find Control

jituvaiz

New Member
I have a login screen that sits in a DataGrid. I am trying to access the username and password textboxes and it is not turning out to be as easy as I thought. I am looping through the grid items as follows. I constantly get an error:<BR> "Object reference not set to an instance of an object"<BR><BR>Thing is I have a similar function where I look for a panel and it finds it just fine. I assure you all the web controls are named right and accounted for in the Class file.<BR>The error is occuring in the FOR loop when I try to FindControl.<BR>What am I doing wrong? Thanks...........M<BR><BR><BR>Sub Login2(sender as Object, E AS EventArgs)<BR> If Page.IsValid()<BR> Dim pwd as string<BR> Dim OrderID as Integer<BR> Dim uname as String<BR> Dim i as integer<BR> For i = 0 to showhunt.items.count-1<BR> uname = CTYPE(showhunt.items(i).FindControl("username"),TextBox).Text<BR> pwd = CTYPE(showhunt.items(i).FindControl("password"),TextBox).Text<BR> NEXT<BR>End If<BR>End SubCould it be that not all the items in your datagrid have the textbox... and the item on index 0 is returning null so you get the error... How about inside your for loop you do something like this...<BR><BR><BR>for i = 0 to showhunt.items.count - 1<BR> Dim uname as TextBox = showhunt.items(i).FindControl("username")<BR> Dim pwd as TextBox = showhunt.items(i).FindControl("password")<BR><BR> if not uname is nothing then ' found the username textbox <BR> ' set uname string... <BR> end if<BR> if not pwd is nothing then ' found password textbox<BR> ' set pwd string...<BR> end if<BR> <BR>next<BR><BR>I think there is an easier way to find the textbox... without looping through each item... not sure though.That could be exactly what the problem is however I only return <BR>showhunt.items.count returns 1. That's it. I tried what you have above and uname and pwd return nothing. They are not being found.<BR><BR>Would the way I am using the Grid have anything to do with it?<BR>I have the grid in ItemTemplates and I actually doubled it - meaning it is 2 cells wide.<BR><BR><ITEMTEMPLATE> <BR><TABLE><BR><TR><BR><TD>First Half of Grid</TD><BR><TD>Second Have of grid</TD><BR></TR><BR></TABLE><BR></ITEMTEMPLATE><BR><BR>I don't know where this would matter. I have a couple panels in here and I am able to turn them on and off with the same code.<BR><BR>Thanks.........M <BR>
 
Back
Top