简体   繁体   中英

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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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