2011-12-06 17 views
1

グリッドビューを含むページがあるので、girdviewは、Webサービスを介してオブジェクトデータソースからデータを取得します。これには、itemtemplatesを持つ2つの列が含まれています。編集モードのときにこれらのテンプレートのそれぞれに、編集アイテムテンプレートの下に格納されるドロップダウンリストを含みます。グリッドビューを正しくデータバインドするにはどうすればいいですか?

編集モードにあり、アップデートイベントを発生させるときを除いて、すべての機能が正常に動作しているように見えます。ドロップダウンリストはnull参照エラーをスローします。それ以上の調査では、コードクローンがdropdroplistsを見つけることができないと考えていることが原因であるようです。さらに、デバッガを掘り下げると、ドロップダウンリストも存在しないように見えます。 私のページでは、編集モードのときにドロップダウンリストが表示されますが、コードビハインドはそれらのコントロールを見つけることができません。

私が推測しているのは、間違った場所にグリッドビューをバインドしているか、バインディング自体が正しく行われていないということです。

私が知りたいことは、なぜこのような状況が起こり、どうすれば修正できるのでしょうか? 私のバインディングを正しく行っているかどうか誰かが私に知らせることができたら、私はまた感謝します。

以下は、gridviewとそのコードビハインドの両方のための私のコードを見つけるでしょう。

のGridView:

<asp:GridView ID="GridViewHolder" 
         runat="server" 
         AllowPaging="True" 
         AutoGenerateColumns="False" 
         BackColor="Transparent" 
         BorderColor="#999999" 
         BorderStyle="Ridge" 
         BorderWidth="3px" 
         CellPadding="4" 
         CellSpacing="2" 
         DataSourceID="MachineDataSet" 
         ForeColor="Black" 
         HeaderStyle-HorizontalAlign="Center" 
         HorizontalAlign="Center" 
         RowStyle-HorizontalAlign="Center" 
         Width="796px" 
         OnRowUpdating="GridViewHolder_Updating" 
         OnRowCancelingEdit="GridViewHolder_Canceling" 
         OnRowEditing="GridViewHolder_Editing" 
         OnRowCommand="GridViewHolder_RowCommand"              
         EnableViewState="False"> 
      <RowStyle BackColor="Transparent" 
         HorizontalAlign="Center" /> 
      <Columns> 
       <asp:TemplateField HeaderText="ID" 
            SortExpression="ID" 
            Visible="False"> 
        <ItemTemplate> 
         <asp:Label ID="MachineIDLabel" 
            runat="server" 
            Text='<%# Bind("ID") %>' 
            Visible="false"></asp:Label> 
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:TextBox ID="MachineIDText" 
            runat="server" 
            Text='<%# Bind("ID") %>'> 
         </asp:TextBox> 
        </EditItemTemplate> 
       </asp:TemplateField> 
       <asp:BoundField DataField="SiteName" 
           HeaderText="Site Name" 
           SortExpression="SiteName" 
           ReadOnly="true" /> 
       <asp:BoundField DataField="Name" 
           HeaderText="Machine Name" 
           ReadOnly="true" 
           SortExpression="Name" /> 
       <asp:TemplateField HeaderText="Machine Type" 
            SortExpression="MachineType"> 
        <ItemTemplate> 
         <asp:Label ID="MachineTypeLabel" 
            runat="server" 
            Text='<%# Bind("MachineType") %>'> 
         </asp:Label>        
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:DropDownList ID="MachineTypeDropDown" 
              runat="server" 
              AppendDataBoundItems="True"             
              Height="21px" 
              Width="217px" 
              DataSourceID="GetMachineType" 
              DataTextField="Name"            
              DataValueField="ID">            
          <asp:ListItem Enabled="true" 
              Text="Select a Machine Type." 
              Value="empty"> 
          </asp:ListItem> 
         </asp:DropDownList> 
        </EditItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Machine Model" SortExpression="MachineModel"> 
        <ItemTemplate> 
         <asp:Label ID="MachineModelLabel" 
            runat="server" 
            Text='<%# Bind("MachineModel") %>'> 
         </asp:Label>        
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:DropDownList ID="MachineModelDropDown" 
              runat="server" 
              AppendDataBoundItems="True"             
              Height="21px" Width="217px" 
              DataSourceID="GetMachineModel" 
              DataTextField="Name" 
              DataValueField="ID">            
          <asp:ListItem Enabled="true" 
              Text="Select a Machine Model." 
              Value="empty"> 
          </asp:ListItem> 
         </asp:DropDownList> 
        </EditItemTemplate> 
       </asp:TemplateField> 
       <asp:CommandField ButtonType="Button" 
            ShowEditButton="True" 
            CausesValidation="false" > 
        <ItemStyle HorizontalAlign="Center" 
           Wrap="True" /> 
       </asp:CommandField> 
      </Columns> 
      <FooterStyle BackColor="Transparent" /> 
      <PagerStyle BackColor="Transparent" 
         ForeColor="Black" 
         HorizontalAlign="Left" /> 
      <SelectedRowStyle BackColor="Transparent" 
           Font-Bold="True" 
           ForeColor="White" /> 
      <HeaderStyle BackColor="Black" 
         Font-Bold="True" 
         ForeColor="White" 
         HorizontalAlign="Center" /> 
    </asp:GridView> 

