asp.net中TextBox裡面實現斷行符號觸發指定事件

來源:互聯網
上載者:User

我在一個user_top使用者控制項裡面做了個包括搜尋的功能。然後再一個頁面中添加這個使用者控制項。瀏覽時候在textbox裡面輸入搜尋內容後。下意識的摁了斷行符號。誰知道報錯了。因為頁面回傳。我在page_load裡面寫了!IsPostBack{...}所以導致了未將對象引入對象執行個體。網上搜了下。

方法一:

    <asp:Panel ID="panSearch" runat="server" DefaultButton="SearchBtn">      
        <asp:TextBox ID="KeywordsTextField" runat="server" AutoCompleteType="Search"></asp:TextBox>
        <asp:ImageButton ID="SearchBtn" runat="server" ImageUrl="~/images/search_btn.gif" OnClick="BtnToSearch" ImageAlign="Middle" />
        <br />
    </asp:Panel>

 將TextBox和你要關聯的Button放在同一個panel裡面,用Panel控制項的DefaultButton="SearchBtn"屬性來指定要執行那個按鈕事件。Panel會產生一個div。

 

方法二:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title>無標題頁</title>    <script type="text/javascript">       function keyDown()       {                  if(event.keyCode==13)           {            document.getElementById("Button1").click();        }       }       </script></head><body>    <form id="form1" runat="server">    <asp:TextBox ID="KeywordsTextField" runat="server" AutoCompleteType="Search"></asp:TextBox>    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />    <br />    </form></body></html>

 因為伺服器控制項最終產生的還是html。所以雖然textbox裡面沒有onkeydown這個屬性。不過也可以用。

 方法三:

 

<script type="text/javascript">function document.onkeydown()     {        if(event.keyCode==13)        {            return false;        }    }</script>

這個是我乾脆把斷行符號給屏蔽來了。直接讓他點按搜尋按鈕。這個方法不友好。這些都是一個思路。知道了這些,自己都可以隨便弄了。 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.