[英]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). 我正在尝试在asp.net Webform页面(.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. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.