我有两个实体类-用户和任务。每个用户都具有以下属性: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”,而不是这样:AsssignedTo-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,知道它对应的userId并更新我的Task。我哪里出问题了?
抱歉,对于冗长的帖子,我试图尽可能地做到透彻,并且由于我不明白问题出在哪里,所以也许我甚至错过了一些重要的事情(这是我的第一篇文章)。请帮忙,因为我从一开始就一直坚持这一点。
对于第一个问题,您可以使用一种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来进行尝试,所以这只是一个猜测。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句