GridView中RowEditing中如何实例化DropDownList?
在编辑RowEditing中如何实例化DropDownList?
直接写
DropDownList ddl=(DropDownList)GridView1.FindControl( "DropDownList1 ");
DropDownList DDL1 = (DropDownList)GVW_ShowCard.Rows[e.NewEditIndex].Cells[5].FindControl( "DropDownList1 ");
上面二种写法都不行,但是下面却能取到相应的值,为什么啊?
string strValue = ((HtmlInputHidden)GVW_ShowCard.Rows[e.NewEditIndex].Cells[2].FindControl( "Hid1 ")).Value;
大家是如何写的呢?帮小弟一下吧,谢谢了!:)
------解决方案--------------------protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow gvr = GridView1.Rows[e.NewEditIndex];
DropDownList ddl = (DropDownList)gvr.Cells[你DropDownList放在列的索引].Controls[0];
string str=ddl.SelectItem.Text;
}
//Controls[0]代表单元格中的控件的索引,0代表第一个控件
------解决方案--------------------up
------解决方案--------------------参考以下代码:
protected void ddlDispOrder_DataBound(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
for (int i=0; i <= 999; i++)
{
ddl.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
GridViewRow det = (GridViewRow)ddl.NamingContainer;
ddl.SelectedValue = ((DataRowView)det.DataItem)[ "DispOrder "].ToString();
}
protected void gvResume_JobList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (Page.IsValid)
{
string strDispOrder = ((DropDownList)((GridView)sender).Rows[e.RowIndex].FindControl( "ddlDispOrder ")).SelectedValue;
e.NewValues[ "DispOrder "] = strDispOrder;
}
}
------解决方案--------------------实例化应该写在RowDataBound事件里而不是RowEditing事件里吧
if (((DropDownList)e.Row.FindControl( "ddlLb ")) != null)
{
DropDownList ddlLb = (DropDownList)e.Row.FindControl( "ddlLb ");
dbm_lb_dal dallb = new dbm_lb_dal();
DataTable dtLb;
dtLb = dallb.GetList( " ");
ddlLb.Items.Clear();
ddlLb.DataSource = dtLb;
ddlLb.DataTextField = "LBMC ";
ddlLb.DataValueField = "LBID ";
ddlLb.DataBind();
ddlLb.Items.Insert(0, new ListItem( " ", " "));
ddlLb.Items.FindByText(dtLb.Rows[0][ "LBMC "].ToString()).Selected = true;
dtLb = null;
dallb = null;
}
在RowUpdating事件里取值
string Lbmc = ((DropDownList)gdvXl.Rows[e.RowIndex].FindControl( "ddlLb ")).SelectedItem.Text;