输入提交onlick和onserverclick无法正常工作 [英] Input submit onlick and onserverclick not working

查看:106
本文介绍了输入提交onlick和onserverclick无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好



我正在使用以下代码在简单的登录屏幕上工作:



Hi

I'm working on a simple login screen with the below code:

<form id="login">
    <div class="formHeader">
      <h1>Login</h1>
    </div>
    <div class="formDiv">
      <input type="text" placeholder="Email" name="txtEmail"/>
      <div class="inputImage fa fa-user"></div>
    </div>
    <div class="formDiv">
      <input type="password" placeholder="Password" name="txtPassword"/>
      <div class="inputImage fa fa-lock"></div>
    </div>
    <div class="formDiv">
      <label id="remember">
        <input type="checkbox" name="cbxRemberMe" value="Checked"/>
        <div class="checkbox"></div><span>Remember me</span>
      </label>
    </div>
    <div class="formDiv">
      <input type="submit" value="LOGIN" runat="server" onserverclick="btnLogin_Click" onclick="btnLogin_Click" />
    </div>
    <div class="formFooter"><a class="forgot" href="#">Forgot Password</a></div>
  </form>





和后端代码





and back-end code

 protected void btnLogin_Click(object sender, EventArgs e) 
{

}





但该按钮根本不会调用后端代码。



除了URL从

http:// localhost:15726 / Pages / Login.aspx更改为

http:// localhost:15726 / Pages / Login.aspx?txtEmail =& txtPassword =& ctl00 = LOGIN



我不能在表单中放置一个Runat = Server属性,因为我在这个页面上有2个表单(登录和注册)



任何帮助表示赞赏。



我的尝试:



1.玩Runat = Server,onclick()和OnServerClick()(交换并更改它们)



2.输入类型=提交/输入类型=按钮



这些都不起作用。



But the button does not call the backend code at all.

Nothing happens except the URL changes from
"http://localhost:15726/Pages/Login.aspx" to
"http://localhost:15726/Pages/Login.aspx?txtEmail=&txtPassword=&ctl00=LOGIN"

I cannot put a Runat=Server attribute in the form as I have 2 forms on this page (login and register)

any help is appreciated.

What I have tried:

1. Playing around with Runat=Server, onclick() and OnServerClick() (swapping around and changing them up)

2. Making input type=submit / input type=button

none of these work.

推荐答案

网址正在更新,因为你没有POST作为表单方法



The url is updating like that because you don't have POST as the form method

<form id="login" method="POST">





但这不是你的主要问题。如果需要,您可以在页面上有多个表单,它们只是不能嵌套,只有一个可以有runat = server。如果您的表单没有嵌套,那么您可以使用普通表单,但是您将无法像使用asp控件那样使用服务器端功能。相反,你需要使用page_load事件并使用Request.Form [control Name Here]来分析表单组件的值(注意它是控件的名称,不是你用来引用控件的id。)



如果你的表单*是嵌套的,那么除了外部表单和包装之外你需要除掉它们你在asp:Panel中的内部登录控件组并指定面板的DefaultButton属性





However that isn't your main problem. You can have multiple forms on the page if you need to, they just can't be nested and only one can have runat=server. If your forms are not nested then you can use a normal form but you won't be able to use server-side functionality like you do with asp controls. Instead you'll need to use the page_load event and use Request.Form["controlNameHere"] to analyse the values of your form components (note it is the control's name, not the id you use to reference the control).

If your forms *are* nested then you need to get rid of all but the outer form and wrap your inner group of login controls in an asp:Panel and specify the "DefaultButton" attribute of the panel

<asp:Panel DefaultButton="btnLogin" runat="server">
    <asp:TextBox ID="txtUsername" runat="server" />
    <asp:Button ID="btnLogin" Text="Login" runat="server" />
</asp:Panel>





这会创建一种虚拟形式的形式,如果有人按下用户名框上的RETURN,然后单击登录按钮,允许您使用btnLogin_Click。这将导致包含整个表单的回发,但你只是忽略你不感兴趣的控件。



总而言之,要么使用非嵌套非服务器表单并使用Request.Form从表单元素中读取值,或者使用一个包含所有控件的服务器表单,并使用Panel \DefaultButton在表单中创建表单。



That creates a kind of virtual form-within-a-form where if someone presses RETURN on the username box then it will click the Login button allowing you to use btnLogin_Click. This will cause a postback containing the entire form but you just ignore the controls you're not interested in.

So to sum up either use a non-nested non-server form and use Request.Form to read the values from the form elements, or have one server form that has all controls and use Panel\DefaultButton to create a form within a form.


这篇关于输入提交onlick和onserverclick无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