2017-03-24 14 views
0

我無法弄清楚我在這裏做錯了什麼。試圖動態地填充GridView中的下拉列表,並一直告訴我對象引用未設置爲對象的實例。GridView內的DropDownList不被視爲對象

首先,我的ASPX:現在

<asp:Panel runat="server" ID="ShowDiv3" Visible="false" BorderStyle="Solid" BorderWidth="0" Width="1389px"> 
     <asp:Label ID="lblShowDiv3Title" runat="server" Text="Root Causes: " Font-Bold="true"></asp:Label><asp:DropDownList ID="ddlRootCauses" runat="server" Width="300px" OnSelectedIndexChanged="ddlRootCauses_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList> 
     <br /> 
     <%-- This line needed to be commented out when paging was removed: AllowPaging="True" AllowCustomPaging="True" PageSize="10" --%> 
     <div id="divGrid3" style='width:1290px; overflow:auto'> 
    <asp:GridView ID="DataGrid_RootCauses" runat="server" 
     AllowSorting="True" OnSorting="DataGrid_RootCauses_Sorting" AutoGenerateColumns="False" ShowFooter ="true" CellPadding="1" 
     CssClass="hoverTable" 
     HeaderStyle-BackColor="#4DA6A6" HeaderStyle-BorderColor="#4DA6A6" 
     HeaderStyle-Font-Size="Small" HeaderStyle-ForeColor="White" 
     FooterStyle-BackColor="#4DA6A6" FooterStyle-BorderColor="#4DA6A6" FooterStyle-ForeColor="White" 
     DataKeyNames="root_cause_id" 
         OnRowDataBound="DataGrid_RootCauses_RowDataBound" 
     OnRowCancelingEdit="DataGrid_RootCauses_CancelCommand" 
     OnRowEditing="DataGrid_RootCauses_EditCommand" 
     OnRowDeleting="DataGrid_RootCauses_DeleteCommand" 
     OnRowUpdating="DataGrid_RootCauses_UpdateCommand"> 
      <Columns> 

      <asp:TemplateField HeaderText="LPI Due"> 
      <ItemStyle HorizontalAlign="Center"></ItemStyle> 
      <ItemTemplate> 
       <asp:Label ID="lbl_LPI_DUE" runat="server" Width="70px" Text='<%#Eval("LPI_DUE") %>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:DropDownList ID="ddl_RC_LPI_DUE" runat="server" Width="70px" Text='<%#Eval("LPI_DUE") %>'> 
        <asp:ListItem Value=""> - </asp:ListItem> 
        <asp:ListItem Value="N"> N </asp:ListItem> 
        <asp:ListItem Value="Y"> Y </asp:ListItem> 
       </asp:DropDownList> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:DropDownList ID="nddl_RC_LPI_DUE" runat="server" Width="70px" > 
        <asp:ListItem Value=""> - </asp:ListItem> 
        <asp:ListItem Value="N"> N </asp:ListItem> 
        <asp:ListItem Value="Y"> Y </asp:ListItem> 
       </asp:DropDownList> 
      </FooterTemplate> 
     </asp:TemplateField>      

     <asp:TemplateField HeaderText="Root Cause Category"> 
      <ItemTemplate> 
       <asp:Label ID="lbl_LPI_Category" runat="server" Width="170px" Text='<%#Eval("LPI_Category") %>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:DropDownList ID="ddl_RC_LPI_Category" runat="server" Width="170px" Text='<%#Eval("LPI_Category") %>'></asp:DropDownList> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:DropDownList ID="nddl_RC_LPI_Category" runat="server" Width="170px" ></asp:DropDownList> 
      </FooterTemplate> 
     </asp:TemplateField>     

     <asp:TemplateField HeaderText="Root Cause Reason"> 
      <ItemTemplate> 
       <asp:Label ID="lbl_LPI_Reason" runat="server" Width="370px" Text='<%#Eval("LPI_reason") %>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:TextBox ID="txt_RC_LPI_Reason" runat="server" Width="370px" Text='<%#Eval("LPI_reason") %>'></asp:TextBox> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:TextBox ID="ntxt_RC_LPI_Reason" runat="server" Width="370px" ></asp:TextBox> 
      </FooterTemplate> 
     </asp:TemplateField>     

     <asp:TemplateField HeaderText="Business Owned"> 
      <ItemStyle HorizontalAlign="Center"></ItemStyle> 
      <ItemTemplate> 
       <asp:Label ID="lbl_Business" runat="server" Width="70px" Text='<%#Eval("Business") %>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:DropDownList ID="ddl_RC_Business" runat="server" Width="70px" Text='<%#Eval("Business") %>'> 
        <asp:ListItem Value=""> - </asp:ListItem> 
        <asp:ListItem Value="N"> N </asp:ListItem> 
        <asp:ListItem Value="Y"> Y </asp:ListItem> 
       </asp:DropDownList> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:DropDownList ID="nddl_RC_Business" runat="server" Width="70px" > 
        <asp:ListItem Value=""> - </asp:ListItem> 
        <asp:ListItem Value="N"> N </asp:ListItem> 
        <asp:ListItem Value="Y"> Y </asp:ListItem> 
       </asp:DropDownList> 
      </FooterTemplate> 
     </asp:TemplateField> 

     <asp:TemplateField HeaderText="CP Owned"> 
     <ItemStyle HorizontalAlign="Center"></ItemStyle> 
      <ItemTemplate> 
       <asp:Label ID="lbl_CP" runat="server" Width="70px" Text='<%#Eval("CP") %>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:DropDownList ID="ddl_RC_CP" runat="server" Width="70px" Text='<%#Eval("CP") %>'> 
        <asp:ListItem Value=""> - </asp:ListItem> 
        <asp:ListItem Value="N"> N </asp:ListItem> 
        <asp:ListItem Value="Y"> Y </asp:ListItem> 
       </asp:DropDownList> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:DropDownList ID="nddl_RC_CP" runat="server" Width="70px" > 
        <asp:ListItem Value=""> - </asp:ListItem> 
        <asp:ListItem Value="N"> N </asp:ListItem> 
        <asp:ListItem Value="Y"> Y </asp:ListItem> 
       </asp:DropDownList> 
      </FooterTemplate> 
     </asp:TemplateField>      

     <asp:TemplateField HeaderText="Non-CP Owned"> 
     <ItemStyle HorizontalAlign="Center"></ItemStyle> 
      <ItemTemplate> 
       <asp:Label ID="lbl_Non_CP" runat="server" Width="70px" Text='<%#Eval("Non_CP") %>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:DropDownList ID="ddl_RC_Non_CP" runat="server" Width="70px" Text='<%#Eval("Non_CP") %>'> 
        <asp:ListItem Value=""> - </asp:ListItem> 
        <asp:ListItem Value="N"> N </asp:ListItem> 
        <asp:ListItem Value="Y"> Y </asp:ListItem> 
       </asp:DropDownList> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:DropDownList ID="nddl_RC_Non_CP" runat="server" Width="70px" > 
        <asp:ListItem Value=""> - </asp:ListItem> 
        <asp:ListItem Value="N"> N </asp:ListItem> 
        <asp:ListItem Value="Y"> Y </asp:ListItem> 
       </asp:DropDownList> 
      </FooterTemplate> 
     </asp:TemplateField> 

     <asp:TemplateField HeaderText="Proclaim"> 
     <ItemStyle HorizontalAlign="Center"></ItemStyle> 
      <ItemTemplate> 
       <asp:Label ID="lbl_Proclaim" runat="server" Width="70px" Text='<%#Eval("Proclaim") %>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:DropDownList ID="ddl_RC_Proclaim" runat="server" Width="70px" Text='<%#Eval("Proclaim") %>'> 
        <asp:ListItem Value=""> - </asp:ListItem> 
        <asp:ListItem Value="N"> N </asp:ListItem> 
        <asp:ListItem Value="Y"> Y </asp:ListItem> 
       </asp:DropDownList> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:DropDownList ID="nddl_RC_Proclaim" runat="server" Width="70px" > 
        <asp:ListItem Value=""> - </asp:ListItem> 
        <asp:ListItem Value="N"> N </asp:ListItem> 
        <asp:ListItem Value="Y"> Y </asp:ListItem> 
       </asp:DropDownList> 
      </FooterTemplate> 
     </asp:TemplateField> 

     <asp:TemplateField HeaderText="PMHS"> 
     <ItemStyle HorizontalAlign="Center"></ItemStyle> 
      <ItemTemplate> 
       <asp:Label ID="lbl_PMHS" runat="server" Width="70px" Text='<%#Eval("PMHS") %>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:DropDownList ID="ddl_RC_PMHS" runat="server" Width="70px" Text='<%#Eval("PMHS") %>'> 
        <asp:ListItem Value=""> - </asp:ListItem> 
        <asp:ListItem Value="N"> N </asp:ListItem> 
        <asp:ListItem Value="Y"> Y </asp:ListItem> 
       </asp:DropDownList> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:DropDownList ID="nddl_RC_PMHS" runat="server" Width="70px" > 
        <asp:ListItem Value=""> - </asp:ListItem> 
        <asp:ListItem Value="N"> N </asp:ListItem> 
        <asp:ListItem Value="Y"> Y </asp:ListItem> 
       </asp:DropDownList> 
      </FooterTemplate> 
     </asp:TemplateField> 

     <asp:TemplateField HeaderText="Facets"> 
     <ItemStyle HorizontalAlign="Center"></ItemStyle> 
      <ItemTemplate> 
       <asp:Label ID="lbl_Facets" runat="server" Width="70px" Text='<%#Eval("Facets") %>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:DropDownList ID="ddl_RC_Facets" runat="server" Width="70px" Text='<%#Eval("Facets") %>'> 
        <asp:ListItem Value=""> - </asp:ListItem> 
        <asp:ListItem Value="N"> N </asp:ListItem> 
        <asp:ListItem Value="Y"> Y </asp:ListItem> 
       </asp:DropDownList> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:DropDownList ID="nddl_RC_Facets" runat="server" Width="70px" > 
        <asp:ListItem Value=""> - </asp:ListItem> 
        <asp:ListItem Value="N"> N </asp:ListItem> 
        <asp:ListItem Value="Y"> Y </asp:ListItem> 
       </asp:DropDownList> 
      </FooterTemplate> 
     </asp:TemplateField> 

     <asp:TemplateField ItemStyle-Width="90px"> 
     <ItemTemplate> 
      <asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" /> 
     </ItemTemplate> 
     <EditItemTemplate> 
      <asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/> 
      <asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/> 
     </EditItemTemplate> 
     <FooterTemplate> 
      <asp:Button ID="btn_Add" runat="server" Text="Add" OnClick="DataGrid_RootCauses_RowCreated" /> 
     </FooterTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField> 
     <ItemTemplate> 
      <asp:Button ID="btn_Delete" runat="server" OnClientClick="javascript:return confirm('Are you sure?');" Text="Delete" CommandName="Delete" /> 
     </ItemTemplate> 
     </asp:TemplateField>   
     <asp:TemplateField HeaderText="Cause ID"> 
     <ItemTemplate> 
      <asp:Label ID="lbl_Cause_ID" runat="server" Text='<%#Eval("root_cause_id") %>'></asp:Label> 
     </ItemTemplate> 
     </asp:TemplateField>   

     </Columns> 
    </asp:GridView> 
    <asp:Label ID="lblEmpty1" runat="server" Visible="false" Style="font-weight:bold; font-size:large;"></asp:Label> 
    </div> 