分離コード:

Pageload方法:

 /// <summary> 
    /// Handles the Load event of the Page control. 
    /// </summary> 
    /// <param name="sender">The source of the event.</param> 
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     logger.Debug("Entering Page_Load"); 
     Boolean loginRequired = true; 
     // If no login is required set the session variable and proceed to the main page. 
     string str = ConfigurationManager.AppSettings["i0"] as string; 
     if (!string.IsNullOrEmpty(str)) 
     { 
      string flag = MyExtensions.Decrypt(str, true); 
      if ("false".Equals(flag, StringComparison.InvariantCultureIgnoreCase)) 
       loginRequired = true; 
      else 
      { 
       loginRequired = false; 

       // User logged in so check the permissions. 
       UserInfo user = (UserInfo)Session[Constants.LOGGEDINUSER]; 
       if (null == user) 
        loginRequired = true; 
       else 
       { 
        string groupId = user.GroupId; 
        if (string.IsNullOrEmpty(groupId)) 
         loginRequired = true; 
        else if (!"Admins".Equals(user.GroupId) && !"Engineer".Equals(user.GroupId)) 
         loginRequired = true; 
       } 
      } 
     } 
     if (!Page.IsPostBack) 
     { 
      Control ctrl = MyExtensions.FindControlRecursive(this, "CommissioningLoginPanel"); 
      Panel loginPanel = null; 
      Panel contentPanel = null; 

      if (null != ctrl) 
      { 
       loginPanel = (Panel)ctrl; 
       ctrl = MyExtensions.FindControlRecursive(this, "CommissioningPanel"); 
       if (null != ctrl) 
        contentPanel = (Panel)ctrl; 
      } 

      if (loginRequired) 
      { 
       if (null != loginPanel) 
        loginPanel.Visible = true; 
       if (null != contentPanel) 
        contentPanel.Visible = false; 
      } 
      else 
      { 
       if (null != loginPanel) 
        loginPanel.Visible = false; 
       if (null != contentPanel) 
        contentPanel.Visible = true; 
      }     
     } 
     BindData(); 
     logger.Debug("Leaving Page_Load"); 
    } 

