Asp.net WebForms DropDownList - 具有不同值和显示文本的控件 (DataValueField/DataTextField)

Asp.net WebForms DropDownList - controls with different values and displayed texts (DataValueField/DataTextField)

我有两个实体 类 - 用户和任务。每个用户都具有以下属性: UserId(整数),用户名(字符串),密码(字符串),名字(字符串),姓氏(字符串)

和每个任务,这些: TaskId(整数),标题(字符串),描述(字符串), EstimatedTime(int), CreatedOn(Datetime), CreatedBy(int), AssignedTo(int), 完成(bool)

所以CreatedByAssignedTo实际上是UserIds——CreatedBy是创建任务的用户的Id,AssignedTo——任务的ID。我有一个使用它们的数据库 - 那里没有问题。

在我的 WebForms 应用程序中,我使用 GridView 来显示任务。所以我隐藏了 TaskId,所有其他属性都显示为 GridView 中的列。但是,问题是,我不希望我的用户将 CreatedBy 和 AssignedTo 中的值视为 ids(integers) - 我希望他们将值视为用户名,因此它看起来像这样: 分配给 - "myUsername", 而不是这样: 分配给 - 321.

这是我的第一个问题,我不知道该怎么做。正如您将在我的代码中看到的,ItemTemplate 是一个标签,绑定到 属性 AssignedTo。如何让值不可见并改为显示用户名?

其次,当我以管理员身份登录时,我希望能够编辑任务。所以我将这些命令字段添加到我的 GridView:

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

我有一个 TaskRepository ans UserRepository 用于访问我的数据库。例如,GridView 绑定到我的 TaskRepository 中的 属性,即 returns 所有任务的列表:

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>

因此,当我编辑任务时,我单击 "Edit",然后我可以更改每列中 selected 行中的值。但问题又出在 AssignedTo 上。正如您从代码中看到的,当我编辑它时,我看到一个 DropDownList,其中包含 ID,我必须从用户名中进行选择。但是当我尝试保存我的更改时,我得到一个 object 空引用异常。我不知道为什么。这是我的代码:

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 时,我得到了异常。这是我想要发生的事情:当我单击“编辑”时,我想在“分配给”列中看到一个 DropDownList,其中包含可供选择的所有用户用户名(到目前为止,还不错),当我 select 一个用户并单击更新,我希望它获得 assignedTo 的 selected 用户名值,知道它对应的 userId 并更新我的任务。我哪里出错了?

抱歉这么久了 post,我尽量说得更透彻了,因为我不明白问题出在哪里,也许我什至错过了一些重要的东西(这是我的第一个 post).请帮忙,因为我一直坚持这个。

对于第一个问题,您可以使用 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来试试,所以这只是一个猜测。