How to find label controls in an asp.net page

18.1k views Asked by At

I have some label controls in a panel with id ResultsPanel. To find the label controls on the page, I did the following:

for (int ctr = 0; ctr < lblString.Length - 1; ctr++)
{
    //string labelID = string.Format("lblResult{0}", ctr);
    int mylbl = ctr + 1;
    string lblResult = ((Label)ResultsPanel.FindControl("lblResult" + mylbl.ToString())).Text;
    lblResult = lblString[mylbl].ToString();
}
lblResult1.Text = lblString.ToString();

lblString is a stringBuilder Object with 24 numbers. The Idea is to map each of the numbers in the stringbuilder object to labels in this manner:

lblResult1.Text = lblString[mylbl].ToString(); to the 24th label. But I can't seem to generate the labels and map the values to the label control.

3

There are 3 answers

0
Naveen On BEST ANSWER

Change the line to

Label lblResult = ((Label)ResultsPanel.FindControl("lblResult" + mylbl.ToString()));
        lblResult.Text = lblString[mylbl].ToString();
1
Alok On

Your question and code are a bit misleading, but I will try my best anyway.

Your code:

for (int ctr = 0; ctr < lblString.Length - 1; ctr++)
{
    //string labelID = string.Format("lblResult{0}", ctr);
    int mylbl = ctr + 1;
    string lblResult = ((Label)ResultsPanel.FindControl("lblResult" + mylbl.ToString())).Text;
    lblResult = lblString[mylbl].ToString();
}

lblResult1.Text = lblString.ToString();

If you have labels in the order of label1, label2, label3 ... then I would write something like this:

private void MapLabels()
{
    var labelsResult = string.Empty;
    var labelString = "123456789";
    for (int i = 0; i < labelString.Length; i++)
    {
        var control = this.FindControl("label" + labelString[i]);
        if(control != null)
        {
            labelsResult += ((Label)control).Text;
        }
    }

    labelResult1.Text = labelsResult;
}

Let me know if you get stuck.

1
Kumod Singh On

Hope this will help you. Like this you can get the value of label too.

public void GetControlsValuePopulated(Control control, Type controlType, Dictionary<string, string> dictControlPageValParam)
{
    try
    {

        if (control.HasControls())
        {
            GetControlsValuePopulated(control, controlType, dictControlPageValParam);
        }
        else
        {
            if (controlType == null || controlType.IsAssignableFrom(control.GetType()))
            {
                if (control.ID != null)
                {
                    bool FoundControl = dictControlPageValParam.ContainsKey(control.ID.Substring(3));
                    if (FoundControl)
                    {
                        switch (control.GetType().ToString())
                        {
                            case "System.Web.UI.WebControls.TextBox":
                                TextBox txt = (TextBox)control;
                                txt.Text = dictControlPageValParam[txt.ID.Substring(3)];
                                break;

                            case "System.Web.UI.WebControls.CheckBox":
                                CheckBox chk = (CheckBox)control;
                                if (dictControlPageValParam[chk.ID.Substring(3)].ToUpper() == "TRUE" || dictControlPageValParam[chk.ID.Substring(3)].ToUpper() == "T")
                                {
                                    chk.Checked = true;
                                }
                                else
                                {
                                    chk.Checked = false;
                                }
                                break;

                            case "System.Web.UI.WebControls.DropDownList":
                                DropDownList ddl = (DropDownList)control;
                                //ddl.SelectedValue = dictControlPageValParam[ddl.ID.Substring(3)];
                                break;

                            case "System.Web.UI.WebControls.RadioButtonList":
                                RadioButtonList rbl = (RadioButtonList)control;
                                rbl.SelectedValue = dictControlPageValParam[rbl.ID.Substring(3)];
                                break;

                            case "System.Web.UI.WebControls.HiddenField":
                                HiddenField hdn = (HiddenField)control;
                                hdn.Value = dictControlPageValParam[hdn.ID.Substring(3)];
                                break;
                        }
                    }
                }

            }
        }
    }
    catch (Exception)
    {
        throw;
    }
}

public void GetChildControlsId(Control control, Type controlType)
{
    try
    {

        if (control.HasControls())
        {
            GetChildControlsId(control, controlType);
        }
        else
        {
            if (controlType == null || controlType.IsAssignableFrom(control.GetType()))
            {
                ///checking if control already existing in the collection
                if (control.ID != null)
                {
                    bool FoundControl = controlHt.ContainsKey(control.ID);

                    if (!FoundControl)
                    {
                        switch (control.GetType().ToString())
                        {
                            case "System.Web.UI.WebControls.TextBox":
                                TextBox txt = (TextBox)control;
                                controlTypeName = txt.ID;
                                controlTypeName = controlTypeName.Substring(3);
                                controlTypeValue = txt.Text;
                                if (dictControlValParam.ContainsKey(controlTypeName) == false)
                                {
                                    dictControlValParam.Add(controlTypeName, controlTypeValue);
                                }
                                else
                                {
                                    dictControlValParam.Remove(controlTypeName);
                                    dictControlValParam.Add(controlTypeName, controlTypeValue);
                                }
                                break;


                            case "System.Web.UI.WebControls.CheckBox":
                                CheckBox chk = (CheckBox)control;
                                controlTypeName = chk.ID;
                                controlTypeName = controlTypeName.Substring(3);
                                controlTypeValue = chk.Checked.ToString();
                                if (dictControlValParam.ContainsKey(controlTypeName) == false)
                                {
                                    dictControlValParam.Add(controlTypeName, controlTypeValue);
                                }
                                else
                                {
                                    dictControlValParam.Remove(controlTypeName);
                                    dictControlValParam.Add(controlTypeName, controlTypeValue);
                                }
                                break;
                            case "System.Web.UI.WebControls.DropDownList":
                                DropDownList ddl = (DropDownList)control;
                                controlTypeName = ddl.ID;
                                controlTypeName = controlTypeName.Substring(3);
                                controlTypeValue = ddl.SelectedValue.ToString();
                                if (dictControlValParam.ContainsKey(controlTypeName) == false)
                                {
                                    dictControlValParam.Add(controlTypeName, controlTypeValue);
                                }
                                else
                                {
                                    dictControlValParam.Remove(controlTypeName);
                                    dictControlValParam.Add(controlTypeName, controlTypeValue);
                                }
                                break;
                            case "System.Web.UI.WebControls.RadioButtonList":
                                RadioButtonList rbl = (RadioButtonList)control;
                                controlTypeName = rbl.ID;
                                controlTypeName = controlTypeName.Substring(3);
                                controlTypeValue = rbl.SelectedValue.ToString();
                                if (dictControlValParam.ContainsKey(controlTypeName) == false)
                                {
                                    dictControlValParam.Add(controlTypeName, controlTypeValue);
                                }
                                else
                                {
                                    dictControlValParam.Remove(controlTypeName);
                                    dictControlValParam.Add(controlTypeName, controlTypeValue);
                                }
                                break;

                            case "System.Web.UI.WebControls.HiddenField":
                                HiddenField hdn = (HiddenField)control;
                                controlTypeName = hdn.ID;
                                controlTypeName = controlTypeName.Substring(3);
                                controlTypeValue = hdn.Value;
                                if (dictControlValParam.ContainsKey(controlTypeName) == false)
                                {
                                    dictControlValParam.Add(controlTypeName, controlTypeValue);
                                }
                                else
                                {
                                    dictControlValParam.Remove(controlTypeName);
                                    dictControlValParam.Add(controlTypeName, controlTypeValue);
                                }
                                break;
                        }

                    }

                }

            }
        }
    }
    catch (Exception)
    {
        throw;
    }
}