我试图在asp.net网页(.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");
}
}
}
希望这能有所帮助。