.aspxasp:GridView ID="GridViewSSort" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridViewSSort_RowCanelingEdit" OnRowEditing="GridViewSSort_RowEditing" OnRowUpdating="GridViewSSort_RowUpdating" OnRowDeleting="GridViewSSort_RowDeleting" OnRowDataBound="GridViewSSort_RowDataBound"  DataKeyNames="id" >
                              <Columns>
                                  <asp:BoundField DataField="id" HeaderText="ID" ReadOnly="True" SortExpression="id" />
                                  <asp:TemplateField HeaderText="大类名称">
                                      <ItemTemplate>
                                          <asp:HiddenField runat="server" ID="hidTxt" Value='<%#Bind("BSorID")%>' />
                                          <asp:DropDownList ID="ddlGVBSort" runat="server" >
                                          </asp:DropDownList>
                                      </ItemTemplate>
                                  </asp:TemplateField>
                                  <asp:BoundField DataField="SSortName" HeaderText="小类名称" SortExpression="SSortName" />
                                  <asp:CommandField ShowEditButton="True" />
                                  <asp:CommandField ShowDeleteButton="True" />
                              </Columns>
                              <EmptyDataTemplate> 
                                  <asp:DropDownList ID="ddlGVBSort" runat="server">
                                  </asp:DropDownList>
                              </EmptyDataTemplate>
                          </asp:GridView>
.cs更新
protected void GridViewSSort_RowUpdating(object sender, GridViewUpdateEventArgs e)  //小类
        {
            string strKeyId = GridViewSSort.DataKeys[e.RowIndex].Value.ToString();
            string strDDLValue = ((DropDownList)GridViewBSort.Rows[e.RowIndex].Cells[2].FindControl("ddlGVBSort")).SelectedValue;
            string strSSortName = ((TextBox)GridViewSSort.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString();
            string strUpdate = "UPDATE Tb_Train_SSort SET BSorID='"+ strDDLValue +"',SSortName='" + strSSortName + "' WHERE id=" + strKeyId;
            int intResult = this.ExecuteNonQuery(strUpdate, dsn, database);
}
//RowBind
if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes["onclick"] = "if(!confirm('你确定要删除“" + e.Row.Cells[1].Text + "”记录吗?')) return false;";
                }
                DropDownList dropTemp = (DropDownList)e.Row.Cells[0].FindControl("ddlGVBSort");
                string strSelect = "SELECT * FROM Tb_Train_BSort";
                DataTable dt = this.ExecuteReader(strSelect, dsn, database);
                if (dt != null && dt.Rows.Count > 0)
                {
                    dropTemp.DataSource = dt;
                    dropTemp.DataTextField = "BsortName";
                    dropTemp.DataValueField = "id";
                    dropTemp.DataBind();
                    dropTemp.SelectedValue = ((HiddenField)e.Row.FindControl("hidTxt")).Value.ToString();
                }
            }更新的时候报错:string strDDLValue = ((DropDownList)GridViewBSort.Rows[e.RowIndex].Cells[2].FindControl("ddlGVBSort")).SelectedValue;
为将对象引用实例,请问这个是怎么回事啊。

解决方案 »

  1.   

    FindControl("ddlGVBSort")的问题
    应该是这个控件没有找到,所以是空引用
      

  2.   

    this.GridView1.Rows[e.RowIndex].FindControl(“ddlGVBSort”)
    是不是列指定错了,试下在行里面FindControl
      

  3.   

    是列错了,应该是Cell[0],看你aspx页的代码,应该是[0]列
      

  4.   

    是不是因为 <asp:HiddenField runat="server" ID="hidTxt" Value=' <%#Bind("BSorID")%>' /> 的原因呢 <ItemTemplate> 
                                               <asp:HiddenField runat="server" ID="hidTxt" Value=' <%#Bind("BSorID")%>' /> 
                                               <asp:DropDownList ID="ddlGVBSort" runat="server" > 
                                               </asp:DropDownList> 
                                           </ItemTemplate> 
      

  5.   

    没错,应该是1.在行里FindControl也不行?
      

  6.   

    呵呵,昨天晚上正好在解决这个问题
    看看这篇文章你应该能找到答案http://www.cnblogs.com/dushouke/archive/2008/03/27/1126232.html
      

  7.   

    点编辑
    html代码
    <td>10</td><td>
                                              <input type="hidden" name="GridViewSSort$ctl05$hidTxt" id="GridViewSSort_ctl05_hidTxt" value="11" />
                                              <select name="GridViewSSort$ctl05$ddlGVBSort" id="GridViewSSort_ctl05_ddlGVBSort">
    <option selected="selected" value="11">管理</option>
    <option value="12">营销</option>
    <option value="13">技术</option>
    <option value="14">生产</option> </select>
                                          </td><td><input name="GridViewSSort$ctl05$ctl00" type="text" title="小类名称" /></td><td><a href="javascript:__doPostBack('GridViewSSort$ctl05$ctl01','')">更新</a>&nbsp;<a href="javascript:__doPostBack('GridViewSSort','Cancel$3')">取消</a></td><td></td>
      

  8.   

    问题解决
    由于控件名写错导致:
    ((DropDownList)GridViewBSort.Rows[e.RowIndex].Cells[2].FindControl("ddlGVBSort")).SelectedValue;
    改成:((DropDownList)GridViewSSort.Rows[e.RowIndex].Cells[2].FindControl("ddlGVBSort")).SelectedValue;