在服务器上发布时不调用后面的用户控制代码
User Control code behind is not called when published on server
我有一个简单的用户控件,它基本上是一个联系表单,由三个文本框、1 个按钮和 1 个标签组成。我也在使用 Telerik RadAjaxLoadingPanel 和 RadAjaxPanel。用户控件的标记如下,
<telerik:RadAjaxLoadingPanel ID="RALP_ContactForm" runat="server" Transparency="5">
<div class="border" style="display: table; height: 240px; width: 240px; #position: relative;
overflow: hidden; background-color:White">
<div class="border" style="#position: absolute; #top: 50%; display: table-cell; text-align: center;
vertical-align: middle;">
<div class="border" style="width: 100%; #position: relative; #top: -50%">
<img src="images/cf_animation.GIF" alt="Processing Request..." />
</div>
</div>
</div>
</telerik:RadAjaxLoadingPanel>
<telerik:RadAjaxPanel ID="upContactForm" runat="server">
<div id="form-main">
<div id="form-div">
<p class="name">
<asp:TextBox ID="txtContactName" ValidationGroup="ContactForm" CausesValidation="true"
runat="server" name="name" CssClass="validate[required,custom[onlyLetter],length[0,100]] feedback-input"
ClientIDMode="Static" placeholder="Name"></asp:TextBox>
<asp:CustomValidator ID="customValidator" runat="server" ValidationGroup="ContactForm"
ControlToValidate="txtContactName" Display="Dynamic" ClientValidationFunction="ValidateContactName"
ErrorMessage="" ValidateEmptyText="true"></asp:CustomValidator>
</p>
<p class="email">
<asp:TextBox ID="txtContactEmail" ValidationGroup="ContactForm" CausesValidation="true"
runat="server" name="email" CssClass="validate[required,custom[onlyLetter],length[0,100]] feedback-input"
ClientIDMode="Static" placeholder="Email"></asp:TextBox>
<asp:CustomValidator ID="customValidator1" runat="server" ValidationGroup="ContactForm"
ControlToValidate="txtContactEmail" Display="Dynamic" ClientValidationFunction="ValidateContactEmail"
ErrorMessage="" ValidateEmptyText="true"></asp:CustomValidator>
</p>
<p class="text">
<asp:TextBox ID="txtContactComment" ValidationGroup="ContactForm" CausesValidation="true"
TextMode="MultiLine" runat="server" name="text" ClientIDMode="Static" CssClass="validate[required,custom[onlyLetter],length[0,100]] feedback-input"
placeholder="Comment"></asp:TextBox>
<asp:CustomValidator ID="customValidator2" runat="server" ValidationGroup="ContactForm"
ControlToValidate="txtContactComment" Display="Dynamic" ClientValidationFunction="ValidateContactComment"
ErrorMessage="" ValidateEmptyText="true"></asp:CustomValidator>
</p>
<p><asp:Label ID="lblMessage" runat="server" Visible="false"></asp:Label></p>
<div class="submit">
<asp:Button ID="btnSubmitContactForm" Width="100%" runat="server" ValidationGroup="ContactForm"
Text="SEND" CssClass="btn-flat gr btn-submit-reg" OnClick="btnSubmitContactForm_Click" />
</div>
</div>
</div>
在后面的代码中,我只是将文本框中的信息发送到一个电子邮件地址。点击提交按钮时的事件代码如下,
protected void btnSubmitContactForm_Click(object sender, EventArgs e)
{
try
{
string AppPath = Request.PhysicalApplicationPath;
StreamReader sr = new StreamReader(AppPath + "EmailTemplates/UserFeedback.htm");
string MailBody = sr.ReadToEnd();
MailBody = MailBody.Replace("<%Name%>", txtContactName.Text.Trim());
MailBody = MailBody.Replace("<%Email%>", txtContactEmail.Text.Trim());
MailBody = MailBody.Replace("<%Comments%>", txtContactComment.Text.Trim());
// Close the StreamReader after reading text from it
sr.Close();
MailMessage message = new MailMessage(
"sender@mymail.com",
"receiver@mymail.com",
"Feedback",
MailBody);
message.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.myserver.com";
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.Credentials = new NetworkCredential("MyUserName", "MyPassword");
try
{
smtp.Send(message);
}
catch
{
}
txtContactComment.Text = "";
txtContactEmail.Text = "";
txtContactName.Text = "";
lblMessage.Visible = true;
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "Feedback sent successfully!";
}
catch
{
lblMessage.Visible = true;
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Error sending feedback ! ";
}
}
User控件是这样调用的,
<uc1:FooterContactForm ID="FooterContactForm" runat="server"></uc1:FooterContactForm>
并且在页面顶部,
<%@ Register Src="ContactForm.ascx" TagName="FooterContactForm" TagPrefix="uc1" %>
现在这一切都变得非常简单,并且在我的测试机上工作得很好。当我在服务器上发布它时,会显示加载图像,但不会触发后面的代码。我试图在用户控件的 Page_Load 事件的标签中设置虚拟文本,但即使是该测试也不会显示在实时服务器上的用户控件上。我每次在本地提交时都会收到电子邮件。
我的问题是,当用户控件发布到实时服务器上时,为什么不调用后面的代码,是否有人遇到过这样的问题?
编辑:(从控制台发现错误)
POST http://test.mywebsite.com/ 500 (Internal Server Error)
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6
Sys.Net.XMLHttpExecutor.executeRequest
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6
Sys.Net._WebRequestManager.executeRequest
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6
Sys.Net.WebRequest.invoke
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:15
Sys.WebForms.PageRequestManager._onFormSubmit
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:15
Sys.WebForms.PageRequestManager._doPostBackTelerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:15
Sys.WebForms.PageRequestManager._doPostBackWithOptions
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6
(anonymous function)(index):800
onclick
第二个错误:
Uncaught Sys.WebForms.PageRequestManagerServerErrorException:
Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6 Error.create
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:15
Sys.WebForms.PageRequestManager._createPageRequestManagerServerError
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:15
Sys.WebForms.PageRequestManager._onFormSubmitCompleted
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6 (anonymous function)
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6 (anonymous function)
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6
Sys.Net.WebRequest.completed
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6 _onReadyStateChange
当你在 ajax 面板中有一些东西,并且由于某种原因开始不起作用时,可能是你错过了一个 javascript 错误,一个未加载的文件,一个异常你没看到。
在这种情况下,只需删除 ajax 面板以定位代码上的错误,and/or 检查浏览器上的 javascript 控制台错误。
从您的意见来看,如果用户未通过身份验证,某些文件夹中的某些 javascript 文件会因安全问题而无法加载,然后您会遇到一些 javascript 错误,无法让您的代码生效运行正确。
我有一个简单的用户控件,它基本上是一个联系表单,由三个文本框、1 个按钮和 1 个标签组成。我也在使用 Telerik RadAjaxLoadingPanel 和 RadAjaxPanel。用户控件的标记如下,
<telerik:RadAjaxLoadingPanel ID="RALP_ContactForm" runat="server" Transparency="5">
<div class="border" style="display: table; height: 240px; width: 240px; #position: relative;
overflow: hidden; background-color:White">
<div class="border" style="#position: absolute; #top: 50%; display: table-cell; text-align: center;
vertical-align: middle;">
<div class="border" style="width: 100%; #position: relative; #top: -50%">
<img src="images/cf_animation.GIF" alt="Processing Request..." />
</div>
</div>
</div>
</telerik:RadAjaxLoadingPanel>
<telerik:RadAjaxPanel ID="upContactForm" runat="server">
<div id="form-main">
<div id="form-div">
<p class="name">
<asp:TextBox ID="txtContactName" ValidationGroup="ContactForm" CausesValidation="true"
runat="server" name="name" CssClass="validate[required,custom[onlyLetter],length[0,100]] feedback-input"
ClientIDMode="Static" placeholder="Name"></asp:TextBox>
<asp:CustomValidator ID="customValidator" runat="server" ValidationGroup="ContactForm"
ControlToValidate="txtContactName" Display="Dynamic" ClientValidationFunction="ValidateContactName"
ErrorMessage="" ValidateEmptyText="true"></asp:CustomValidator>
</p>
<p class="email">
<asp:TextBox ID="txtContactEmail" ValidationGroup="ContactForm" CausesValidation="true"
runat="server" name="email" CssClass="validate[required,custom[onlyLetter],length[0,100]] feedback-input"
ClientIDMode="Static" placeholder="Email"></asp:TextBox>
<asp:CustomValidator ID="customValidator1" runat="server" ValidationGroup="ContactForm"
ControlToValidate="txtContactEmail" Display="Dynamic" ClientValidationFunction="ValidateContactEmail"
ErrorMessage="" ValidateEmptyText="true"></asp:CustomValidator>
</p>
<p class="text">
<asp:TextBox ID="txtContactComment" ValidationGroup="ContactForm" CausesValidation="true"
TextMode="MultiLine" runat="server" name="text" ClientIDMode="Static" CssClass="validate[required,custom[onlyLetter],length[0,100]] feedback-input"
placeholder="Comment"></asp:TextBox>
<asp:CustomValidator ID="customValidator2" runat="server" ValidationGroup="ContactForm"
ControlToValidate="txtContactComment" Display="Dynamic" ClientValidationFunction="ValidateContactComment"
ErrorMessage="" ValidateEmptyText="true"></asp:CustomValidator>
</p>
<p><asp:Label ID="lblMessage" runat="server" Visible="false"></asp:Label></p>
<div class="submit">
<asp:Button ID="btnSubmitContactForm" Width="100%" runat="server" ValidationGroup="ContactForm"
Text="SEND" CssClass="btn-flat gr btn-submit-reg" OnClick="btnSubmitContactForm_Click" />
</div>
</div>
</div>
在后面的代码中,我只是将文本框中的信息发送到一个电子邮件地址。点击提交按钮时的事件代码如下,
protected void btnSubmitContactForm_Click(object sender, EventArgs e)
{
try
{
string AppPath = Request.PhysicalApplicationPath;
StreamReader sr = new StreamReader(AppPath + "EmailTemplates/UserFeedback.htm");
string MailBody = sr.ReadToEnd();
MailBody = MailBody.Replace("<%Name%>", txtContactName.Text.Trim());
MailBody = MailBody.Replace("<%Email%>", txtContactEmail.Text.Trim());
MailBody = MailBody.Replace("<%Comments%>", txtContactComment.Text.Trim());
// Close the StreamReader after reading text from it
sr.Close();
MailMessage message = new MailMessage(
"sender@mymail.com",
"receiver@mymail.com",
"Feedback",
MailBody);
message.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.myserver.com";
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.Credentials = new NetworkCredential("MyUserName", "MyPassword");
try
{
smtp.Send(message);
}
catch
{
}
txtContactComment.Text = "";
txtContactEmail.Text = "";
txtContactName.Text = "";
lblMessage.Visible = true;
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "Feedback sent successfully!";
}
catch
{
lblMessage.Visible = true;
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Error sending feedback ! ";
}
}
User控件是这样调用的,
<uc1:FooterContactForm ID="FooterContactForm" runat="server"></uc1:FooterContactForm>
并且在页面顶部,
<%@ Register Src="ContactForm.ascx" TagName="FooterContactForm" TagPrefix="uc1" %>
现在这一切都变得非常简单,并且在我的测试机上工作得很好。当我在服务器上发布它时,会显示加载图像,但不会触发后面的代码。我试图在用户控件的 Page_Load 事件的标签中设置虚拟文本,但即使是该测试也不会显示在实时服务器上的用户控件上。我每次在本地提交时都会收到电子邮件。
我的问题是,当用户控件发布到实时服务器上时,为什么不调用后面的代码,是否有人遇到过这样的问题?
编辑:(从控制台发现错误)
POST http://test.mywebsite.com/ 500 (Internal Server Error)
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6
Sys.Net.XMLHttpExecutor.executeRequest
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6
Sys.Net._WebRequestManager.executeRequest
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6
Sys.Net.WebRequest.invoke
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:15
Sys.WebForms.PageRequestManager._onFormSubmit
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:15
Sys.WebForms.PageRequestManager._doPostBackTelerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:15
Sys.WebForms.PageRequestManager._doPostBackWithOptions
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6
(anonymous function)(index):800
onclick
第二个错误:
Uncaught Sys.WebForms.PageRequestManagerServerErrorException:
Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6 Error.create
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:15
Sys.WebForms.PageRequestManager._createPageRequestManagerServerError
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:15
Sys.WebForms.PageRequestManager._onFormSubmitCompleted
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6 (anonymous function)
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6 (anonymous function)
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6
Sys.Net.WebRequest.completed
Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScri…:6 _onReadyStateChange
当你在 ajax 面板中有一些东西,并且由于某种原因开始不起作用时,可能是你错过了一个 javascript 错误,一个未加载的文件,一个异常你没看到。
在这种情况下,只需删除 ajax 面板以定位代码上的错误,and/or 检查浏览器上的 javascript 控制台错误。
从您的意见来看,如果用户未通过身份验证,某些文件夹中的某些 javascript 文件会因安全问题而无法加载,然后您会遇到一些 javascript 错误,无法让您的代码生效运行正确。