我有一个gridview,其中我已经创建了一个面板,该面板有一个状态名称的下拉列表,在选择时应该更新下面列表框的值,该名称为该州的城市名称。 我的问题是我无法调用dropdownlist的selectedIndexchange函数(在禁用viewstatemode时,函数被调用但selectedindex值为null)
<asp:GridView ID="gvCountry1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" OnRowDataBound="gvCountry1_OnRowDataBound"
onselectedindexchanged="gvCountry1_SelectedIndexChanged">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt = "" style="cursor: pointer" src="images/plus.png" />
<asp:Panel ID="pnlStates" runat="server" style="display:none">
<asp:DropDownList ID="ddlStates1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlStates1_OnSelectedIndexChanged" >
<asp:ListItem Text="Select State" Value=""></asp:ListItem>
</asp:DropDownList><br />
<asp:ListBox ID="lbCity" runat="server"></asp:ListBox>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
后端代码: -
protected void gvCountry1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string countryid = gvCountry1.DataKeys[e.Row.RowIndex].Value.ToString();
ddlStates1 = (DropDownList)e.Row.FindControl("ddlStates1");
lbCity=(ListBox)e.Row.FindControl("lbCity");
SqlDataAdapter adp = new SqlDataAdapter();
String conString = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
con.ConnectionString = conString;
String conCmd = "sp_SelectStateList";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = conCmd;
cmd.Parameters.AddWithValue("@countryid", SqlDbType.Int).Value = countryid;
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
adp.SelectCommand = cmd;
DataSet ds = new DataSet();
adp.Fill(ds);
con.Open();
ddlStates1.DataSource = cmd.ExecuteReader();
ddlStates1.DataTextField = "State";
ddlStates1.DataValueField = "ID";
ddlStates1.DataBind();
con.Close();
ddlStates1.SelectedIndexChanged += ddlStates1_OnSelectedIndexChanged;
conCmd = "sp_SelectCityList";
cmd = new SqlCommand();
cmd.CommandText = conCmd;
string stateid = ddlStates1.SelectedValue;
Session["stateid"] = stateid;
cmd.Parameters.AddWithValue("@stateid", SqlDbType.Int).Value = stateid;
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
adp.SelectCommand = cmd;
ds = new DataSet();
adp.Fill(ds);
con.Open();
lbCity.DataSource = cmd.ExecuteReader();
lbCity.DataTextField = "City";
lbCity.DataValueField = "ID";
lbCity.DataBind();
con.Close();
ddlStates1.ViewStateMode = ViewStateMode.Disabled;
}
}
protected void ddlStates1_OnSelectedIndexChanged(object sender, EventArgs e)
{
string stateid = ddlStates1.SelectedValue;
SqlDataAdapter adp = new SqlDataAdapter();
String conString = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
con.ConnectionString = conString;
String conCmd = "sp_SelectCityList";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = conCmd;
cmd.Parameters.AddWithValue("@stateid", SqlDbType.Int).Value = stateid;
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
adp.SelectCommand = cmd;
DataSet ds = new DataSet();
adp.Fill(ds);
con.Open();
lbCity.DataSource = cmd.ExecuteReader();
lbCity.DataTextField = "City";
lbCity.DataValueField = "ID";
lbCity.DataBind();
con.Close();
ddlStates1.ViewStateMode = ViewStateMode.Enabled;
}
我是c#.net的新手。在我错的地方帮助我