这是我第一次提出问题所以请耐心等待。我有GridView
templatefields
而datasource
是Business Logic Layer
方法,可以通过Data Access Layer
检索员工列表,该列表可用作LinqToSql。
即使如此,当我尝试使用任何Northwind
表的相同方法时,gridview始终为空,例如它工作正常,并且在Northwind
网格上设置的相同属性在Employee网格上设置
值得一提的是,当我将AutoGenerateColumns
设置为true
时,它可以正常工作,但我想在编辑时进行第一次column(ID)
读取。
编辑: 我尝试混合数据绑定表达式:#Eval(),#bind()但没有任何工作,另一件事是当我按下编辑按钮时,数据正确显示在EditTemplate中,这很奇怪,为什么显示不起作用只是查看数据!!! 这是我的标记:
<asp:GridView ID="gvEmployees" runat="server" ShowFooter="true" AutoGenerateColumns="false"
OnRowEditing="gvEmployees_RowEditing" OnRowUpdating="gvEmployees_RowUpdating"
OnRowCancelingEdit="gvEmployees_RowCancelingEdit">
<Columns>
<asp:TemplateField HeaderText="EmployeeId">
<asp:ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("EmployeeId")%>'></asp:Label>
</asp:ItemTemplate>
<asp:FooterTemplate>
<asp:TextBox ID="txtEmployeeId" runat="server" ReadOnly="true" />
</asp:FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<asp:ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("FirstName")%>'></asp:Label>
</asp:ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Eval("FirstName")%>'></asp:TextBox>
</EditItemTemplate>
<asp:FooterTemplate>
<asp:TextBox ID="txtFirstName" runat="server" />
</asp:FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<asp:ItemTemplate>
<asp:Label ID="lblLasttName" runat="server" Text='<%# Eval("LastName")%>'></asp:Label>
</asp:ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%# Eval("LastName")%>'></asp:TextBox>
</EditItemTemplate>
<asp:FooterTemplate>
<asp:TextBox ID="txtLastName" runat="server" />
</asp:FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<asp:ItemTemplate>
<asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email")%>'></asp:Label>
</asp:ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmail" runat="server" Text='<%# Eval("Email")%>'></asp:TextBox>
</EditItemTemplate>
<asp:FooterTemplate>
<asp:TextBox ID="txtEmail" runat="server" />
</asp:FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%# Eval("EmployeeId")%>'
OnClientClick="return confirm('Do you want to delete?')"
Text="Delete" OnClick="DeleteEmployee"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="AddNewEmployee" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
</Columns>
</asp:GridView>
这是背后的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindData();
}
private void BindData()
{
String conString = ConfigurationManager.ConnectionStrings["SampleDBContext"].ConnectionString;
String path = "/Logs/WebSample.log";
path = HttpContext.Current.Server.MapPath(path);
BllService service = new BllService(conString, path);
gvEmployees.DataSource = service.GetEmployess().AsEnumerable();
gvEmployees.DataBind();
}
protected void gvEmployees_RowEditing(object sender, GridViewEditEventArgs e)
{
gvEmployees.EditIndex = e.NewEditIndex;
BindData();
}
protected void AddNewEmployee(object sender, EventArgs e)
{
}
protected void gvEmployees_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvEmployees.EditIndex = -1;
BindData();
}
protected void gvEmployees_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
protected void DeleteEmployee(object sender, EventArgs e)
{
LinkButton lnkRemove = (LinkButton)sender;
//lnkRemove.CommandArgument;
}
BLLService是名称表示业务逻辑层DLL,允许检索employeesess(LinqToSql) 我附上了截屏1
提前致谢。 米歇尔