function getInfo()
{
var UsrNameId = '<%=Txb_UserName.ClientID%>';
var PswordId = '<%=Txb_Psswd.ClientID%>';
if (FieldCheck(UsrNameId, PswordId))
{
document.getElementById('<%= Btn_Hidden.ClientID %>').click();
alert("test");
}
}
我在ASPx文件中使用此JavaScript 我有一个单独的javascript文件包含以下代码
function FieldCheck(UserNameId,PasswordId)
{
var Usr_Name = document.getElementById(UserNameId).value;
if (Usr_Name == null || Usr_Name == "") {
alert("Name Field Required");
return false;
}
var Usr_Psswd = document.getElementById(PasswordId).value;
if (Usr_Psswd == null || Usr_Psswd == "") {
alert("Password Field Required");
return false;
}
return true;
}
当我运行代码并将值插入必填字段时,我会得到一个包含文本&#34; test&#34;的警告框。但是
document.getElementById('<%= Btn_Hidden.ClientID %>').click();
没有触发 Btn_Hidden上的代码是:
protected void Btn_Hidden_Click(object sender, EventArgs e)
{
Lbl_Warning.Visible = true;
if(Txb_UserName.Text!="Maurice")
{
Lbl_Warning.Text = "Wrong Username";
}
else if(Txb_Psswd.Text!="admin")
{
Lbl_Warning.Text = "Wrong Password";
}
else
{
Response.Redirect("Welcome.html?");
}
}
在2个asp按钮上写的代码是:
<asp:Button ID="Btn_Submit" runat="server" OnClientClick="getInfo()" Text="Login" />
<asp:Button runat="server" ID="Btn_Hidden" OnClick="Btn_Hidden_Click"/>
答案 0 :(得分:0)
当你点击“Btn_Submit”按钮整页是PostBack。 要解决问题,你必须像下面这样添加ScriptManager:
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<asp:TextBox ID="Txb_UserName" runat="server"></asp:TextBox>
<asp:TextBox ID="Txb_Psswd" runat="server"></asp:TextBox>
<asp:Button ID="Btn_Submit" runat="server" OnClientClick="getInfo()" Text="Login" />
<asp:Button runat="server" ID="Btn_Hidden" OnClick="Btn_Hidden_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</form>