2012-07-14 77 views
0

我是一名新的ASP.NET開發人員,我正在努力使用DropDownList更新GridView中的列。具體來說,我有一個GridView,我正在使用它來添加,刪除和更新/編輯員工信息。這些信息是員工用戶名,姓名,BadgeNo,JobTitle和DivisionShortcut。部門將列在DropDownList中,但我仍然無法更新此列,我不知道爲什麼。如何使用GridView中的DropDownList更新一列?

我有以下的數據庫設計:

Employee Table: Username, Name, JobTitle, BadgeNo, IsActive, DivisionCode 
Divisions Table: SapCode, DivisionShortcut 

我寫的代碼,我調試的代碼中沒有發現任何錯誤。但是,當我運行代碼並從下拉列表中選擇其中一個值時,該列不更新,我不知道爲什麼。那麼如何讓這個列在GridView內更新呢?

ASP.NET代碼:

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
      AutoGenerateColumns="False" DataKeyNames="Username" 
      DataSourceID="SqlDataSource1" BorderWidth="1px" BackColor="#DEBA84" 
      CellPadding="3" CellSpacing="2" BorderStyle="None" 
      BorderColor="#DEBA84" OnRowEditing="GridView1_RowEditing" 
      OnRowCancelingEdit="GridView1_RowCancelingEdit" 
      OnRowUpdating="GridView1_RowUpdating"> 
      <FooterStyle ForeColor="#8C4510" 
       BackColor="#F7DFB5"></FooterStyle> 
      <PagerStyle ForeColor="#8C4510" 
       HorizontalAlign="Center"></PagerStyle> 
      <HeaderStyle ForeColor="White" Font-Bold="True" 
       BackColor="#A55129"></HeaderStyle> 
      <Columns> 
       <asp:CommandField ButtonType="Image" ShowEditButton="true" ShowCancelButton="true" 
           EditImageUrl="Images/icons/edit24.png" UpdateImageUrl="Images/icons/update24.png" 
           CancelImageUrl="Images/icons/cancel324.png" /> 

       <asp:TemplateField HeaderText="Division"> 
        <ItemTemplate> 
         <%# Eval("DivisionShortcut")%> 
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:DropDownList ID="DivisionsList" runat="server" DataSourceID="DivisionsListDataSource" 
              AutoPostBack="true" DataTextField="DivisionShortcut" DataValueField="SapCode" 
              selectedValue='<%# Bind("DivisionShortcut") %>'> 
         </asp:DropDownList> 
        </EditItemTemplate> 
       </asp:TemplateField> 

       <asp:BoundField DataField="Username" HeaderText="Network ID" ReadOnly="True" 
        SortExpression="Username" /> 

       <asp:TemplateField HeaderText="Name"> 
        <ItemTemplate> 
         <%# Eval("Name")%> 
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:TextBox ID="txtEmployeeName" runat="server" Text='<%# Bind("Name")%>' /> 
        </EditItemTemplate> 
       </asp:TemplateField> 

       <asp:TemplateField HeaderText="Job Title"> 
        <ItemTemplate> 
         <%# Eval("JobTitle")%> 
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:TextBox ID="txtJobTitle" runat="server" Text='<%# Bind("JobTitle")%>' /> 
        </EditItemTemplate> 
       </asp:TemplateField> 

       <asp:TemplateField HeaderText="Badge No."> 
        <ItemTemplate> 
         <%# Eval("BadgeNo")%> 
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:TextBox ID="txtBadgeNo" runat="server" Text='<%# Bind("BadgeNo")%>' /> 
        </EditItemTemplate> 
       </asp:TemplateField> 

       <asp:TemplateField HeaderText="Is Active?"> 
        <ItemTemplate> 
         <%# Eval("IsActive")%> 
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:CheckBox ID="isActive" runat="server" 
             Checked='<%# Eval("IsActive").ToString().Equals("True") %>'/> 
        </EditItemTemplate> 
       </asp:TemplateField> 

       <asp:TemplateField HeaderText="Delete?"> 
        <ItemTemplate> 
         <span onclick="return confirm('Are you sure to Delete the record?')"> 
          <asp:ImageButton ID="lnkB" runat="server" ImageUrl="Images/icons/delete24.png" CommandName="Delete" /> 
         </span> 
        </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
     </asp:GridView> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="<%$ ConnectionStrings:UsersInfoDBConnectionString %>" 

      SelectCommand="SELECT  dbo.Divisions.DivisionShortcut,dbo.employee.DivisionCode dbo.employee.Username, dbo.employee.Name, dbo.employee.JobTitle, dbo.employee.BadgeNo, dbo.employee.IsActive 
       FROM   dbo.Divisions INNER JOIN 
          dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode" 
      UpdateCommand="UPDATE [employee] SET [Name] = @Name, [JobTitle] = @JobTitle, 
               [BadgeNo] = @BadgeNo WHERE [Username] = @Username" 
      DeleteCommand="DELETE FROM [employee] WHERE [Username] = @Username"> 
      <UpdateParameters> 
       <asp:Parameter Name="Name" Type="String" /> 
       <asp:Parameter Name="JobTitle" Type="String" /> 
       <asp:Parameter Name="BadgeNo" Type="String" /> 
       <asp:Parameter Name="Username" Type="String" /> 
      </UpdateParameters> 
      <DeleteParameters> 
       <asp:Parameter Name="Username" Type="String" /> 
      </DeleteParameters> 
     </asp:SqlDataSource> 

