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