0

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?');" />
Kurkula
  • 6,386
  • 27
  • 127
  • 202
  • You're trying to use inline syntax that is more akin to Razor or classic ASP. This would be much better done using codebehind instead or through an event within the .aspx file such as the Page_Load. – Mark Fitzpatrick Oct 14 '14 at 21:58

1 Answers1

2

It's not possible to do what you are trying to do with a server control. I.e. 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.

Sam
  • 2,917
  • 1
  • 15
  • 28