而GridView控件外,我爲DropDownList在SqlDataSource:

<asp:SqlDataSource ID="DivisionsListDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:UsersInfoDBConnectionString %>" 
    SelectCommand="SELECT * FROM Divisions" 
    InsertCommand="INSERT INTO [Divisions] ([SapCode], [DivisionShortcut]) VALUES (@SapCode, @DivisionShortcut)" 
    UpdateCommand="UPDATE [Divisions] SET [DivisionShortcut] = @DivisionShortcut WHERE [SapCode] = @SapCode"> 
    <DeleteParameters> 
     <asp:Parameter Name="SapCode" Type="Double" /> 
    </DeleteParameters> 
    <InsertParameters> 
     <asp:Parameter Name="SapCode" Type="Double" /> 
     <asp:Parameter Name="DivisionShortcut" Type="String" /> 
    </InsertParameters> 
    <UpdateParameters> 
     <asp:Parameter Name="DivisionShortcut" Type="String" /> 
     <asp:Parameter Name="SapCode" Type="Double" /> 
    </UpdateParameters> 
</asp:SqlDataSource> 

代碼隱藏:

//For editing any row in the GridView 
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 
    { 
     GridView1.EditIndex = e.NewEditIndex; 
    } 

    //For canceling any editng in any row in the GridView 
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 
    { 
     e.Cancel = true; 
     GridView1.EditIndex = -1; 
    } 

    //For updating the information in any row in the GridView 
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 
     GridViewRow gvrow = GridView1.Rows[e.RowIndex]; 

     DropDownList DivisionsList = (DropDownList)gvrow.FindControl("DivisionsList"); 

     TextBox txtEmployeeName = (TextBox)gvrow.FindControl("txtEmployeeName"); 
     TextBox txtJobTitle = (TextBox)gvrow.FindControl("txtJobTitle"); 
     TextBox txtBadgeNo = (TextBox)gvrow.FindControl("txtBadgeNo"); 

     CheckBox isActive = (CheckBox)gvrow.FindControl("isActive"); 

     //For getting the ID (primary key) of that row 
     string username = GridView1.DataKeys[e.RowIndex].Value.ToString(); 

     string name = txtEmployeeName.Text; 
     string jobTitle = txtJobTitle.Text; 
     string badgeNo = txtBadgeNo.Text; 
     string division = DivisionsList.Text; 
     //string divisioncode = DivisionsList.SelectedValue; 

     UpdateEmployeeInfo(username, name, jobTitle, badgeNo, division); 
    } 


    private void UpdateEmployeeInfo(string username, string name, string jobTitle, string badgeNo, string division) 
    { 
     string connString = ConfigurationManager.ConnectionStrings["UsersInfoDBConnectionString"].ConnectionString; 
     SqlConnection conn = new SqlConnection(connString); 
     string update = @"UPDATE Employee SET Name = @Name, JobTitle = @JobTitle, 
          BadgeNo = @BadgeNo WHERE Username = @Username"; 

     SqlCommand cmd = new SqlCommand(update, conn); 

     cmd.Parameters.AddWithValue("@Name", name); 
     cmd.Parameters.AddWithValue("@JobTitle", jobTitle); 
     cmd.Parameters.AddWithValue("@BadgeNo", badgeNo); 
     cmd.Parameters.AddWithValue("@division", division); 
     //cmd.Parameters.AddWithValue("@SapCode", divisioncode); 
     cmd.Parameters.AddWithValue("@Username", username); 
     //cmd.Parameters.AddWithValue("@IsActive", isActive.checked); 

     try 
     { 
      conn.Open(); 
      cmd.ExecuteNonQuery(); 
      conn.Close(); 

      GridView1.EditIndex = -1; 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      cmd.Dispose(); 
      conn.Close(); 
      conn.Dispose(); 
     } 
     GridView1.DataBind(); 
    } 

    //For deleting a row in the GridView 
    protected void DeleteRecord(object sender, GridViewDeleteEventArgs e) 
    { 
     string networkID = GridView1.DataKeys[e.RowIndex].Value.ToString(); 

     string connString = ConfigurationManager.ConnectionStrings["UsersInfoDBConnectionString"].ConnectionString; 
     SqlConnection conn = new SqlConnection(connString); 
     string deleteCommand = "DELETE employee where dbo.employee.Username = " + networkID; 
     SqlCommand cmd = new SqlCommand(deleteCommand, conn); 
     cmd.Parameters.AddWithValue("@username", networkID); 
     try 
     { 
      conn.Open(); 
      cmd.ExecuteNonQuery(); 
      conn.Close(); 
     } 
     catch (SqlException se) 
     { 
      throw se; 
     } 
     finally 
     { 
      cmd.Dispose(); 
      conn.Close(); 
      conn.Dispose(); 
     } 
     GridView1.DataBind(); 
    } 
+0

您的問題仍然沒有解決:( – 2012-07-14 07:50:00

回答

0

string division = DivisionsList.Text;這是錯誤的,你必須通過SapCode因爲SapCode是Divison表中的主鍵,它是Employees表中的主鍵,因此您必須更新Employee表中的SapCode。希望它能起作用。

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    string division = DivisionsList.Value; // Hope that it will work 
} 
+0

這是行不通的。 – 2012-07-14 08:54:25

+0

確定,但這個是在你的代碼錯誤。你在代碼隱藏獲得DivisionsList.Value?你檢查一下 – 2012-07-14 09:44:35

相關問題