我有一个名为Employees的数据库表,其中包含每个员工的ID号,名字,姓氏和SupervisorID。为员工存储的SupervisorID等于该员工主管的ID号。
我想要一个可以为员工编辑这些字段的DetailsView控件。 Supervisor字段需要是一个下拉列表,将SupervisorID存储为值,并显示主管的名字和姓氏作为文本。
我已经环顾了很多,并且能够弄清楚如何创建下拉列表以选择并显示SupervisorID。我遇到的问题是如何更改下面的代码来存储SupervisorID,但是将主管的名字显示为文本?
以下是我的DetailsList中的Supervisor下拉列表的代码:
<asp:TemplateField HeaderText="Supervisor" SortExpression="SupervisorID">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource3" DataTextField="SupervisorID" DataValueField="SupervisorID" SelectedValue='<%# Bind("SupervisorID") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnectionString %>" SelectCommand="SELECT [SupervisorID] FROM [Employees]"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate >
<asp:Label Runat="server" Text='<%# Bind("SupervisorID") %>' ID="Label1"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
对于我可以改变以实现目标的任何想法?谢谢你的帮助!
答案 0 :(得分:0)
这里有一些代码供您检查。您需要修改检索员工列表的位置。我编写静态数据。任何你不清楚的事情,请告诉我。
----------------------- aspx code ------
<asp:GridView ID="grvEmp" runat="server" AutoGenerateColumns="false"
onrowdatabound="grvEmp_RowDataBound">
<Columns>
<asp:BoundField HeaderText="Employee Id" DataField="ID" />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="dlList" runat="server" AppendDataBoundItems="true">
<asp:ListItem Value="" Text=" -- select supervisor --"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnUpdate" runat="server" Text="Update"
onclick="btnUpdate_Click" />
-----------------代码背后-------------------------- //将此方法放在数据绑定到gridview
的位置 BindGrid();
private void BindGrid()
{
//emp list
//change here.. load employee list from your database
dtEmp = new DataTable();
dtEmp.Columns.Add("ID");
dtEmp.Columns.Add("Name");
dtEmp.Columns.Add("SupervisorId");
DataRow dr = dtEmp.NewRow();
dr[0] = "1";
dr[1] = "John";
dr = dtEmp.NewRow();
dr[0] = "2";
dr[1] = "Mary";
dtEmp.Rows.Add(dr);
dr = dtEmp.NewRow();
dr[0] = "3";
dr[1] = "Edward";
dtEmp.Rows.Add(dr);
dr = dtEmp.NewRow();
dr[0] = "4";
dr[1] = "Phippip";
dtEmp.Rows.Add(dr);
dr = dtEmp.NewRow();
dr[0] = "5";
dr[1] = "James";
dtEmp.Rows.Add(dr);
dr = dtEmp.NewRow();
dr[0] = "6";
dr[1] = "Jason";
dtEmp.Rows.Add(dr);
grvEmp.DataSource = dtEmp;
grvEmp.DataBind();
}
---------在gridview行数据绑定事件..对于主管下拉列表
protected void grvEmp_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList dl = (DropDownList)e.Row.FindControl("dlList");
if (dl != null)
{
dl.DataSource = dtEmp;
dl.DataTextField = "Name";
dl.DataValueField = "ID";
dl.DataBind();
//remove own id
dl.Items.Remove(dl.Items.FindByValue(e.Row.Cells[0].Text));
}
}
}
-----------------关于更新按钮点击事件以保存每位员工的主管
protected void btnUpdate_Click(object sender, EventArgs e)
{
// get emp id and thier supervisor id
for (int i = 0; i < grvEmp.Rows.Count; i++)
{
GridViewRow row = grvEmp.Rows[i];
//get emp id
string empId = row.Cells[0].Text;
//get supervisor id
DropDownList dl = (DropDownList)row.FindControl("dlList");
string supervisorId = (dl != null) ? dl.SelectedValue : "";
if (supervisorId != "")
{
//update supervisor id of employee here to DB
//you can keep emp n supervisor id pair.. and save all only one time after looping of gridivew
}
}
}