繁体   English   中英

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

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

我正在尝试在asp.net Webform页面(.aspx)的服务器控件中使用一个变量。 我收到语法错误。 可能是什么问题?

<%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?');" />

使用服务器控件无法完成您想做的事情。 即在标记中动态添加属性。 您只能设置属性值,但这不是您想要的。

您可以按照下面的代码实现所需的功能。

保持这样的标记。

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

并且,在您后面的代码中执行此操作。

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");
            }
        }
    }

希望这可以帮助。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM