Telerik 控件中的控件无法从 .cs 文件访问

发布于 2024-09-30 03:22:19 字数 1081 浏览 0 评论 0原文

我在我的 c# asp.net 项目中使用 telerik 控件。我正在尝试从 .cs 文件禁用 telerik 导航菜单中的 div。例如:

        if (Emp_Role == "1" || Emp_Role == "5")
        {
            DivLeave.Visible = true;
        }

我尝试运行该项目,但收到此错误:

CS0103:当前上下文中不存在名称“DivLeave”

这是 aspx 代码的示例

<telerik:RadMenu runat="server" ID="RadMenu1" Skin="Sitefinity"   OnClientItemOpened="itemOpened"
            Width="670px" Height="26px" EnableShadows="true">
            <Items>
                <telerik:RadMenuItem Text="Expenses" PostBack="false">
                    <Items>
                        <telerik:RadMenuItem CssClass="Stores" Width="640px">
                            <ItemTemplate>
                                <div id="DivLeave" class="Wrapper">
                                    <h3>
                                        Expense Management</h3>
                                    </div>

有人可以帮忙吗?如果我将 div 放在 telerik 控件之外,它就可以正常工作。这太令人沮丧了!

亲切的问候,

R

I am using telerik controls in my c# asp.net project. I am trying to disable a div in a telerik navigation menu from the .cs file. For example:

        if (Emp_Role == "1" || Emp_Role == "5")
        {
            DivLeave.Visible = true;
        }

I try run the project I get this error:

CS0103: The name 'DivLeave' does not exist in the current context

Here is an example of the aspx code

<telerik:RadMenu runat="server" ID="RadMenu1" Skin="Sitefinity"   OnClientItemOpened="itemOpened"
            Width="670px" Height="26px" EnableShadows="true">
            <Items>
                <telerik:RadMenuItem Text="Expenses" PostBack="false">
                    <Items>
                        <telerik:RadMenuItem CssClass="Stores" Width="640px">
                            <ItemTemplate>
                                <div id="DivLeave" class="Wrapper">
                                    <h3>
                                        Expense Management</h3>
                                    </div>

Can anyone help with this? If I place the div outside the telerik control it works fine. This is so frustrating!

Kind regards,

R

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

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

发布评论

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

评论(2

む无字情书 2024-10-07 03:22:19

首先,您必须使用 asp.net 控件(或至少是在服务器中运行的控件)才能从后面的代码访问它。例如。

<asp:Label ID="DivLeave" runat="server"></asp:Label>

其次,要获得 Telerik 控件内的控件,您需要一些特殊代码。在您的示例中,您可以执行以下操作:

 // Find menuitem by css class
 RadMenuItem expenses = RadMenu1.FindItem(i => i.CssClass == "Stores");
 // Find control inside menuitem
 Label label = expenses.FindControl("DivLeave") as Label;
 label.Visible = true;

要了解更多信息:
访问模板内的控件

First, you have to use a asp.net control ( or at least a control that runs in server ) to be able to access it from code behind. For example.

<asp:Label ID="DivLeave" runat="server"></asp:Label>

Second, to get a control inside a Telerik control you need som special code. In your example, you can do something like this:

 // Find menuitem by css class
 RadMenuItem expenses = RadMenu1.FindItem(i => i.CssClass == "Stores");
 // Find control inside menuitem
 Label label = expenses.FindControl("DivLeave") as Label;
 label.Visible = true;

To learn more:
Accessing Controls Inside Templates

欢你一世 2024-10-07 03:22:19

在客户端执行此操作也可以工作,并且您不必使 div 成为服务器端。
使用 jQuery,您可以:

if (Emp_Role == "1" || Emp_Role == "5")
{
    Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "show_divleave", "$(function() {   $(\"div[id$='DivLeave']\").each(function(index) { $(this).css(\"display\", \"\"); }); });", true);
}

假设这些 div 元素最初使用 "display: none;" CSS 规则隐藏。

Doing it client side will also work and you won't have to make the div become server side.
Using jQuery you can have:

if (Emp_Role == "1" || Emp_Role == "5")
{
    Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "show_divleave", "$(function() {   $(\"div[id$='DivLeave']\").each(function(index) { $(this).css(\"display\", \"\"); }); });", true);
}

This assumes those div elements are initially hidden using "display: none;" CSS rule.

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