查找动态创建的控件的客户端 ID?

发布于 2024-11-16 04:02:30 字数 580 浏览 4 评论 0原文

如何找到动态创建的控件的客户端 ID?

在我的 ascx 中,我有以下片段。

  function DoSomething() {
        var loneStar= $find("<%= loneStar.ClientID %>");
        loneStar.hide();
    }

在我后面的代码中,我遇到的

public partial class SomeControl: System.Web.UI.UserControl
    {
    protected Label loneStar = new Label { Text = "Raspeberry", ForeColor = System.Drawing.Color.DarkGray};

    private void someOtherMethod()
         {
         somePanel.Controls.Add(loneStar);
         }
    }

问题是渲染页面中的 ClientID 显示为空。

我在这里缺少什么?

How can I find the client ID of a dynamically created control?

In my ascx, I have the following snippet.

  function DoSomething() {
        var loneStar= $find("<%= loneStar.ClientID %>");
        loneStar.hide();
    }

In my code behind, I have

public partial class SomeControl: System.Web.UI.UserControl
    {
    protected Label loneStar = new Label { Text = "Raspeberry", ForeColor = System.Drawing.Color.DarkGray};

    private void someOtherMethod()
         {
         somePanel.Controls.Add(loneStar);
         }
    }

The problem is that the ClientID in the rendered page comes up as empty.

What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾冷 2024-11-23 04:02:30

需要给控件一个ID,否则不会生成ID属性。对您的 c# 进行更改,如下所示:

protected Label loneStar = new Label { ID = "loneStar", Text = "Raspeberry", ForeColor = System.Drawing.Color.DarkGray};

You need to give the control an ID, otherwise no ID attribute will be generated. Make a change to your c# that looks something like this:

protected Label loneStar = new Label { ID = "loneStar", Text = "Raspeberry", ForeColor = System.Drawing.Color.DarkGray};
烧了回忆取暖 2024-11-23 04:02:30

http://jagregory.com/writings/ how-to-use-clientids-in-javascript-without-the-uginess/

<script type="text/javascript">
   var txtUsernameID = '<%= txtUsername.ClientID %>';
   var txtPasswordID = '<%= txtPassword.ClientID %>';
 </script>

也许你应该检查一下,是否每次都使用相同的 id 动态添加控件

简单形式:

    <asp:Panel runat="server" ID="pnl"></asp:Panel>
    <script type="text/javascript">
        var txtUsernameID = '<%= myLabel.ClientID %>';

CodeBehind:

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AddedControl();
    }

    protected Label myLabel = new Label();
    void AddedControl()
    {
        myLabel.Text = "Labelll";
        this.pnl.Controls.Add(myLabel);
    }
}

Html 输出:

<span>Labelll</span>


<script type="text/javascript">
    var txtUsernameID = 'ctl02';   

http://jagregory.com/writings/how-to-use-clientids-in-javascript-without-the-ugliness/

<script type="text/javascript">
   var txtUsernameID = '<%= txtUsername.ClientID %>';
   var txtPasswordID = '<%= txtPassword.ClientID %>';
 </script>

Maybe you should check, if you are adding dynamically the control each time with the same id

Simple Form:

    <asp:Panel runat="server" ID="pnl"></asp:Panel>
    <script type="text/javascript">
        var txtUsernameID = '<%= myLabel.ClientID %>';

CodeBehind :

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AddedControl();
    }

    protected Label myLabel = new Label();
    void AddedControl()
    {
        myLabel.Text = "Labelll";
        this.pnl.Controls.Add(myLabel);
    }
}

Html Output:

<span>Labelll</span>


<script type="text/javascript">
    var txtUsernameID = 'ctl02';   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文