GridView中DropDownList更新问题
gridview普通模板是label,点击更新是变成dropdownlist,并在后台绑定。代码: /// <summary>
/// 编辑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvBrand_RowEditing(object sender, GridViewEditEventArgs e)
{
gvBrand.EditIndex = e.NewEditIndex;
GetData();
DropDownList ddlServies = (DropDownList)gvBrand.Rows[gvBrand.EditIndex].Cells[2].FindControl("ddlServies");
Label lbCnameVisble = (Label)gvBrand.Rows[gvBrand.EditIndex].Cells[3].FindControl("lbCnameVisble");
dtBrand = brandBll.GetList("").Tables[0];
ddlServies.DataSource = dtBrand;
ddlServies.DataTextField = "Cname";
ddlServies.DataValueField = "BrandId";
ddlServies.DataBind();
ddlServies.SelectedValue = lbCnameVisble.Text.ToString();
}
点击保存:
/// <summary>
/// 保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvBrand_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string bId = gvBrand.DataKeys[e.RowIndex].Value.ToString();
DropDownList ddlServies = (DropDownList)gvBrand.Rows[gvBrand.EditIndex].Cells[2].FindControl("ddlServies");
TextBox txtName = (TextBox)gvBrand.Rows[gvBrand.EditIndex].Cells[1].FindControl("txtName");
try
{
bsModel.BsId =int.Parse(bId);
bsModel.BsName = txtName.Text.ToString();
string s = ddlServies.SelectedValue.ToString();--试了下,取不到值,
bsModel.BsBrandId = int.Parse(ddlServies.SelectedValue.ToString());--在这报错
bsBll.Update(bsModel);
}
catch(Exception ex)
{
throw ex;
}
} --------------------编程问答-------------------- 别沉了啊,各位高手来支招啊
--------------------编程问答-------------------- 楼主试下这中方式
:
string dept_code=(GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox).Text ; --------------------编程问答-------------------- string s = (GridView1.Rows[e.RowIndex].Cells[0].FindControl("DropDownList的ID") as DropDownList).SelectedValue; --------------------编程问答-------------------- Cells[0]是第一列,你按照你对应的列改下就行了 --------------------编程问答-------------------- 说不定不是取不到值,而是选定项的value是empty string呢?! --------------------编程问答-------------------- 变成Textbox我到试过,但dropdownlist还没试过 --------------------编程问答-------------------- <ItemTemplate>
<asp:DropDownList ID="ddlSort" runat="server" Width="98%">
</asp:DropDownList>
<asp:HiddenField ID="Hd_Sort" runat="server" Value='<%# Eval("BalSort")%>' />
</ItemTemplate>
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{ if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
{
((DropDownList)e.Row.FindControl("ddlSort")).SelectedValue = ((HiddenField)e.Row.FindControl("Hd_Sort")).Value;
}
}
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
string strSort = ((DropDownList)gv.Rows[e.NewEditIndex].FindControl("ddlSort")).SelectedValue.Trim();
gv.EditIndex = -1;
BindData();
}
补充:.NET技术 , ASP.NET