[英]Asp.net WebForms DropDownList - controls with different values and displayed texts (DataValueField/DataTextField)
我有兩個實體類-用戶和任務。 每個用戶都具有以下屬性: UserId (int), Username (字符串), Password (字符串), FirstName (字符串), Lastname (字符串)
和每一個任務,這些: 任務id(INT), 標題 (串), 說明 (串),EstimatedTime(INT),CreatedOn(日期時間),CreatedBy(INT),AssignedTo(INT), 完成 (布爾)
因此, CreatedBy和AssignedTo實際上是UserIds-CreatedBy是創建任務的用戶的ID,而AssignedTo是任務所在的用戶的ID。 我有一個使用它們的數據庫-那里沒有問題。
在我的WebForms應用程序中,我使用GridView來顯示任務。 因此,我隱藏了TaskId,所有其他屬性在GridView中顯示為列。 但是,問題是,我不希望我的用戶以id(integers)的形式看到CreatedBy和AssignedTo中的值-我希望他們將這些值視為用戶名,所以看起來像這樣: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。 從代碼中可以看到,當我對其進行編輯時,我看到一個放置了ID的DropDownList,我必須從用戶名中進行選擇。 但是,當我嘗試保存所做的更改時,我得到一個對象null引用異常。。我不知道為什么。 這是我的代碼:
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,知道它對應的用戶標識並更新我的任務。 我哪里出錯了?
很抱歉,對於冗長的帖子,我試圖盡可能地做到透徹,並且由於我不明白問題出在哪里,也許我甚至錯過了一些重要的事情(這是我的第一篇文章)。 請幫忙,因為我從一開始就堅持這樣做。
對於第一個問題,您可以使用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來進行嘗試,所以這只是一個猜測。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.