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占位符_C#_Asp.net - Fatal编程技术网

C# 以编程方式正确引用ASP占位符

C# 以编程方式正确引用ASP占位符,c#,asp.net,C#,Asp.net,我有下面的代码,但它正在按预期工作;选择单选按钮后,我希望CheckChanged事件向StatusBar占位符添加控件。如何从事件引用此控件?如有任何反馈,将不胜感激 public void Page_Load(object sender, EventArgs e) { for (int T = 0; T <= 26; T++) { Label x = new Label();

我有下面的代码,但它正在按预期工作;选择单选按钮后,我希望CheckChanged事件向StatusBar占位符添加控件。如何从事件引用此控件?如有任何反馈,将不胜感激

    public void Page_Load(object sender, EventArgs e)
    {

            for (int T = 0; T <= 26; T++)
            {
                Label x = new Label();
                x.ID = T.ToString();
                x.Text = "orem ipsum dolor sit amet, consectetur adipiscing elit. Nulla blandit id felis ac volutpat. Aenean tempor faucibus est, ac feugiat libero egestas sit amet. Aliquam";

                Label y = new Label();
                y.ID = "Title_" + T.ToString();
                y.Text = "Title " + T.ToString();



                RadioButton Radio1 = new RadioButton();
                Radio1.ID = "R_" + T.ToString();
                Radio1.Text = "Yes";
                Radio1.GroupName = "Radio_" + T.ToString();
                Radio1.CheckedChanged += new EventHandler(this.CheckedChanged);
                Radio1.AutoPostBack = true;

                RadioButton Radio2 = new RadioButton();
                Radio2.ID = "RX_" + T.ToString();
                Radio2.Text = "No";
                Radio2.GroupName = "Radio_" + T.ToString();
                Radio2.CheckedChanged += new EventHandler(this.CheckedChanged);
                Radio2.AutoPostBack = true;

                PlaceHolder StatusBar = new PlaceHolder();
                StatusBar.ID = "status_" + T.ToString();


                PlaceHolder pcl = new PlaceHolder();
                pcl.ID = "test_" + T.ToString();
                pcl.Controls.Add(y);
                pcl.Controls.Add(new LiteralControl("<br>"));
                pcl.Controls.Add(x);
                pcl.Controls.Add(new LiteralControl("<br>"));
                pcl.Controls.Add(Radio1);
                pcl.Controls.Add(Radio2);
                pcl.Controls.Add(new LiteralControl("<br><br>"));
                form1.Controls.Add(pcl);
                form1.Controls.Add(StatusBar);
            }
        }


    protected void CheckedChanged(object sender, EventArgs e)
    {
        RadioButton tRadio = (RadioButton)sender;

        if (tRadio.Text == "Yes")
        {
            Statusbar.Controls.Add(somecontrol);
        }


    }
