2013-01-08 13 views
0

我有一些項目列表框存儲 並從文本框的按鍵向上事件中,我選擇我的選擇列表框選擇事件不與UpdatePanel的射擊和JQuery啓用

我面臨的問題是, 一旦我選擇我的選擇Listbox1_selectIndexchanged事件(設置我的選擇到另一個文本框)不正確觸發,它只激發時,我擊出了一些按鈕或某些頁面回發事件東西...

[我已經使用jQuery和的UpdatePanel]

這裏我的代碼

<%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/Main.Master"    CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication2.WebForm1" 
title="Untitled Page" %> 
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> 
    </asp:Content> 
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
    <asp:ScriptManager ID="ScriptManager1" runat="server"> 
    </asp:ScriptManager> 
    <asp:UpdatePanel ID="uppnl" runat="server"> 
     <ContentTemplate> 
     <script type="text/javascript" > 
      var prm = Sys.WebForms.PageRequestManager.getInstance(); 

      prm.add_endRequest(function() { 
      $('select[id$=ListBox1]').filterByText($('input[id$=TextBox1]'), true); 
      }); 
      </script> 
    <asp:Panel ID="Panel1" runat="server" Style="border-right: thin groove; border-top: thin groove; border-left: thin groove; border-bottom: thin groove; vertical-align: middle; width: 100%; text-align: center; padding-right: 5px; padding-left: 5px; padding-bottom: 5px; padding-top: 5px;"> 

    <br /> 

      <asp:Label ID="Label1" runat="server" Text="Type"></asp:Label> 
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
    <asp:Label ID="Label2" runat="server" Text="SelectedText"></asp:Label> 
      <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 


    &nbsp;<asp:ListBox ID="ListBox1" runat="server" Height="119px" Width="126px"> 
     <asp:ListItem Text="Apple" Value="1"></asp:ListItem> 
     <asp:ListItem Text="Orange" Value="2"></asp:ListItem> 
     <asp:ListItem Text="Peache" Value="3"></asp:ListItem> 
     <asp:ListItem Text="Banana" Value="4"></asp:ListItem> 
     <asp:ListItem Text="Melon" Value="5"></asp:ListItem> 
     <asp:ListItem Text="Lemon" Value="6"></asp:ListItem> 
     <asp:ListItem Text="Pineapple" Value="7"></asp:ListItem> 
     <asp:ListItem Text="Pomegranate" Value="8"></asp:ListItem> 
     <asp:ListItem Text="Mulberry" Value="9"></asp:ListItem> 
     <asp:ListItem Text="Apricot" Value="10"></asp:ListItem> 
     <asp:ListItem Text="Cherry" Value="11"></asp:ListItem> 
     <asp:ListItem Text="Blackberry" Value="12"></asp:ListItem> 
    </asp:ListBox> 
    <asp:Button ID="Button1" runat="server" Text="Refresh" /> 
    <br /> 
    </asp:Panel> 
    </ContentTemplate> 
    </asp:UpdatePanel> 

    <script type="text/javascript"> 
     jQuery.fn.filterByText = function(textbox, selectSingleMatch) { 
     return this.each(function() { 
      var select = this; 
      var options = []; 
      $(select).find('option').each(function() { 
       options.push({value: $(this).val(), text: $(this).text()}); 
      }); 
      $(select).data('options', options); 
      $(textbox).bind('change keyup', function() { 
       var options = $(select).empty().data('options'); 
       var search = $.trim($(this).val()); 
       var regex = new RegExp(search,"gi"); 

       $.each(options, function(i) { 
        var option = options[i]; 
        if(option.text.match(regex) !== null) { 
         $(select).append(
          $('<option>').text(option.text).val(option.value) 
         ); 
        } 
       }); 
       if (selectSingleMatch === true && $(select).children().length === 1) { 
        $(select).children().get(0).selected = true; 
       } 
      });    
     }); 
    }; 

    $(function() { 
     $('select[id$=ListBox1]').filterByText($('input[id$=TextBox1]'), true); 
    }); 

    </script> 

    </asp:Content> 

,並在我的代碼後面我有

Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged 
    TextBox2.Text = ListBox1.SelectedItem.ToString() 
    End Sub 

回答

0

你的事件做了火。

控件屬性在Page_Load方法中加載,並且您的事件將在Page_Load方法後觸發,因此您的頁面不會反映其結果。根據頁面生命週期,所有事件將在Page_Load方法之後觸發。

試試這個:

Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged 
    TextBox2.Text = ListBox1.SelectedItem.ToString() 
    Page_Load(sender, e) 
End Sub 

要了解更多有關頁面生命週期退房http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx

編輯:

您還需要你的列表框綁定到你的活動:

<asp:ListBox ID="ListBox1" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" runat="server"> 
+0

是的,我們絕對沒錯t有沒有辦法在用戶在列表框中選擇一個項目時強制頁面加載(例如,如果用戶在文本框中鍵入「ap」,那麼列表框中將包含{apple,pineapple,apricot},所以如果用戶在該瞬間選擇杏子,我希望我listbox textchange event to fire)是可能的 –

+0

還是有辦法將鼠標事件添加到列表框中 –

+0

是的,它有效,從此鏈接使用鼠標事件http://stackoverflow.com/questions/12195288/how-to- add-double-click-mouse-event-to-listbox –