2012-09-21 54 views
7

我在asp.net中有一个下拉列表当我点击一个选项,我应该得到选定的值,然后将其传递给数据库,然后使用查询结果填充第二个下拉列表。选择选项更改DropdownList C#ASP.NET火灾事件

当我点击第一个下拉菜单时,我似乎无法“触发”此事件。下面是我有:

ASPX代码

<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> 

代码隐藏代码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); 
     }    
    } 

我可以填充第一个DropDownList没有问题。第二个是似乎没有工作。在ASP.NET中非常新。我来自PHP背景,使用jquery ajax很容易实现,但我想学习C#。

任何帮助表示赞赏。

编辑

所有的答案表明,我让货币下拉列表AutoPostBack = true 我已经做到了:

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

但它似乎仍然没有工作。在附注中,页面重新加载,我的选择菜单选项重置为第一个选项。

+0

将AutoPostBack属性添加为true。 –

+0

更新了我的答案。 –

回答

10

变化

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

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

UPDATE

更新你的问题,你的更新之后。

更改此:

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

    } 

要这样:

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

    } 
2

货币下拉自动回真。

2

默认DropDownListAutoPostBack属性为false。

只要 用户更改列表的选择,就会自动发送回服务器,则为true;否则,是错误的。默认 是错误的。

其指定为真:

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

如果这仍然不起作用,那么它可能是你有一个UpdatePanel内的控件,需要指定一个触发器。

0

您需要设置Autopostback = True。