本文介绍了网格视图中绑定下拉列表中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有网格,客户端下拉列表就在那里。编辑网格行时收到错误 请找到以下代码 C#grid < asp:TemplateField HeaderText = 客户< span style ='颜色:红色'> * < / span> HeaderStyle-Font-Bold = true > < ItemTemplate> < asp:标签ID = lblClient runat = server Width = 90 Text = ' <%#Bind(Client_Name)%>' 启用= true CssClass = control-label > < / asp:标签 > < / ItemTemplate > < EditItemTemplate中> < asp:DropDownList ID = ddlClients runat = server > < / asp:DropDownList > < asp:RequiredFieldValidator ID = frClient runat = server ErrorMessage = 客户端字段必需 ControlToValidate = ddlClients显示= 无 Text = * ValidationGroup = update > < / asp:RequiredFieldValidator > < / EditItemTemplate > < HeaderStyle Font-Bold = True > < / HeaderStyle > < / asp:TemplateField > 代码背后 受保护 void GrdViewProject_RowDataBound ( object sender,GridViewRowEventArgs e) { try { DataRowView drview = e.Row.DataItem as DataRowView; #region Active Inactive Code if (e.Row.RowType == DataControlRowType.DataRow) { CheckBox chk =(CheckBox)e.Row.FindControl( chkActive); if (Convert.ToBoolean(drview [ Is_Active])== true ) { chk.Checked = 真; } else { chk.Checked = false ; } } #endregion if (e.Row.RowType == DataControlRowType.DataRow) { clsProject objProject1 = new clsProject(); DataSet objDataSet1 = new DataSet(); objDataSet1 = objProject1.FetchClients(); DropDownList ddlClients =(DropDownList)e.Row.FindControl( ddlClients); if (objDataSet1.Tables [ 0 ]。Rows.Count > 0 ) { ddlClients.Items.Clear(); ddlClients.DataSource = objDataSet1; ddlClients.DataValueField = Client_ID; ddlClients.DataTextField = Client_Name; ddlClients.DataBind(); } } } catch (例外情况) { throw ex; } } 错误消息 来自ddlClients.Items.Clear()的; line im得到以下错误消息 对象引用未设置为对象的实例 System.NullReferenceException:对象引用未设置为对象的实例。 请给我解决方案解决方案 如果你需要在编辑模板中找到控件,请检查 DataControlRowState 如下所示 if (e.Row.RowType == DataControlRowType.DataRow) { if ((e.Row.RowState& DataControlRowState.Edit)> 0 ) { // 在此处查找编辑控件... // DropDownList ddlClients =(DropDownList)e.Row.FindControl(ddlClients); } } I have grid in that client dropdown is there. while edit the grid row i m getting errorplease find below codeC# grid <asp:TemplateField HeaderText="Client <span style='color:Red'>*</span>" HeaderStyle-Font-Bold="true"> <ItemTemplate> <asp:Label ID="lblClient" runat="server" Width="90" Text='<%#Bind("Client_Name") %>' Enabled="true" CssClass="control-label"> </asp:Label> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID = "ddlClients" runat = "server"> </asp:DropDownList> <asp:RequiredFieldValidator ID="frClient" runat="server" ErrorMessage="client field Required" ControlToValidate="ddlClients" Display="None" Text="*" ValidationGroup="update"></asp:RequiredFieldValidator> </EditItemTemplate> <HeaderStyle Font-Bold="True"></HeaderStyle> </asp:TemplateField>code behindprotected void GrdViewProject_RowDataBound(object sender, GridViewRowEventArgs e) { try { DataRowView drview = e.Row.DataItem as DataRowView; #region Active Inactive Code if (e.Row.RowType == DataControlRowType.DataRow) { CheckBox chk = (CheckBox)e.Row.FindControl("chkActive"); if (Convert.ToBoolean(drview["Is_Active"]) == true) { chk.Checked = true; } else { chk.Checked = false; } } #endregion if (e.Row.RowType == DataControlRowType.DataRow) { clsProject objProject1 = new clsProject(); DataSet objDataSet1 = new DataSet(); objDataSet1 = objProject1.FetchClients(); DropDownList ddlClients = (DropDownList)e.Row.FindControl("ddlClients"); if (objDataSet1.Tables[0].Rows.Count > 0) { ddlClients.Items.Clear(); ddlClients.DataSource = objDataSet1; ddlClients.DataValueField = "Client_ID"; ddlClients.DataTextField = "Client_Name"; ddlClients.DataBind(); } } } catch (Exception ex) { throw ex; } }error message from ddlClients.Items.Clear(); line i m getting below error message Object reference not set to an instance of an objectSystem.NullReferenceException: Object reference not set to an instance of an object.please give me the solution 解决方案 if you need to find control inside edit template , check for DataControlRowState as belowif (e.Row.RowType == DataControlRowType.DataRow) { if ((e.Row.RowState & DataControlRowState.Edit) > 0) { // find edit controls here ... //DropDownList ddlClients = (DropDownList)e.Row.FindControl("ddlClients"); }} 这篇关于网格视图中绑定下拉列表中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-26 22:23