</asp:Panel> 

,後臺代碼:

protected void ddlRootCauses_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    LoadRootCauseGrid(); 

    OracleConnection conn = GetConnection(); 
    try 
    { 
     { 
      // ddlRootCauses 
      OracleCommand cmd6 = new OracleCommand(); 
      cmd6.CommandType = CommandType.StoredProcedure; 
      cmd6.CommandText = "SP_LPI_MAINT_LIST_ROOTCAUSE"; 
      cmd6.Connection = conn; 

      //cmd3.Parameters.Add("v_Lookup_Value", OracleType.VarChar, 20).Value = "LEAD"; 
      cmd6.Parameters.Add("vRootCauseList", OracleType.Cursor).Direction = ParameterDirection.Output; 

      var SearchAdapter6 = new OracleDataAdapter(cmd6); 
      var ds6 = new DataTable(); 
      SearchAdapter6.Fill(ds6); 

      DataView view = new DataView(ds6); 
      DataTable distinctValues = new DataTable(); 
      distinctValues = view.ToTable(true, "LPI_Category"); 

      // Fill all of the dropdowns from the same data adapter 
      { 
       //This one works fine 
       ddlRootCauses.DataSource = distinctValues; 
       ddlRootCauses.DataTextField = "LPI_Category"; 
       ddlRootCauses.DataValueField = "LPI_Category"; 
       ddlRootCauses.DataBind(); 
       ddlRootCauses.Items.Insert(0, new ListItem("All", "")); 

       //This one send me down to the Catch block on the first line 
       ddl_RC_LPI_Category.DataSource = distinctValues; 
       ddl_RC_LPI_Category.DataTextField = "LPI_Category"; 
       ddl_RC_LPI_Category.DataValueField = "LPI_Category"; 
       ddl_RC_LPI_Category.DataBind(); 
       ddl_RC_LPI_Category.Items.Insert(0, new ListItem("All", "")); 

       nddl_RC_LPI_Category.DataSource = distinctValues; 
       nddl_RC_LPI_Category.DataTextField = "LPI_Category"; 
       nddl_RC_LPI_Category.DataValueField = "LPI_Category"; 
       nddl_RC_LPI_Category.DataBind(); 
       nddl_RC_LPI_Category.Items.Insert(0, new ListItem("All", "")); 

      } 

      ds6.Dispose(); 
      cmd6.Dispose(); 
      SearchAdapter6.Dispose(); 

      conn.Close(); 
     } 
    } 
    catch (Exception ex) 
    { 
      Response.Write(ex.Message); 
      conn.Close(); 
    } 

} 

