How do I loop this

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?aUhhdkFRdWVzdGlvbg==?=

    #1

    How do I loop this

    I actually need to do the below for 10 controls

    ddlInsuranceCom pany1.SelectedI tem.Text = Value;
    txtPolicy1.Text = value;

    SO i opted for a for loop

    My question is how do i add contol + increment
    ddlInsuranceCom pany + integer

    The below code does do not work can anyone plz hep me out

    for (int iCount = 0; iCount < 10; iCount++)
    {
    int iControls = iCount + 1;
    ddlInsuranceCom pany + (iControls).Sel ectedItem.Text
    = ilist.Tables[0].Rows[iCount]["InsurComp_TEXT "].ToString();
    txtPolicy+ (iControls).Tex t =
    ilist.Tables[0].Rows[iCount]["PoliciesNum_IN T"].ToString();
    }
  • Scott M.

    #2
    Re: How do I loop this

    You can't concatenate a number to a control name to come up with a control
    name that will be recognized by the compiler, since the compiler checks your
    variable names before the concatenation happens.

    Instead, put all the controls into a collection (like a panel) and loop
    through the controls in the panel. You can then check the type of control
    you're iterating over to know whether it's a textbox or a dropdown list.

    You'd use a foreach loop to accomplish this.

    -Scott

    "iHavAQuest ion" <iHavAQuestion@ discussions.mic rosoft.comwrote in message
    news:2A0FBE88-3190-45DA-9E41-0BE0E1F54574@mi crosoft.com...
    >I actually need to do the below for 10 controls
    >
    ddlInsuranceCom pany1.SelectedI tem.Text = Value;
    txtPolicy1.Text = value;
    >
    SO i opted for a for loop
    >
    My question is how do i add contol + increment
    ddlInsuranceCom pany + integer
    >
    The below code does do not work can anyone plz hep me out
    >
    for (int iCount = 0; iCount < 10; iCount++)
    {
    int iControls = iCount + 1;
    ddlInsuranceCom pany + (iControls).Sel ectedItem.Text
    = ilist.Tables[0].Rows[iCount]["InsurComp_TEXT "].ToString();
    txtPolicy+ (iControls).Tex t =
    ilist.Tables[0].Rows[iCount]["PoliciesNum_IN T"].ToString();
    }

    Comment

    • =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=

      #3
      Re: How do I loop this

      iHavAQuestion wrote:
      I actually need to do the below for 10 controls
      >
      ddlInsuranceCom pany1.SelectedI tem.Text = Value;
      txtPolicy1.Text = value;
      >
      SO i opted for a for loop
      >
      My question is how do i add contol + increment
      ddlInsuranceCom pany + integer
      >
      The below code does do not work can anyone plz hep me out
      >
      for (int iCount = 0; iCount < 10; iCount++)
      {
      int iControls = iCount + 1;
      ddlInsuranceCom pany + (iControls).Sel ectedItem.Text
      = ilist.Tables[0].Rows[iCount]["InsurComp_TEXT "].ToString();
      txtPolicy+ (iControls).Tex t =
      ilist.Tables[0].Rows[iCount]["PoliciesNum_IN T"].ToString();
      }
      You can just put the references in arrays, so that you can access them
      by index:

      DropDownList[] dropdowns = new DropDownList[] { ddlInsuranceCom pany1,
      ddlInsuranceCom pany2, ..., ddlInsuranceCom pany10 };
      TextBox[] textboxes = new TextBox[] { txtPolicy1, txtPolicy2, ... ,
      txtPolicy10 };
      for (int i = 0; i < 10; i++) {
      dropdowns[i].SelectedItem.T ext =
      ilist.Tables[0].Rows[i]["InsurComp_TEXT "].ToString();
      textboxes[i].Text =
      ilist.Tables[0].Rows[i]["PoliciesNum_IN T"].ToString();
      }

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      • Munna

        #4
        Re: How do I loop this

        Hi

        have use another trick to do such kind of things..
        I keep all those controls in a dictionary for just keep safe
        reference...
        Like

        Dctionary<strin g,dropdownlistm ydictionaryddl = new
        Dctionary<strin g,dropdownlist> ();
        Dctionary<strin g,textboxmydict ionarytxt = new
        Dctionary<strin g,textbox>();


        and add all the dropdown and textbox in their respective
        dictionary...
        dictionary key can be the integer value of the controls...
        after that when access needed

        I used

        for(int i=0; i <mydictionarydd l .Keys.Count;i++ ;)
        {

        Dropdown ddl = ( Dropdown ) mydictionaryddl[i];
        TextBox txt = (Textbox)mydict ionarytxt[i];
        //perfrom operation

        }

        Best of luck

        Munna



        Comment

        Working...