javascript页面加载发生在Button Click上

时间:2012-08-29 07:53:52

标签: javascript asp.net

我的问题是当我点击id =“cmdAddATM”整个.aspx页面重新加载按钮时,虽然我没有写任何与之关联的点击功能。问题是什么?因为这个问题在我的项目中出现了一些与ajax / jquery相关的新问题

文件名:AddEditATM.aspx.cs

namespace Monitoring_Tool
{
    public partial class AddEditATM : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Generix.fillDropDown(ref litRegion, Generix.getData("dbo.Region", "REGION, Code", "", "", "Code", 1));
        }
    }
}

文件:AddEditATM.aspx

<script language="javascript" type="text/javascript">
    $(document).ready(function() {
        showAddEditATMLoad();
    });
</script>
<body>
// I an not writing full syntax here but i have one button with id "cmdAddATM"
</body>

外部JS档案:

function showAddEditATMLoad() { 
    //Its Blank
}

我的HTML代码: -

<table style="margin: 0px auto; width: 90%" runat="server">
  <thead>
      <tr>
          <th colspan="4" align="center" class="ui-widget-header PageHeader">
              ADD/EDIT ATM
          </th>
      </tr>
  </thead>
  <tbody style="vertical-align: bottom; border-style: solid; border-width: thick;">
      <tr>
          <td style="padding-left: 5px" colspan="2">
              Enter ATM ID &nbsp;<input id="txtEditATM" name="txtEditATM" type="text" />
              &nbsp;&nbsp;
              <button id="cmdEditATM">
                  EDIT ATM</button>
          </td>
          <td align="left" style="font-weight: bold" colspan="1">
              OR
          </td>
          <td align="center" colspan="2">
              <button id="cmdAddATM">
                  ADD ATM</button>
          </td>
      </tr>
  </tbody>
</table>

5 个答案:

答案 0 :(得分:2)

如果您的按钮是<asp:Button runat="server" />,则会在浏览器上呈现为<input type="submit" />。所以,它会回发你的页面。

已修改:使用type="button"标记添加<button>属性, <button>标记的类型属性有三个值,您的案例中标记的默认值是提交,这就是提交表单的原因。

1. Submit - For Submitting a Form
2. Reset - For Resetting the Form
3. button - A Simple Button

答案 1 :(得分:1)

将类型属性添加到<button>代码。

<button id="cmdEditATM" type='button'>EDIT ATM</button>

SO线程

  1. vs. . Which to use?
  2. input type=“submit” Vs button tag are they interchangeable?

答案 2 :(得分:1)

如果您正在使用asp:Button对象,则可以使用OnClientClick属性或仅使用jQuery来运行某些客户端代码。您可以从javascript click事件处理程序返回false(或使用jQuery的event.preventDefault()方法)来停止回发。

如果您不希望此按钮发生回发,只需使用普通的HTML按钮(<input type="button" ... />)即可。如果您需要在服务器端访问此按钮,只需向其添加正常的runat="server"属性。

请注意,HTML <input type="submit"/>按钮会导致回发,因为它会在页面上提交表单。

答案 3 :(得分:1)

为您的按钮添加type="button"属性。不同的浏览器可能会为button元素使用不同的默认类型。

答案 4 :(得分:0)

在asp.net中如果你添加任何asp按钮它自动转换asp按钮提交按钮所以用HTML按钮替换asp按钮

<input type="button" id='cmdAddATM' value="Button" />
相关问题