Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在服务器控件中使用变量的asp.net web窗体_C#_Asp.net_Webforms_Aspbutton - Fatal编程技术网

C# 在服务器控件中使用变量的asp.net web窗体

C# 在服务器控件中使用变量的asp.net web窗体,c#,asp.net,webforms,aspbutton,C#,Asp.net,Webforms,Aspbutton,我试图在asp.net webform页面(.aspx)中的服务器控件内使用变量。我得到语法错误。可能是什么问题 <%string msgCancelProject = "You are not authorized to cancel the project."; %> <asp:Button ID="CancelProject" <%if(IsAuthorized){%> title="<% =msgCancelProject %>" clickD

我试图在asp.net webform页面(.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?');" />

希望这有帮助。

您正在尝试使用更类似于Razor或classic ASP的内联语法。使用codebehind或者通过.aspx文件中的事件(例如页面加载)来实现这一点会更好。
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");
            }
        }
    }