Please HELP!!!! FindControl problem

Results 1 to 2 of 2

Thread: Please HELP!!!! FindControl problem

  1. #1
    Join Date
    Dec 1969

    Default Please HELP!!!! FindControl problem

    Hi I have a page that loops through a database and creates some htmlselect boxes dynamically. The code adds a number to the end of each id (ie id=select0, id=select1, etc.)<BR><BR>When the user clicks a button, we want to do a bulk update and loop through and get the values from each select. <BR><BR>So I have code like this in the onclick event of the button.<BR><BR>for (int i=0;i&#060;s.Length;i++)<BR>{<BR> string sPriority = "cboPriority" + i.ToString();<BR> //sPriority should = Priority0, Priority1, etc.<BR> HtmlSelect cPriority = (HtmlSelect)this.FindControl("sPriority");<BR> Response.Write (cPriority.Value.ToString());<BR> <BR>}<BR><BR>I keep getting a Object reference no set to an instance of an object error.<BR><BR>The select box does have runat=server set.<BR><BR>&#060;select runat=&#039;server&#039; id=&#039;cboPriority0&#039; name=&#039;cboPriority0&#039;&#062;<BR><BR>Any ideas? This is code that was written a long time ago and I&#039;d like to save myself having to write the entire page just to add this one little feature. <BR><BR>Any help would be greatly appreciated.<BR><BR>Much Thanks<BR><BR>Rhonda

  2. #2
    Join Date
    Dec 1969

    Default Understanding variables...

    *ANY* time you use something that is inside quotes, you are getting *EXACTLY* what is in the quotes. Period.<BR><BR>So when you do <BR> this.FindControl("sPriority")<BR>you are looking *ONLY* for a control named<BR> sPriority<BR><BR>The fact that your string happens to *contain* a value that coincidentally matches the name of some variable in your code matters not a bit. <BR><BR>If you want to use a varying value in FindControl (or in any other place in future code), you can NOT use a quoted string. Look at the term: "variable". Something that varies.<BR><BR> string sPriority = "cboPriority" + i.ToString();<BR> // sPriority should = Priority0, Priority1, etc.<BR> // NO, it will NOT be. It will be something like<BR> // cboPriority0<BR> // or cboPriority1<BR> // etc. That&#039;s how you coded it.<BR> // And then *USE* that variable, with its cboPriorityNN value, <BR> // instead of a literal string:<BR> HtmlSelect cPriority = (HtmlSelect)this.FindControl(sPriority);<BR><BR>Not to ask too fine a question, but if you are the real newbie to programming that you seem to be, why are you tackling C# first??<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts