2012-09-21 39 views
7

Tôi có một danh sách thả xuống trong asp.net Khi tôi nhấp vào một tùy chọn, tôi sẽ nhận được giá trị được chọn rồi chuyển nó vào cơ sở dữ liệu và sau đó sử dụng kết quả truy vấn để điền danh sách thả xuống thứ hai.Chọn Tùy chọn Thay đổi DropdownList C# ASP.NET Fire Event

Dường như tôi không thể "kích hoạt" sự kiện này khi tôi nhấp vào menu thả xuống đầu tiên. Dưới đây là những gì tôi có:

ASPX Mã

<td class="style3"> 
       <asp:DropDownList ID="Currencies" runat="server" 
        onselectedindexchanged="Currencies_SelectedIndexChanged"> 
       </asp:DropDownList> 
      </td> 
      <td> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td class="style2"> 
       Payment Mode</td> 
      <td class="style3"> 
       <asp:DropDownList ID="PaymentModes" runat="server"> 
       </asp:DropDownList> 
      </td> 

đang CodeBehind C#

String conn = WebConfigurationManager.ConnectionStrings["pvconn"].ToString(); 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     populatecurrencylist(); 

    } 

    public void populatecurrencylist() 
    { 
     SqlCommand sql = new SqlCommand("SELECT * FROM CURRENCIES_TBL ORDER BY Currency_Initials",new SqlConnection(conn)); 
     sql.Connection.Open(); 
     SqlDataReader listcurrencies; 
     listcurrencies = sql.ExecuteReader(); 
     Currencies.DataSource = listcurrencies; 
     Currencies.DataTextField = "Currency_Initials"; 
     Currencies.DataValueField = "Currency_Group_ID"; 
     Currencies.DataBind(); 
     sql.Connection.Close(); 
     sql.Connection.Dispose(); 
    } 

    protected void TextBox1_TextChanged(object sender, EventArgs e) 
    { 

    } 
    protected void Currencies_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     var currid = Currencies.SelectedValue; 
     HttpContext.Current.Response.Write(currid); 
     //int currid = 0; 
     try 
     { 
      SqlCommand sql = new SqlCommand("SELECT * FROM PAYMENT_MODES_TBL WHERE Currency_ID = @currencyid", new SqlConnection(conn)); 
      SqlParameter param0 = new SqlParameter(); 
      param0.ParameterName = "@currencyid"; 
      param0.SqlDbType = System.Data.SqlDbType.Int; 
      param0.Value = currid; 

      sql.Parameters.Add(param0); 
      sql.Connection.Open(); 
      SqlDataReader listpaymodes; 
      listpaymodes = sql.ExecuteReader(); 
      PaymentModes.DataSource = listpaymodes; 
      PaymentModes.DataTextField = "Payment_Mode"; 
      PaymentModes.DataValueField = "Paying_Account_Code"; 
      PaymentModes.DataBind(); 
      sql.Connection.Close(); 
      sql.Connection.Dispose(); 
     } 
     catch(Exception s) 
     { 
      HttpContext.Current.Response.Write("Error Occured " + s.Message); 
     }    
    } 

tôi có thể cư trú trong DropDownList đầu tiên mà không một vấn đề. Thứ hai là những gì dường như không hoạt động. Rất mới trong ASP.NET. Tôi đến từ một nền tảng PHP, nơi điều này sẽ đạt được một cách dễ dàng bằng cách sử dụng jquery ajax, nhưng tôi muốn học C#.

Bất kỳ trợ giúp nào được đánh giá cao.

EDIT

Tất cả các câu trả lời gợi ý rằng tôi làm cho đồng tiền danh sách thả xuống để AutoPostBack = true Tôi đã làm điều đó:

<asp:DropDownList ID="Currencies" runat="server" AutoPostBack="True" 
        onselectedindexchanged="Currencies_SelectedIndexChanged"> 
       </asp:DropDownList> 

Nhưng nó vẫn có vẻ như không làm việc. Trên một mặt lưu ý, trang tải lại và tùy chọn menu đã chọn của tôi được đặt lại về tùy chọn đầu tiên.

+0

Thêm thuộc tính AutoPostBack thành true. –

+0

Đã cập nhật câu trả lời của tôi. –

Trả lời

10

Thay đổi

<asp:DropDownList ID="Currencies" runat="server" 
        onselectedindexchanged="Currencies_SelectedIndexChanged"> 
       </asp:DropDownList> 

Để

<asp:DropDownList ID="Currencies" runat="server" AutoPostBack="True" 
        onselectedindexchanged="Currencies_SelectedIndexChanged"> 
       </asp:DropDownList> 

CẬP NHẬT

Cập nhật cho câu hỏi của bạn, sau khi cập nhật của bạn.

Thay đổi này:

protected void Page_Load(object sender, EventArgs e) 
    { 
     populatecurrencylist(); 

    } 

Để này:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if(!IsPostBack) { 
     populatecurrencylist(); 
     } 

    } 
2

Hãy Đơn vị tiền tệ thả xuống AutoPostBack True.

2

Theo mặc định, thuộc tính AutoPostBack của DropDownList là sai.

đúng nếu đăng lại máy chủ tự động xảy ra khi người dùng thay đổi lựa chọn danh sách; ngược lại, sai. Mặc định là sai.

Chỉ định nó như là đúng:

<asp:DropDownList ... AutoPostBack="True" ...> 
    ... 
</asp:DropDownList> 

Nếu điều này vẫn không hoạt động thì nó có thể được rằng bạn có điều khiển trong một UpdatePanel và cần phải xác định một cò.

0

Bạn cần đặt Autopostback = True.

Các vấn đề liên quan