奇怪的是,ddlRootCauses得到沒有問題的填充。但是,該下拉列表不在gridview中。我不明白爲什麼它在gridview中的下拉列表中遇到問題。 ,它不僅僅是因爲gridviews不喜歡下拉菜單;硬編碼列表項的Y/N編碼器工作正常。

+0

你是否嘗試從ddlRootCauses_SelectedIndexChanged事件填充網格的下拉列表? –

+0

是的。 ddlRootCauses是一個充當gridview過濾器的下拉菜單。一旦選擇了數據並對數據進行了過濾,下拉菜單ddl_RC_LPI_Category和nddl_RC_LPI_Category只能填充適當的項目,因此一旦ddlRootCauses更改就需要重新填充它們。 –

回答

0

由於控制是一個GridView裏面,你不能訪問他們像你這樣一個控逆變這是它的外(如ddlRootCauses

要填充你需要先用FindControl已找到他們的GridView裏面一個DropDownList。

protected void ddlRootCauses_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    //find the dropdownlist in the footer row 
    DropDownList ddl = DataGrid_RootCauses.FooterRow.FindControl("nddl_RC_LPI_Category") as DropDownList; 

    //or in a normal row you need to use an index 
    DropDownList ddl = DataGrid_RootCauses.Rows[5].FindControl("ddl_RC_LPI_Category") as DropDownList; 

    ddl.DataSource = distinctValues; 
    ddl.DataTextField = "LPI_Category"; 
    ddl.DataValueField = "LPI_Category"; 
    ddl.DataBind(); 
    ddl.Items.Insert(0, new ListItem("All", "")); 
} 

