您可以从后面的代码中为更新面板生成的 div 设置 css 类吗?

发布于 2024-08-09 12:42:31 字数 256 浏览 1 评论 0原文

如果我有一个 UpdatePanel

<asp:UpdatePanel ID="udPanel" runat="server">
            ...
</asp:UpdatePanel>

在渲染时创建这个 div

<div id="ctl00_udPanel">

你如何在后面的代码中引用它来动态更改 css 类?

If I have an UpdatePanel

<asp:UpdatePanel ID="udPanel" runat="server">
            ...
</asp:UpdatePanel>

creates this div when it's rendered

<div id="ctl00_udPanel">

How do you reference this in the code behind to change the css class dynamically?

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

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

发布评论

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

评论(4

不念旧人 2024-08-16 12:42:31

你可以做的是在 javascript 中写出 ClientID 客户端,然后使用带有 AddClass未经测试):

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script type="text/javascript">
    $("#<%=udPanel.ClientID%>").addClass("MyClass");
</script>

What you could do is write out the ClientID client-side within javascript and then use jQuery with an AddClass (untested):

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script type="text/javascript">
    $("#<%=udPanel.ClientID%>").addClass("MyClass");
</script>
岛徒 2024-08-16 12:42:31

不幸的是,UpdatePanel 被认为是浏览器不可见的抽象(但实际上并非如此)。所以你不能对其应用类名。

唯一的方法是将类名应用于 UpdatePanel 内的 div 或 ASP.NET Panel

Unfortunately, UpdatePanel is considered to be an abstraction that is invisible to the browser (but in reality it isn't). So you can't apply a classname to it.

The only way is to apply the classname to a div or an ASP.NET Panel inside the UpdatePanel.

摇划花蜜的午后 2024-08-16 12:42:31

在我的脑海里,尝试udPanel.Attributes

From the back of my mind, try udPanel.Attributes.

涫野音 2024-08-16 12:42:31

这是我能找到的最好的动态执行此操作的方法。您可以创建一个 JavaScript 函数来更改 css 类,该函数接受要更改的控件的 clientID 和 css 类的字符串作为参数。您可以将其设置为在主体的 onload() 方法中调用,动态传入 clientID 和 css 类。这使您能够更改页面不同状态的 css 类。

    HtmlGenericControl body = (HtmlGenericControl)uPanel.Page.Master.FindControl("masterBody");
    body.Attributes.Add("onload", "setCSSClass('" + uPanel.ClientID.ToString() + "', '" + cssClassName + "')");

Here's the best I could find to do this dynamically. You can create a javascript function to change the css class which accepts the clientID of the control to change and a string for the css class as parameters. You can set this to be called in the onload() method of the body dynamically passing in the clientID and the css class. This enables you to change the css class for different states of the page.

    HtmlGenericControl body = (HtmlGenericControl)uPanel.Page.Master.FindControl("masterBody");
    body.Attributes.Add("onload", "setCSSClass('" + uPanel.ClientID.ToString() + "', '" + cssClassName + "')");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文