简体   繁体   English

使用服务器控件内的变量的asp.net Web表单

[英]asp.net web forms using variables inside a server control

I am trying to use a variable inside server control in asp.net webform page(.aspx). 我正在尝试在asp.net Webform页面(.aspx)的服务器控件中使用一个变量。 I am getting syntax error. 我收到语法错误。 What may be the issue? 可能是什么问题?

<%string msgCancelProject = "You are not authorized to cancel the project."; %> 
<asp:Button ID="CancelProject" <%if(IsAuthorized){%> title="<% =msgCancelProject %>" clickDisabled="disable" <%}%> runat="server" Text="Cancel Project" 
                     OnClick="btnCancelProject_Click" 
                    OnClientClick="return confirm('Are you certain you want to cancel the record?');" />

It's not possible to do what you are trying to do with a server control. 使用服务器控件无法完成您想做的事情。 Ie adding a property dynamically in markup. 即在标记中动态添加属性。 You can only set property values but that's not what you want. 您只能设置属性值,但这不是您想要的。

You could achieve what you want from the code behind as follows. 您可以按照下面的代码实现所需的功能。

Keep your markup like this. 保持这样的标记。

<asp:Button ID="CancelProject" runat="server" Text="Cancel Project" OnClick="btnCancelProject_Click" 
                    OnClientClick="return confirm('Are you certain you want to cancel the record?');" />

And, in your code behind do this. 并且,在您后面的代码中执行此操作。

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string msgCancelProject = "You are not authorized to cancel the project.";

            if (IsAuthorized)
            {
                CancelProject.Attributes.Add("title", msgCancelProject);
                CancelProject.Attributes.Add("clickDisabled", "disable"); // I'm not sure what you are trying to do here
            }
            else
            {
                CancelProject.Attributes.Remove("title");
                CancelProject.Attributes.Remove("clickDisabled");
            }
        }
    }

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ASP.NET Web窗体中,我想使用javascript在子Repeater中进行控制 - In ASP.NET Web Forms I want to get control inside child Repeater with javascript 如何使用ASP.NET C#Web表单隐藏ASP控件 - How to hide the asp control using asp.net C# web forms 将jqGrid与Asp.Net Web窗体一起使用 - Using jqGrid with Asp.Net Web Forms 在ASP.NET Web窗体中使用ASP.NET Web API时出现“方法不允许”错误 - Getting “Method Not Allowed” error when using ASP.NET Web API inside ASP.NET Web Forms Google Map or Bing Map Control for ASP.net Web Forms - Google Map or Bing Map Control for ASP.net Web Forms 在ASP.NET网页中托管Windows窗体用户控件 - Hosting Windows Forms user control in asp.net web page ASP.NET表单身份验证在本地主机服务器上进行身份验证,但不在Web服务器上进行身份验证 - ASP.NET Forms Authentication authenticates in localhost server, but not on the web server 如何使用包含验证文本框值的RequiredFieldValidator控件的asp.net创建Web服务器控件 - how to create web server control using asp.net that contain RequiredFieldValidator control that validate textbox value 在ASP.NET中的Web表单之间传递数据 - Pass data between web forms inside ASP.NET 使用转发器内部的枚举,复选框的值是空的(ASP.NET Web窗体) - The Value Of A Check Box Is Empty Using An Enumeration Inside A Repeater (ASP.NET Web Forms)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM