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?');" />
1
  • 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. Commented Oct 14, 2014 at 21:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.