Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在asp.net页面中查找标签控件_C#_Asp.net_Label_Findcontrol - Fatal编程技术网

C# 如何在asp.net页面中查找标签控件

C# 如何在asp.net页面中查找标签控件,c#,asp.net,label,findcontrol,C#,Asp.net,Label,Findcontrol,我在一个面板中有一些标签控件,id为ResultsPanel。要查找页面上的标签控件,我执行了以下操作: 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

我在一个面板中有一些标签控件,id为
ResultsPanel
。要查找页面上的标签控件,我执行了以下操作:

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();
for(int-ctr=0;ctr
lblString
是一个具有24个数字的
stringBuilder
对象。其思想是以以下方式将stringbuilder对象中的每个数字映射到标签:


lblResult1.Text=lblString[mylbl].ToString()到第24个标签。但我似乎无法生成标签并将值映射到label控件。

您的问题和代码有点误导,但无论如何我都会尽力

您的代码:

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();
for(int-ctr=0;ctr
如果标签的顺序为label1、label2、label3。。。然后我会这样写:

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;
}
private void映射标签()
{
var labelsResult=string.Empty;
var labelString=“123456789”;
for(int i=0;i
如果卡住了,请告诉我。

将线路更改为

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

希望这对你有帮助。这样你也可以得到标签的值

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;
    }
}
public void getControlsValue填充(控件控件,类型controlType,字典dictControlPageValParam)
{
尝试
{
if(control.HasControls())
{
GetControlsValue填充(控件、控件类型、dictControlPageValParam);
}
其他的
{
if(controlType==null | | controlType.IsAssignableFrom(control.GetType()))
{
if(control.ID!=null)
{
bool FoundControl=dictControlPageValParam.ContainsKey(control.ID.Substring(3));
if(FoundControl)
{
开关(control.GetType().ToString())
{
案例“System.Web.UI.WebControls.TextBox”:
TextBox txt=(TextBox)控件;
txt.Text=dictControlPageValParam[txt.ID.Substring(3)];
打破
案例“System.Web.UI.WebControls.CheckBox”:
复选框chk=(复选框)控件;
if(dictControlPageValParam[chk.ID.Substring(3)].ToUpper()=“TRUE”| dictControlPageValParam[chk.ID.Substring(3)].ToUpper()=“T”)
{
chk.Checked=真;
}
其他的
{
chk.Checked=假;
}
打破
案例“System.Web.UI.WebControl.DropDownList”:
DropDownList ddl=(DropDownList)控件;
//ddl.SelectedValue=dictControlPageValParam[ddl.ID.Substring(3)];
打破
案例“System.Web.UI.WebControl.RadioButtonList”:
RadioButtonList rbl=(RadioButtonList)对照;
rbl.SelectedValue=dictControlPageValParam[rbl.ID.Substring(3)];
打破
案例“System.Web.UI.WebControls.HiddenField”:
HiddenField hdn=(HiddenField)控件;
hdn.Value=dictControlPageValParam[hdn.ID.Substring(3)];
打破
}
}
}
}
}
}
捕获(例外)
{
投掷;
}
}
public void GetChildControlsId(控件控件,类型controlType)
{
尝试
{
if(control.HasControls())
{
GetChildControlsId(控件,控件类型);
}
其他的
{
if(controlType==null | | controlType.IsAssignableFrom(control.GetType()))
{
///检查集合中是否已存在控件
if(control.ID!=null)
{
bool FoundControl=controlHt.ContainsKey(control.ID);
如果(!FoundControl)
{
开关(control.GetType().ToString())
{
案例“System.Web.UI.WebControls.TextBox”:
TextBox txt=(TextBox)控件;
controlTypeName=txt.ID;
controlTypeName=controlTypeName.Substring(3);
controlTypeValue=txt.Text;
if(dictControlValParam.ContainsKey(controlTypeName)==false)
{
添加(controlTypeName,controlTypeValue);
}
其他的
{
dictControlValParam.Remove(controlTypeName);
dictControlValP