在gridview的edittemplate中,我有一個下拉列表,當點擊每個gridview行的編輯按鈕時,它將提供一個值列表。如何獲取gridview的dropdownlist的值?
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack=false OnLoad="DropDownList1_onload"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</EditItemTemplate>
C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.Parent.Parent;
int idx = row.RowIndex;
DropDownList txtECustCode = (DropDownList)row.Cells[0].FindControl("DropDownList1");
string _text1 = txtECustCode.selectedvalue();
}
protected void DropDownList1_onload(object sender, EventArgs e)
{
SqlConnection cn = new System.Data.SqlClient.SqlConnection("Data Source=CHANGEME1;Initial Catalog=Reflection;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter("select salary from employee", cn);
DataSet ds = new DataSet();
var ddl = (DropDownList)sender;
da.Fill(ds);
cn.Close();
ddl.DataSource = ds;
ddl.DataTextField = "salary";
ddl.DataValueField = "salary";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("--Select--", "0"));
}
所以,當我在編輯按鈕點擊onload事件將被解僱,並從數據庫列表下的數據。當我選擇下拉菜單項時,我需要訪問下拉列表選定的值。
但每當我改變了下拉之前的SelectedIndexChanged再次調用在onload功能,inturn刷新內容並返回的selectedIndex爲0
我如何避免這種情況?
沒關係,誤解你的代碼。 – JNYRanger
在更新發生的同時是否觸發了'SelectedIndexChanged'事件,就像在選擇過程中一樣? – JNYRanger
當我選擇項目並點擊更新按鈕時,此事件將被解僱。 –