1

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.

Simon Martin
  • 4,203
  • 7
  • 56
  • 93
Guzzyman
  • 561
  • 6
  • 16
  • 37
  • What error/unexpected behaviour do you get? – Andrew Dec 20 '13 at 08:19
  • I dont't get any unexpected behaviour other than this line lblResult1.Text = lblString.ToString(); spitting all the values in the string. 211121211301202220310301 – Guzzyman Dec 20 '13 at 08:24
  • I'm expecting the values of the string to be passed to the labels like so: lblResult1.Text = lblString[0].ToString(); lblResult2.Text = lblString[1].ToString(); lblResult3.Text = lblString[2].ToString(); . . . lblResult24.Text = lblString[23].ToString(); But I don't want to parse the values manulayy...Thats why I have the loop in the above. – Guzzyman Dec 20 '13 at 08:32

3 Answers3

2

Change the line to

Label lblResult = ((Label)ResultsPanel.FindControl("lblResult" + mylbl.ToString()));
        lblResult.Text = lblString[mylbl].ToString();
Naveen
  • 1,496
  • 1
  • 15
  • 24
0

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.

Alok
  • 1,290
  • 1
  • 11
  • 21
0

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;
    }
}
MSDN.WhiteKnight
  • 664
  • 7
  • 30
Kumod Singh
  • 2,113
  • 1
  • 16
  • 18
  • Thanks @kumod-singh Yourcode too is very helpful. Would use this method when next I want to implement something of this nature. Once again, thanks. – Guzzyman Dec 30 '13 at 14:18