2015-02-07 44 views
2

我有兩個實體類 - 用戶和任務。每個用戶具有以下屬性: 用戶ID(INT),用戶名(串),密碼(串),名字(串),姓氏(串)Asp.net WebForms DropDownList - 具有不同值和顯示文本的控件(DataValueField/DataTextField)

和每一個任務,這些: 的TaskID(INT),標題(字符串),說明(字符串),EstimatedTime(INT),CreatedOn(日期時間),CreatedBy(INT),AssignedTo(INT),成品(布爾)

所以CreatedByAssignedTo實際上是用戶ID - CreatedBy是用戶的ID,誰創建任務,AssignedTo - 任務所針對的ID。我有一個數據庫,我使用它們 - 沒有問題。

在我的WebForms應用程序中,我使用GridView來顯示任務。所以我隱藏了TaskId,並且所有其他屬性都顯示爲GridView中的列。但是,問題是,我不希望我的用戶將CreatedBy和AssignedTo中的值看作id(整數) - 我希望他們將值看作Usernames,因此它看起來像這樣: AssignedTo - 「myUsername」 , 而不是這樣: AssignedTo - 321。

這是我的第一個問題,我不知道該怎麼做。正如你將在我的代碼中看到的那樣,ItemTemplate是一個Label,綁定到屬性AssignedTo。我如何保持該值不可見並顯示用戶名?

其次,當我以管理員身份登錄時,我希望能夠編輯任務。所以我將這些命令字段添加到我的GridView中:

<asp:CommandField ShowEditButton="true" /> 
<asp:CommandField ShowDeleteButton="true" /> 

我有一個TaskRepository和UserRepository,用於訪問我的數據庫。例如,GridView控件綁定到我的TaskRepository返回的所有任務列表的屬性:

TaskRepository taskRepo = new TaskRepository; 
GridViewTasks.DataSource = taskRepo.GetAll(); 
GridViewTasks.DataBind(); 

一切完美的作品。 這裏是我的AssignedTo的ItemTemplate:

<asp:TemplateField HeaderText="Assigned to"> 
    <EditItemTemplate> 
        <asp:DropDownList ID="editAssignedTo" runat="server" DataTextField="Username" DataValueField="UserId"></asp:DropDownList> 
    </EditItemTemplate> 
    <ItemTemplate> 
        <asp:Label ID="lbAssignedTo" runat="server" Text='<%#Bind("AssignedTo")%>'></asp:Label> 
    </ItemTemplate> 
</asp:TemplateField> 

所以,當我編輯的任務,我點擊「編輯」,我可以在每列選定行中更改數值。但問題同樣在AssignedTo中。正如你從代碼中看到的那樣,當我編輯它的時候,我看到了一個DropDownList,裏面放入了Ids,我必須從用戶名中選擇。但是當我嘗試保存更改時,我得到一個對象空引用異常..我不知道爲什麼。這裏是我的代碼:

protected void GridViewTasks_RowUpdating(object sender, GridViewUpdateEventArgs e) 
     { 
      GridViewRow selectedRow = GridViewTasks.Rows[e.RowIndex]; 
      Tasks task = new Tasks(); 
      task.TaskId = (int)GridViewTasks.DataKeys[e.RowIndex].Value; 
      TextBox title = (TextBox)selectedRow.FindControl("tbTitle"); 
      task.Title = title.Text; 
      TextBox description = (TextBox)selectedRow.FindControl("tbDescription"); 
      task.Description = description.Text; 
      Label createdOn = (Label)selectedRow.FindControl("lblCreatedOn"); 
      task.CreatedOn = DateTime.Parse(createdOn.Text); 
      Label createdBy = (Label)selectedRow.FindControl("lblCreatedBy"); 
      task.CreatedBy = int.Parse(createdBy.Text); 
      TextBox estimTime = (TextBox)selectedRow.FindControl("tbEstimTime"); 
      task.EstimatedTime = int.Parse(estimTime.Text);   
      DropDownList assignedTo = (DropDownList)selectedRow.FindControl("editAssignedTo");   
      task.AssignedTo = int.Parse(assignedTo.SelectedItem.Value); 
      Label lblFinished = (Label)selectedRow.FindControl("lblFinished"); 
      task.Finished = bool.Parse(lblFinished.Text); 
      taskRepo.Save(task); 
      BindDataToGridView(); 
     } 

一切工作與其他控件,但是當它到達task.AssignedTo,我得到的異常。這裏是我想要發生的事情:當我點擊編輯時,我想在AssignedTo列中看到一個DropDownList列表,其中包含我的所有用戶用戶名(可以選擇),當我選擇一個用戶並單擊更新時,我想要它獲得assignedTo的選定用戶名值,知道它對應的userId並更新我的任務。我哪裏出錯了?

對不起,長的帖子,我想盡可能徹底,因爲我不明白問題出在哪裏,也許我錯過了一些重要的事情(這是我的第一篇文章)。請幫助,因爲我一直堅持這一點,因爲永遠。

回答

0

對於你的第一個問題,你可以使用OnRowDataBound方法您的ID轉換爲用戶名。基本上,你從行中獲取ID並根據ID獲取名稱。

例如:

protected void Gridview_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     //get your control like in rowupdating 
     //take the ID value and query against your user info to get the name 
     // set the value 
    } 
} 

你的第二個問題,它可能是你需要OnRowUpdated代替。我沒有在這臺電腦上安裝VS來試用它,所以這只是一個猜測。

相關問題