Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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
为什么可以';t在C#中向窗体添加动态标签?_C#_Asp.net - Fatal编程技术网

为什么可以';t在C#中向窗体添加动态标签?

为什么可以';t在C#中向窗体添加动态标签?,c#,asp.net,C#,Asp.net,当我运行这段代码时,它告诉我Controls.Add(l)接受无效的参数,它应该接受Control类型的参数 我看到您在问题上添加了“asp.net”标记,因此我假设您所说的是asp.net Web表单,即使您在问题中没有指定它。 在这种情况下,您应该参考 int cnt = 1; public System.Windows.Forms.Label addLabel(string s,int i) { System.Windows.Forms.Label

当我运行这段代码时,它告诉我
Controls.Add(l)
接受无效的参数,它应该接受Control类型的参数

我看到您在问题上添加了“asp.net”标记,因此我假设您所说的是asp.net Web表单,即使您在问题中没有指定它。 在这种情况下,您应该参考

 int cnt = 1;
    public System.Windows.Forms.Label addLabel(string s,int i)
    {



        System.Windows.Forms.Label l = new System.Windows.Forms.Label();
        l.Top = i;
        l.Name = ""+cnt;
        Controls.Add(l);
        cnt++;
        return l;


    }
命名空间(而不是System.Windows.Forms),在您的示例中可能返回LiteralControl。
我希望这会有帮助。

这真的是asp.net吗?使用,你想使用哪个继承了正确命名空间中的
控件
类,而不是。@Pluto,好的,现在它可以工作了,但是你知道如何放置它吗,我想一个接一个地添加几个标签,但我希望它们在不同的行中,而不是在同一行中?
System.Web.UI