我在RowDataBound事件中填充每行ddl中的项,代码如下:if (e.Row.RowType != DataControlRowType.DataRow)
return;
GridViewRow gvr = e.Row;
DropDownList ddl = (DropDownList)gvr.FindControl("ddlRooter");
ListItem lirow = new ListItem("根分类", "0");
ddl.Items.Add(lirow);
int id = int.Parse(((HiddenField)gvr.FindControl("hfId")).Value);
foreach (CategoryModel c in cm)
{
if (c.CategoryIn == 0 && c.CategoryId != id)
{
lirow = new ListItem(c.CategoryName, c.CategoryId.ToString());
ddl.Items.Add(lirow);
}
}
string CgyIn = ((HiddenField)gvr.FindControl("hfIn")).Value;
for (int i = 0; i < ddl.Items.Count; i++)
{
if (ddl.Items[i].Value == CgyIn.ToString())
ddl.SelectedIndex = i;
}这时候如果我要编辑某行的值,并且修改的ddl的选中项,在RowUpdating中获取选中项的值,代码如下:GridViewRow gvr = gvCategoryManager.Rows[rowIndex];
int id = int.Parse(((HiddenField)gvr.FindControl("hfId")).Value);
if (id == 1)
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "delCgySuccess", "<script>$().mjShow('系统默认分类不允许修改!')</script>", false);
return;
}
string name = TextHandler.FilteHTML(((TextBox)gvr.FindControl("tbName")).Text, 20);
if (name.Length <= 0)
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "NewCgyRequired", "<script>$().mjShow('分类名称未填')</script>", false);
return;
}
DropDownList ddl = (DropDownList)gvr.FindControl("ddlRooter");
int rooterId = int.Parse(ddl.SelectedItem.Value);
CategoryModel updateCm = new CategoryModel(id, name, rooterId);
Category cgy = new Category();
if (cgy.UpdateCategory(updateCm) > 0)
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "UpdateCgySuccess", "<script>$().mjShow('更新分类成功')</script>", false);
else
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "UpdateCgyFailed", "<script>$().mjShow('更新分类失败')</script>", false);
问题出现了:ddl的又变回默认的选项了!我调试了老半天发现在DataBind()方法中会重新执行一遍RowDataBound,于是我把它放进了if(!IsPostBack)代码块中.
此时,RowDataBound不触发了,但是ddl的值并没有如我所愿的成为选中项,而还是默认选项.我已经不知所措了,求各位达人帮忙解决,小弟不甚感激!
return;
GridViewRow gvr = e.Row;
DropDownList ddl = (DropDownList)gvr.FindControl("ddlRooter");
ListItem lirow = new ListItem("根分类", "0");
ddl.Items.Add(lirow);
int id = int.Parse(((HiddenField)gvr.FindControl("hfId")).Value);
foreach (CategoryModel c in cm)
{
if (c.CategoryIn == 0 && c.CategoryId != id)
{
lirow = new ListItem(c.CategoryName, c.CategoryId.ToString());
ddl.Items.Add(lirow);
}
}
string CgyIn = ((HiddenField)gvr.FindControl("hfIn")).Value;
for (int i = 0; i < ddl.Items.Count; i++)
{
if (ddl.Items[i].Value == CgyIn.ToString())
ddl.SelectedIndex = i;
}这时候如果我要编辑某行的值,并且修改的ddl的选中项,在RowUpdating中获取选中项的值,代码如下:GridViewRow gvr = gvCategoryManager.Rows[rowIndex];
int id = int.Parse(((HiddenField)gvr.FindControl("hfId")).Value);
if (id == 1)
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "delCgySuccess", "<script>$().mjShow('系统默认分类不允许修改!')</script>", false);
return;
}
string name = TextHandler.FilteHTML(((TextBox)gvr.FindControl("tbName")).Text, 20);
if (name.Length <= 0)
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "NewCgyRequired", "<script>$().mjShow('分类名称未填')</script>", false);
return;
}
DropDownList ddl = (DropDownList)gvr.FindControl("ddlRooter");
int rooterId = int.Parse(ddl.SelectedItem.Value);
CategoryModel updateCm = new CategoryModel(id, name, rooterId);
Category cgy = new Category();
if (cgy.UpdateCategory(updateCm) > 0)
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "UpdateCgySuccess", "<script>$().mjShow('更新分类成功')</script>", false);
else
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "UpdateCgyFailed", "<script>$().mjShow('更新分类失败')</script>", false);
问题出现了:ddl的又变回默认的选项了!我调试了老半天发现在DataBind()方法中会重新执行一遍RowDataBound,于是我把它放进了if(!IsPostBack)代码块中.
此时,RowDataBound不触发了,但是ddl的值并没有如我所愿的成为选中项,而还是默认选项.我已经不知所措了,求各位达人帮忙解决,小弟不甚感激!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货