2015-02-07 41 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来试用它,所以这只是一个猜测。

相关问题