GridViewのイベント:

 /// <summary> 
    /// Handles the Click event of the update button under edit in the gridview control. 
    /// </summary> 
    /// <param name="sender">The source of the event.</param> 
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewUpdateEventArgs"/> instance containing the event data.</param> 
    protected void GridViewHolder_Updating(object sender, GridViewUpdateEventArgs e) 
    { 
     logger.Debug("Entering GridviewHolder_Updating"); 
     int machineid; 
     string machineid1; 
     string machineTypeid; 
     string machineModelid; 

     //retrieve and set the data 
     GridViewHolder.EditIndex = e.RowIndex; 

     try 
     { 

      GridViewRow row = (GridViewRow)GridViewHolder.Rows[e.RowIndex]; 
      TextBox mID = row.FindControl("MachineIDText") as TextBox; 
      DropDownList mType = row.FindControl("MachineTypeDropDown") as DropDownList; 
      DropDownList mModel = row.FindControl("MachineModelDropDown") as DropDownList; 

      machineid1 = mID.Text; 
      machineid = Convert.ToInt32(machineid1); 
      machineTypeid = mType.SelectedValue; 
      machineModelid = mModel.SelectedValue; 


      try 
      { 
       if (machineTypeid != "empty" || machineModelid != "empty") 
       { 
        if (machineTypeid != "empty") 
        { 
         inputsService.UpdateMachineTypes(machineid, machineTypeid); 
        } 
        if (machineModelid != "empty") 
        { 
         inputsService.UpdateMachineModels(machineid, machineModelid); 
        } 
        UpdateSucceed.Visible = true; 
        logger.Debug("Updating - Database successfully updated!"); 
       } 
       else 
       { 
        UpdateFail.Visible = true; 
        logger.Debug("Updating - Database had no data selected to be updated."); 
       } 
      } 
      catch (Exception ex) 
      { 
       logger.ErrorFormat("Updating - Failed to update the table, ex = {0}", ex); 
      } 
     } 
     catch (Exception ex) 
     { 
      logger.ErrorFormat("Updating.gathering page controls - Failed to update the table, ex = {0}", ex); 
     } 
     logger.Debug("Leaving GridViewHolder_Updating"); 
    } 

    /// <summary> 
    /// Handles the Click event of the cancel button under edit in the gridview control. 
    /// </summary> 
    /// <param name="sender">The source of the event.</param> 
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCancelEditEventArgs"/> instance containing the event data.</param> 
    protected void GridViewHolder_Canceling(object sender, GridViewCancelEditEventArgs e) 
    { 
     logger.Debug("Entering GridViewHolder_Canceling"); 
     //reset the edit index 
     GridViewHolder.EditIndex = -1; 
     //Bind data to GridViewHolder 
     BindData(); 
     logger.Debug("Leaving GridViewHolder_Canceling"); 
    } 

    /// <summary> 
    /// Handles the Click event of the cancel button under edit in the gridview control. 
    /// </summary> 
    /// <param name="sender">The source of the event.</param> 
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewEditEventArgs"/> instance containing the event data.</param> 
    protected void GridViewHolder_Editing(object sender, GridViewEditEventArgs e) 
    { 
     logger.Debug("Entering GridViewHolder_Editing");   
     //set the edit index to a new value 
     GridViewHolder.EditIndex = e.NewEditIndex; 
     //Bind data to gridviewholder 
     BindData(); 
     logger.Debug("Leaving GridViewHolder_Editing"); 
    } 

BindData方法:

private void BindData() 
    { 
     logger.Debug("Entering DataBind"); 
     GridViewHolder.DataSource = Session["MachineTable"]; 
     GridViewHolder.DataBind(); 
     logger.Debug("Leaving DataBind"); 
    } 

ご協力いただきありがとうございます。

答えて

1

あなたのGridViewでEnableViewState="True"を設定してみましたありがとう!

ビューステートを有効にしないでポストバックが発生すると、ポストバックが発生したときにドロップダウンリストの状態がわからないことがあります。

+0

とのGridViewをpopulationgているテーブルのIDにGridViewののDataKeyNamesプロパティを設定してみてくださいでした編集モードに入り、更新またはキャンセルしようとしています。それは何かについて言及してロードされているコントロールツリーは、最初のビューステートを保存するために使用されたコントロールツリーと一致する必要があります – James213

+0

おそらく、!Page.IsPostbackの場合にのみデータをバインドし、 – plntxt

+0

私はそれを試してみよう、お返事ありがとうございます。 – James213

1

あなたは私が持っている、しかし、私が行うとき、それは時にビューステートエラーのロードに失敗投げあなたが例えば

<asp:GridView ID="gvEmployees" runat="server" DataKeyNames="EmployeeId" ... 
+0

さて、私はそれを試してみましたが、何か間違っていない限り、それは目に見えるものに見えませんでした。私はdatakeynamesを設定しています=テーブルのプライマリキーに私が動作しようとしている – James213

関連する問題