[英]JavaScript method is not calling from backend
In my page I have a login control: 在我的页面中,我有一个登录控件:
<asp:Login ID="EMSLogin" runat="server" OnAuthenticate="EMSLogin_Authenticate">
<LayoutTemplate>
<asp:Panel ID="Panel1" runat="server" CssClass="wrapper">
<asp:Panel ID="Panel2" runat="server" CssClass="holder">
<asp:Panel ID="Panel3" runat="server" CssClass="loginBox one_edge_shadow">
<h1>
Login Credentials</h1>
<asp:Panel ID="Panel4" runat="server" CssClass="name topmargin">
<asp:Panel ID="Panel5" runat="server" CssClass="label">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
</asp:Panel>
<asp:Panel ID="Panel6" runat="server" CssClass="textBox">
<telerik:RadTextBox ID="UserName" runat="server" Height="16px" Width="165px" Font-Size="14px"
Font-Names="Arial Sans-Serif" ToolTip="Enter your valid login name" />
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
ValidationGroup="EMSLogin">*</asp:RequiredFieldValidator>
</asp:Panel>
<br class="clearfix" />
</asp:Panel>
<asp:Panel ID="Panel7" runat="server" CssClass="name topmargin">
<asp:Panel ID="Panel8" runat="server" CssClass="label">
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
</asp:Panel>
<asp:Panel ID="Panel9" runat="server" CssClass="textBox">
<telerik:RadTextBox ID="Password" runat="server" TextMode="Password" Height="16px"
Width="165px" Font-Size="14px" Font-Names="Arial Sans-Serif" ToolTip="Enter your valid password" />
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
ValidationGroup="EMSLogin">*</asp:RequiredFieldValidator>
</asp:Panel>
<br class="clearfix" />
<telerik:RadButton ID="LoginButton" runat="server" CommandName="Login" Text="Log In"
CssClass="loginButton" Font-Size="14px" Width="100px" ValidationGroup="EMSLogin"
ToolTip="Click to log in" />
</asp:Panel>
</asp:Panel>
</asp:Panel>
</asp:Panel>
</LayoutTemplate>
</asp:Login>
I have added the ScriptManager control to the page. 我已将ScriptManager控件添加到页面中。
Now the EMSLogin_Authenticate
is: 现在, EMSLogin_Authenticate
是:
protected void EMSLogin_Authenticate(object sender, AuthenticateEventArgs e) {
RadTextBox UserName = EMSLogin.FindControl("UserName") as RadTextBox;
RadTextBox Password = EMSLogin.FindControl("Password") as RadTextBox;
if (Membership.ValidateUser(UserName.Text, Password.Text)) {
FormsAuthentication.RedirectFromLoginPage(UserName.Text, false);
} else {
Page.ClientScript.RegisterClientScriptBlock(typeof(ScriptManager), "CallShowDialog", "showDialog();", true);
}
}
The JavaScript method showDialog();
JavaScript方法showDialog();
is defined in the page: 在页面中定义:
<script type="text/javascript">
function showDialog() {
$(".jym").dialog("open");
return false;
}
</script>
But it is not calling. 但这不是召唤。 There is nothing wrong with showDialog()
since I have called it on the onclick method of an anchor tag; showDialog()
没有任何问题,因为我已经在锚标记的onclick方法上调用了它; it is showing the dialog. 它显示对话框。 If I write alert()
in place of showDialog()
in code behind then I can see the alert message. 如果我在后面的代码中写了alert()
代替showDialog()
,那么我会看到警报消息。
What I am doing wrong? 我做错了什么? Is it not possible to call JavaScript in this way? 这样无法调用JavaScript吗?
This is also not working: 这也不起作用:
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this.Page, typeof(RadButton), Guid.NewGuid().ToString(), "showDialog();", true);
The RegisterClientScriptBlock
put the code before the document, so by the time you are calling the function, there is no element with class jym
just yet. RegisterClientScriptBlock
将代码放在文档之前,因此在您调用该函数时,尚没有jym
类的元素。 You can add alert($(".jym").length)
to confirm that, you'll see 0. 您可以添加alert($(".jym").length)
确认,您将看到0。
Why not calling the function from client side? 为什么不从客户端调用该函数?
$(document).ready(function() {
showDialog();
});
If you need it server side, just change your code to: 如果需要服务器端,只需将代码更改为:
if (Membership.ValidateUser(UserName.Text, Password.Text)) {
FormsAuthentication.RedirectFromLoginPage(UserName.Text, false);
} else {
Page.ClientScript.RegisterClientScriptBlock(typeof(ScriptManager), "CallShowDialog", "$(document).ready(function() { showDialog(); });", true);
}
A few questions: 几个问题:
Is that function sitting in the proper script tag: 该函数是否位于正确的脚本标记中:
Try reforming your RegisterClientScriptBlock command: 尝试重新设置RegisterClientScriptBlock命令:
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallShowDialog", "showDialog();"); Page.ClientScript.RegisterClientScriptBlock(this.GetType(),“ CallShowDialog”,“ showDialog();”);
I don't think you need the overload for added the script tags. 我认为您不需要超载来添加脚本标签。
如果您使用的是RadAjaxManager,则必须使用以下命令:
RadAjaxManager.GetCurrent(Page).ResponseScripts.Add("showDialog();");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.