我有DROPDOWNLIST我填充在Page_Load中在下拉Selected_Indexchanged事件的SelectedValue總是越來越復位的第一個值
的時間protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable table = new DataTable();
string connectionString = GetConnectionString();
string sqlQuery = "select distinct sname from contacts where sname is not null";
using (OracleConnection conn = new OracleConnection(connectionString))
{
try
{
conn.Open();
using (OracleCommand cmd = new OracleCommand(sqlQuery, conn))
{
using (OracleDataAdapter ODA = new OracleDataAdapter(cmd))
{
ODA.Fill(table);
}
}
}
catch (Exception ex)
{
Response.Write("Not Connected" + ex.ToString());
}
}
//DropDownList1.DataSource = table;
//DropDownList1.DataValueField = "";
DropDownList2.DataSource = table;
DropDownList2.DataValueField = "sname";
DropDownList2.DataTextField = "sname";
DropDownList2.DataBind();
}
}
現在我要當DropDownList的項目改變
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable table = new DataTable();
string connectionString = GetConnectionString();
string val = DropDownList2.SelectedValue;
string sqlQuery = "SELECT distinct DUTY_DATE FROM DUTY_ROTA,DUTY_TYPES,CONTACTS WHERE DUTY_DATE between SYSDATE and SYSDATE+30 AND DUTY_ROTA.DUTY_TYPE = DUTY_TYPES.DUTY_TYPE AND SNAME IS NOT NULL and contacts.sname = '" + val + "' ORDER BY DUTY_DATE";
using (OracleConnection conn = new OracleConnection(connectionString))
{
try
{
conn.Open();
using (OracleCommand cmd = new OracleCommand(sqlQuery, conn))
{
using (OracleDataAdapter ODA = new OracleDataAdapter(cmd))
{
ODA.Fill(table);
}
}
}
catch (Exception ex)
{
Response.Write("Not Connected" + ex.ToString());
}
}
GridView1.DataSource = table;
GridView1.DataBind();
}
來填充一個GridView
我已啓用AutoPostBack。現在,當我將DropDownList項目更改爲不同的頁面時,頁面正在加載但始終保留第一個值。我試圖調試,我發現
string val = DropDownList2.SelectedValue;
val變量總是查詢返回的第一個值。任何人都可以告訴我,我怎麼能擺脫這一點。我想在我選擇下拉列表中的任何項目時填充GridView。
看看是否改爲DropDownList2.SelectedItem.Value有助於?即字符串val = DropDownList2..SelectedItem.Value; – Ratna
它是一樣的。 :(我是否錯了什麼地方非常基礎? – Subhamoy
你試過DropDownList2.SelectedItem.Text ?? – Rafay