public void页面加载(对象发送方,事件参数e)
{

对于(int T=0;T您应该使用
FindControl
来查找状态栏控件。如果您将
StatusBar
的类型从
占位符更改为“面板”,您的问题将得到解决

更新:您不需要使用
面板
控件而不是
占位符
,而是
占位符
生成标记,这有助于您自定义状态栏的外观。我更改了
CheckedChanged
处理程序以查找状态栏控件并向其添加文字文本

检查以下代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        for (int T = 0; T <= 26; T++)
        {
            Label x = new Label();
            x.ID = T.ToString();
            x.Text = "orem ipsum dolor sit amet, consectetur adipiscing elit. Nulla blandit id felis ac volutpat. Aenean tempor faucibus est, ac feugiat libero egestas sit amet. Aliquam";

            Label y = new Label();
            y.ID = "Title_" + T.ToString();
            y.Text = "Title " + T.ToString();



            RadioButton Radio1 = new RadioButton();
            Radio1.ID = "R_" + T.ToString();
            Radio1.Text = "Yes";
            Radio1.GroupName = "Radio_" + T.ToString();
            Radio1.CheckedChanged += new EventHandler(this.CheckedChanged);
            Radio1.AutoPostBack = true;

            RadioButton Radio2 = new RadioButton();
            Radio2.ID = "RX_" + T.ToString();
            Radio2.Text = "No";
            Radio2.GroupName = "Radio_" + T.ToString();
            Radio2.CheckedChanged += new EventHandler(this.CheckedChanged);
            Radio2.AutoPostBack = true;

            Panel StatusBar = new Panel();

            StatusBar.ID = "status_" + T.ToString();


            PlaceHolder pcl = new PlaceHolder();
            pcl.ID = "test_" + T.ToString();
            pcl.Controls.Add(y);
            pcl.Controls.Add(new LiteralControl("<br>"));
            pcl.Controls.Add(x);
            pcl.Controls.Add(new LiteralControl("<br>"));
            pcl.Controls.Add(Radio1);
            pcl.Controls.Add(Radio2);
            pcl.Controls.Add(new LiteralControl("<br><br>"));
            form1.Controls.Add(pcl);
            form1.Controls.Add(StatusBar);
        }

        //form1.Controls.Add(new PlaceHolder { ID = "MyStatusBar" });
    }

    protected void CheckedChanged(object sender, EventArgs e)
    {
        RadioButton tRadio = (RadioButton)sender;
        var T = tRadio.ID.Split('_')[1];

        var statusBarID = "status_" + T;

        var StatusBar = tRadio.Parent.FindControl(statusBarID) as Panel;

        if (tRadio.Text == "Yes")
        {
            StatusBar.Controls.Add(new LiteralControl("SampleText"));
        }


    }
受保护的无效页面加载(对象发送方,事件参数e)
{

对于(int T=0;T您应该使用
FindControl
来查找状态栏控件。如果您将
StatusBar
的类型从
占位符更改为“面板”,您的问题将得到解决

更新:您不需要使用
面板
控件而不是
占位符
,而是
占位符
生成标记,这有助于您自定义状态栏的外观。我更改了
CheckedChanged
处理程序以查找状态栏控件并向其添加文字文本

检查以下代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        for (int T = 0; T <= 26; T++)
        {
            Label x = new Label();
            x.ID = T.ToString();
            x.Text = "orem ipsum dolor sit amet, consectetur adipiscing elit. Nulla blandit id felis ac volutpat. Aenean tempor faucibus est, ac feugiat libero egestas sit amet. Aliquam";

            Label y = new Label();
            y.ID = "Title_" + T.ToString();
            y.Text = "Title " + T.ToString();



            RadioButton Radio1 = new RadioButton();
            Radio1.ID = "R_" + T.ToString();
            Radio1.Text = "Yes";
            Radio1.GroupName = "Radio_" + T.ToString();
            Radio1.CheckedChanged += new EventHandler(this.CheckedChanged);
            Radio1.AutoPostBack = true;

            RadioButton Radio2 = new RadioButton();
            Radio2.ID = "RX_" + T.ToString();
            Radio2.Text = "No";
            Radio2.GroupName = "Radio_" + T.ToString();
            Radio2.CheckedChanged += new EventHandler(this.CheckedChanged);
            Radio2.AutoPostBack = true;

            Panel StatusBar = new Panel();

            StatusBar.ID = "status_" + T.ToString();


            PlaceHolder pcl = new PlaceHolder();
            pcl.ID = "test_" + T.ToString();
            pcl.Controls.Add(y);
            pcl.Controls.Add(new LiteralControl("<br>"));
            pcl.Controls.Add(x);
            pcl.Controls.Add(new LiteralControl("<br>"));
            pcl.Controls.Add(Radio1);
            pcl.Controls.Add(Radio2);
            pcl.Controls.Add(new LiteralControl("<br><br>"));
            form1.Controls.Add(pcl);
            form1.Controls.Add(StatusBar);
        }

        //form1.Controls.Add(new PlaceHolder { ID = "MyStatusBar" });
    }

    protected void CheckedChanged(object sender, EventArgs e)
    {
        RadioButton tRadio = (RadioButton)sender;
        var T = tRadio.ID.Split('_')[1];

        var statusBarID = "status_" + T;

        var StatusBar = tRadio.Parent.FindControl(statusBarID) as Panel;

        if (tRadio.Text == "Yes")
        {
            StatusBar.Controls.Add(new LiteralControl("SampleText"));
        }


    }
受保护的无效页面加载(对象发送方,事件参数e)
{

对于(int T=0;T)我无法准确地找出您的问题所在?还有什么是somecontrol?somecontrol只是一个占位符。假设我将该行更改为StatusBar.Controls.Add(new LiteralControl(
“>);。我收到一个错误,指出“StatusBar在当前上下文中不存在”。我只是能够通过在Page_Load事件之外添加占位符StatusBar;使其正常工作。还有其他方法吗?占位符控件的目的是在页面中保持一个点处于打开状态,以便您可以在代码隐藏中有条件地向其添加控件。在代码隐藏和加载事件中创建占位符没有实际意义然后将它们添加到表单中。将StatusBar占位符放在.aspx表单中比动态创建它更有意义。我希望能够开发一个应用程序,该应用程序将循环处理来自数据库的26个问题,并且在循环的每个迭代底部都有一个占位符,可以通过n事件。这就是我试图以编程方式添加它们的原因。我无法确切了解您的问题是什么?还有somecontrol?somecontrol只是一个占位符。假设我将该行更改为StatusBar.Controls.add(new LiteralControl(
“>);。我收到一个错误,指出“StatusBar在当前上下文中不存在”。我只是能够通过在Page_Load事件之外添加占位符StatusBar;使其正常工作。还有其他方法吗?占位符控件的目的是在页面中保持一个点处于打开状态,以便您可以在代码隐藏中有条件地向其添加控件。在代码隐藏和加载事件中创建占位符没有实际意义然后将它们添加到表单中。将StatusBar占位符放在.aspx表单中比动态创建它更有意义。我希望能够开发一个应用程序,该应用程序将循环处理来自数据库的26个问题,并且在循环的每个迭代底部都有一个占位符,可以通过n事件。这就是为什么我试图以编程方式添加它们。