Can I use an array in findcontrol?

Hello I have a DataList that has four lables and they all end with a number 1-4. In my code behind I have a for loop and an array that I want to set the labels of the Datalist with.

for (int x = 0; x< cartreciept.Items.Count; x++)
{
    DataListItem item = cartreceipt.Items[x];
    string catalogtype = ("select CatalogType From SC Where OrNum=" + OrNum)

    if (catalogtype="TC")
    {    
         ((Panel)item.FindControl("pnlIprintInfo")).Visible = true;
         string scRID = ("Select SCRID From SC Where OrNum =" + OrNum

        for(int y = 1; y<5; y++)
        {
              string lT[y] = (Select LineText From table Where SCartRD =" + scRID + " AND LN=" + y)

              ((Label)item.FindControl("lbl[y]")).text = lT[y];
        }
     }
}

So would the ((Label)item.FindControl("lbl[y]")) work? most of this code is just pseudo code until I figure the details out. If you need to know anything else I can provide what needs to be known, I am open to other suggestions as well. Thank you to any one who can offer some help.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can use this code – based on string.Format("....{0}",arguments)

var control = (Label)item.FindControl(string.Format("lbl{0}",y));

I suggest you to use DataList.ItemDataBound

Link : http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.datalist.itemdatabound.aspx


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x