例如用的RowDataBound

首先,ASPX

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" ShowFooter="true"> 
    <Columns> 
    <asp:TemplateField HeaderText="Root Cause Reason"> 
     <ItemTemplate> 
      <asp:DropDownList ID="DropDownListItem" runat="server"></asp:DropDownList> 
     </ItemTemplate> 
     <EditItemTemplate> 
      <asp:DropDownList ID="DropDownListEditItem" runat="server"></asp:DropDownList> 
     </EditItemTemplate> 
     <FooterTemplate> 
      <asp:DropDownList ID="DropDownListFooter" runat="server"></asp:DropDownList> 
     </FooterTemplate> 
    </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

背後

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.Header) 
    { 
     //the header row 
    } 
    else if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     //normal row 
     if ((e.Row.RowState & DataControlRowState.Edit) > 0) 
     { 
      //normal row in edit mode 
      DropDownList ddl = e.Row.FindControl("DropDownListEditItem") as DropDownList; 

      ddl.DataSource = distinctValues; 
      ddl.DataTextField = "myValue"; 
      ddl.DataValueField = "ID"; 
      ddl.DataBind(); 
     } 
     else 
     { 
      //normal row 
      DropDownList ddl = e.Row.FindControl("DropDownListItem") as DropDownList; 

      ddl.DataSource = distinctValues; 
      ddl.DataTextField = "myValue"; 
      ddl.DataValueField = "ID"; 
      ddl.DataBind(); 
     } 
    } 
    else if (e.Row.RowType == DataControlRowType.Footer) 
    { 
     //footer row 
     DropDownList ddl = e.Row.FindControl("DropDownListFooter") as DropDownList; 

     ddl.DataSource = distinctValues; 
     ddl.DataTextField = "myValue"; 
     ddl.DataValueField = "ID"; 
     ddl.DataBind(); 
    } 
} 

或者在點擊按鈕的代碼。

protected void Button1_Click(object sender, EventArgs e) 
{ 
    foreach (GridViewRow row in GridView1.Rows) 
    { 
     if (row.RowType == DataControlRowType.DataRow) 
     { 
      DropDownList ddl = row.FindControl("DropDownListItem") as DropDownList; 

      ddl.DataSource = Common.LoadFromDB(); 
      ddl.DataTextField = "field01"; 
      ddl.DataValueField = "itemID"; 
      ddl.DataBind(); 
     } 
    } 
} 
+0

仍然收到相同的錯誤。而且,與此相關的一個問題是,我永遠不知道他們將選擇編輯哪一行,所以硬編碼行[5]不會是最優的。前端只有一個控制器; ddl_RC_LPI_Category。我真的需要連續選擇嗎?是否沒有辦法選擇控件,以便返回數據的所有記錄都會有一個下拉列表,其中包含相同的項目?令人意外的是,頁腳行的代碼完美運行。 –

+0

由於'ddl_RC_LPI_Category'位於編輯模板中,您可能會遇到錯誤。但是如果你想要所有的下拉菜單,你必須循環行或使用OnRowDataBound事件來填充它們。 – VDWWD

+0

你可以編輯你的答案,以顯示如何做? –

相關問題