(c#)asp.net里面gridview调用DropDownList级联事件
我们平时知道gridview里面有RowUpdating可以修改数据~但是在修改的行样式里面我们虽然可以编辑数据但是级联菜单的SelectedIndexChanged事件...
我们平时知道gridview里面有RowUpdating可以修改数据~但是在修改的行样式里面我们虽然可以编辑数据但是级联菜单的SelectedIndexChanged事件却不能正常使用~所以我发下面的代码给各位前辈和高手教我在事件里面调用并且创建DropDownList的SelectedIndexChanged事件~javascript比较复杂所以希望能得到最方便和快捷的方法得以实现两个下拉框的级联查询~
int AreaID = 0;
DropDownList DnArea;
DropDownList DnCity;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
this.Page.EnableViewState = true;
this.loadUser();
DnArea = (DropDownList)SmartGridView1.FindControl("DropDownList3");
DnCity = (DropDownList)SmartGridView1.FindControl("DropDownList4");
this.loadArea();
if (DropDownList1.Items.Count > 0)
{
AreaID = Convert.ToInt32(DropDownList1.SelectedValue);
this.loadCity_Change(AreaID);
}
}
}
public void loadArea() {
SqlConnection con = new SqlConnection(sqlHelper.Sqlopen);
SqlDataAdapter da = new SqlDataAdapter("select * from AreaTab ", con);
DataTable dt = new DataTable();
da.Fill(dt);
DropDownList1.DataSource = dt.DefaultView;
DropDownList1.DataTextField = "AreaName";
DropDownList1.DataValueField = "AreaID";
DropDownList1.DataBind();
}
protected void SmartGridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
SmartGridView1.EditIndex = e.NewEditIndex;
this.loadUser();
}
protected void SmartGridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int UserID = (int)SmartGridView1.DataKeys[e.RowIndex].Values[0];
//DropDownList Area = (((DropDownList)SmartGridView1.Rows[SmartGridView1.EditIndex].Cells[1].FindControl("DropDownList3")));
DnArea.AutoPostBack = true;
int AreaID =int.Parse(DnArea.SelectedValue.ToString());
this.loadCity1_Change(AreaID);
}
protected void DnArea_SelectedIndexChanged(object sender, EventArgs e) {
SqlConnection con = new SqlConnection(sqlHelper.Sqlopen);
SqlDataAdapter da = new SqlDataAdapter("select * from AreaTab ", con);
DataTable dt = new DataTable();
da.Fill(dt);
DnArea.DataSource = dt.DefaultView;
DnArea.DataTextField = "AreaName";
DnArea.DataValueField = "AreaID";
DnArea.DataBind();
}
在初始化的时候我想利用findControl的方式找~但无法找到~传统的
DropDownList Area = (((DropDownList)SmartGridView1.Rows[SmartGridView1.EditIndex].Cells[1].FindControl("DropDownList3")));方式只能找到控件~但是事件的调用和方式不明确所以希望高手能帮我创建一个在事件里面调用事件的方法 展开
int AreaID = 0;
DropDownList DnArea;
DropDownList DnCity;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
this.Page.EnableViewState = true;
this.loadUser();
DnArea = (DropDownList)SmartGridView1.FindControl("DropDownList3");
DnCity = (DropDownList)SmartGridView1.FindControl("DropDownList4");
this.loadArea();
if (DropDownList1.Items.Count > 0)
{
AreaID = Convert.ToInt32(DropDownList1.SelectedValue);
this.loadCity_Change(AreaID);
}
}
}
public void loadArea() {
SqlConnection con = new SqlConnection(sqlHelper.Sqlopen);
SqlDataAdapter da = new SqlDataAdapter("select * from AreaTab ", con);
DataTable dt = new DataTable();
da.Fill(dt);
DropDownList1.DataSource = dt.DefaultView;
DropDownList1.DataTextField = "AreaName";
DropDownList1.DataValueField = "AreaID";
DropDownList1.DataBind();
}
protected void SmartGridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
SmartGridView1.EditIndex = e.NewEditIndex;
this.loadUser();
}
protected void SmartGridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int UserID = (int)SmartGridView1.DataKeys[e.RowIndex].Values[0];
//DropDownList Area = (((DropDownList)SmartGridView1.Rows[SmartGridView1.EditIndex].Cells[1].FindControl("DropDownList3")));
DnArea.AutoPostBack = true;
int AreaID =int.Parse(DnArea.SelectedValue.ToString());
this.loadCity1_Change(AreaID);
}
protected void DnArea_SelectedIndexChanged(object sender, EventArgs e) {
SqlConnection con = new SqlConnection(sqlHelper.Sqlopen);
SqlDataAdapter da = new SqlDataAdapter("select * from AreaTab ", con);
DataTable dt = new DataTable();
da.Fill(dt);
DnArea.DataSource = dt.DefaultView;
DnArea.DataTextField = "AreaName";
DnArea.DataValueField = "AreaID";
DnArea.DataBind();
}
在初始化的时候我想利用findControl的方式找~但无法找到~传统的
DropDownList Area = (((DropDownList)SmartGridView1.Rows[SmartGridView1.EditIndex].Cells[1].FindControl("DropDownList3")));方式只能找到控件~但是事件的调用和方式不明确所以希望高手能帮我创建一个在事件里面调用事件的方法 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询