[英]Grid view Dropdown list data binding error
Am using the below code to bind the dropdown data from another table. 我正在使用以下代码从另一个表绑定下拉数据。 And also refer that control name using rowindex.
并且还使用rowindex引用该控件名称。 But it always return null.And also return the error message.
但是它总是返回null并返回错误消息。
`Object reference not set to an instance of an object.`
Am using the two method, but both return the control name null 我正在使用两种方法,但是都返回控件名称为null
First code: 第一个代码:
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Control ctrl = e.Row.FindControl("DDL_STATUS_FT"); //It always return null
if (ctrl != null)
{
DropDownList dd = ctrl as DropDownList;
DataSet7TableAdapters.sp_getall_trv_masterTableAdapter TA = new DataSet7TableAdapters.sp_getall_trv_masterTableAdapter();
DataSet7.sp_getall_trv_masterDataTable DS = TA.GetData();
dd.DataTextField = "fld_TName";
dd.DataValueField = "fld_id";
dd.DataSource = DS;
dd.DataBind();
}
}
}
Second : 第二:
In databind function
if (DS.Rows.Count > 0)
{
GridView2.DataSource = DS;
GridView2.DataBind();
foreach (GridViewRow grdRow in GridView2.Rows)
{
DataSet7TableAdapters.sp_getall_trv_masterTableAdapter TA1 = new DataSet7TableAdapters.sp_getall_trv_masterTableAdapter();
DataSet7.sp_getall_trv_masterDataTable DS1 = TA1.GetData();
// Nested DropDownList Control reference is passed to the DrdList object. This will allow you access the properties of dropdownlist placed inside the GridView Template column.
DropDownList drdList = (DropDownList)(GridView2.Rows[grdRow.RowIndex].Cells[4].FindControl("DDL_STATUS_FT"));//It always return null
// DataBinding of nested DropDownList Control for each row of GridView Control.
drdList.DataSource = DS1;
drdList.DataValueField = "fld_id";
drdList.DataTextField = "fld_TName";
drdList.DataBind();
}
}
Please help me to do this.. 请帮助我做到这一点。
<asp:TemplateField ItemStyle-Width="100px" HeaderText="TYPE">
<ItemTemplate>
<asp:DropDownList ID="DDL_STATUS" runat="server" AutoPostBack="true" Enabled="false" >
</asp:DropDownList>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DDL_edit_STATUS" runat="server" AutoPostBack="true" SelectedValue='<%# Eval("fld_Type") %>'>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="DDL_STATUS_FT" runat="server" AutoPostBack="true">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
The DropDown "DDL_STATUS_FT" is in Footer Template
..You must check it as follow.. 下拉
Footer Template
“ DDL_STATUS_FT”位于Footer Template
您必须按以下步骤进行检查。
if(e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ctrl =(DropDownList)e.Row.Cells[CellIndex].FindControl("DDL_STATUS_FT");
}
EDITED2 编辑2
you must have dropdownlist
in aspx code in gridview item-template 您必须在gridview item-template的aspx代码中具有
dropdownlist
you are finding using e.row.findcontrol
without declaring it so abusively it return null 您正在发现使用
e.row.findcontrol
没有对其进行如此滥用地声明它返回null
so, fist add dropdownlist into your gridview here is a sample of your dropdownlist 因此,首先将dropdownlist添加到您的gridview中,这是您的dropdownlist的示例
<asp:TemplateField ItemStyle-Width="30px" HeaderText="DDL_STATUS_FT">
<ItemTemplate>
<asp:Dropdownlist ID="DDL_STATUS_FT" runat="server" />
</ItemTemplate>
</asp:TemplateField>
if you got null ctrl
Control ctrl = e.Row.FindControl("DDL_STATUS_FT"); 如果您有空
ctrl
控件ctrl = e.Row.FindControl(“ DDL_STATUS_FT”); //It always return null //它总是返回null
then make sure in your aspx code DDL_STATUS_FT
Control is runat="server"
然后确保在您的aspx代码
DDL_STATUS_FT
控件中运行runat="server"
尝试通过cell和cellIndex查找控件,例如...
Control ctrl = e.Row.Cells[yourCellIndex].FindControl("DDL_STATUS_FT");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.