我有一个网站母版页和一个内容页。每当我点击内容页面中的按钮时,它都不会触发执行单击事件的方法。
SiteMaster代码:
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
</head>
<body background="../../Content/city-balcony@2x.jpg">
<form id="form1" runat="server">
<div class="page">
<div id="header">
<div id="title">
<h1>Welcome to E-Meeting</h1>
</div>
<div id="logindisplay">
<% Html.RenderPartial("LogOnUserControl"); %>
</div>
<div id="menucontainer">
<ul id="menu">
<li><%: Html.ActionLink("Home", "Index", "Home")%></li>
<%
if (Request.IsAuthenticated)
{
%>
<li><%: Html.ActionLink("Chat", "ViewPage1")%></li>
<% }
%>
<li><%: Html.ActionLink("About", "About", "Home")%></li>
</ul>
</div>
</div>
<div id="main">
<%="Last communicated at : " + DateTime.Now.ToLongTimeString()%>
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
<div id="footer">
</div>
</div>
<br />
<br />
</div>
</form>
</body>
</html>
内容页面:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Modified by click event";
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
ChT
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>ChT</h2>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="On Load"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
每当我点击按钮时,我希望标签文本为“由点击事件修改”,但它仍然是“On Load”。有人可以帮忙吗
答案 0 :(得分:0)
尝试在更新面板中添加触发器,如下所示:
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
并检查ScriptManager的 EnablePartialRendering
,如果它显示或将其设置为 true
答案 1 :(得分:0)
您可以使用C#添加方法来触发事件。
例如,
protected void Button1_Click(object sender, EventArgs e){
Label1.text = "Modified by click event";
}
顺便说一下,你可以在你的aspx代码中摆脱Text =“On Load”。 (您的代码为:<asp:Label ID="Label1" runat="server"></asp:Label>
,然后在.cs文件中添加page_load方法:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Label1.text = "On load";
}
在这种情况下,当您打开网页时,它将显示“正在加载”,当您单击按钮时,它将显示“由点击事件修改”。
希望它
答案 2 :(得分:0)
将EnableEventValidation="false"
添加到内容页面
待
<%@ Page Title="" EnableEventValidation="false" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
它会起作用