![](/img/trans.png)
[英]call dropdownlist selectedindexchanged event in gridview rowdatabound event?
[英]getting null value in dropdownlist on rowdatabound event using gridview
我收到錯誤消息:- "Object reference not set to an instance of an object."
當我想將數據綁定到dropdownlist
這會給我錯誤。 這是我的代碼:
protected void EventRequirementGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddllist = (DropDownList)e.Row.FindControl("modeldropdownlist");
ddllist.SelectedValue = DataBinder.Eval(e.Row.DataItem, "exammode").ToString();
}
}
這樣嘗試
protected void EventRequirementGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && && e.Row.RowState == DataControlRowState.Edit)
{
DropDownList ddllist = (DropDownList)e.Row.FindControl("modeldropdownlist");
ddllist.SelectedValue = DataBinder.Eval(e.Row.DataItem, "exammode").ToString();
}
}
(要么)
protected void EventRequirementGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && && e.Row.RowState == DataControlRowState.Edit)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList ddllist = (DropDownList)e.Row.FindControl("modeldropdownlist");
ddllist.SelectedValue = DataBinder.Eval(e.Row.DataItem, "exammode").ToString();
}
}
}
另外,您可以使用GridView的PreRender事件從EditItemTemplate訪問控件,例如:
protected void GridView1_PreRender(object sender, EventArgs e)
{
if (GridView1.EditIndex != -1)
{
//Just changed the index of cells based on your requirements
DropDownList ddllist = (DropDownList)e.Row.FindControl("modeldropdownlist");
ddllist.SelectedValue = DataBinder.Eval(e.Row.DataItem, "exammode").ToString();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.