2015-10-05 27 views
0

我有一個ASP.NET網站與SQL Server 2008數據庫。我想選擇一個下拉列表的文本並在GridView和標籤中填寫詳細信息,但是當我選擇一個選定文本項時,它不會填充GridView和標籤中的詳細信息。如何獲取DropDownList選定的文本和填充GridView中的詳細信息和基於asp.net中的標籤

請注意,我的下拉列表是幾個月的列表。我該如何解決這個問題?

我的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 

public partial class form : System.Web.UI.Page 
{ 
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["emp_dbConnectionString"].ConnectionString); 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void ddlEmpRecord_SelectedIndexChanged(object sender, EventArgs e) 
    { 

    } 

    private void BindEmpGrid() 
    { 
     DataTable dt = new DataTable(); 
     SqlDataAdapter adp = new SqlDataAdapter(); 
     try 
     { 
      SqlCommand cmd = new SqlCommand("select * from shakhes where mah_tadieh=" + ddlEmpRecord.Text + " ", con); 
      adp.SelectCommand = cmd; 
      adp.Fill(dt); 

      if (dt.Rows.Count > 0) 
      { 
       grdEmp.DataSource = dt; 
       grdEmp.DataBind(); 
      } 
     } 
     catch (Exception ex) 
     { 
      ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true); 
     } 
     finally 
     { 
      dt.Clear(); 
      dt.Dispose(); 
      adp.Dispose(); 

     } 
    } 
    protected void ddlEmpRecord_TextChanged(object sender, EventArgs e) 
    { 
     BindEmpGrid(); 
    } 
} 
+0

我有問題。你在哪裏填寫下拉列表的數據。您是否嘗試過通過調用選定索引更改事件中的方法BindEmpGrid? –

回答

1

使用SelectedIndexChanged事件,而不是TextChanged事件,並確保你的DropDownList的AutoPostBack屬性設置爲 「真」。

protected void ddlEmpRecord_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    BindEmpGrid(); 
} 

希望它有幫助。

相關問題