我正在尝试使用网格视图来显示多个表的连接(Empdetail,Department,Country,State,City,Qualification)。主表(Empdetail)具有不同的ID(例如DeptID),其引用表中存在的主键ID。我希望部门(和其他字段)在更新gridview时显示为下拉选项,但是在填充下拉列表并使编辑按钮触发时我遇到问题。以下是代码。
.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Database.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Employee Database</title>
<script src="Themes/js/bootstrap.js" type="text/javascript"></script>
<script src="Themes/js/jquery.min.js" type="text/javascript"></script>
<script src="Themes/js/npm.js" type="text/jahttp://www.aspsnippets.com/Articles/Simple-Insert-Select-Edit-Update-and-Delete-in-ASPNet-GridView-control.aspxvascript"></script>
<script src="Themes/js/bootstrap.min.js" type="text/javascript"></script>
<script type = "text/javascript" src = "Scripts/jquery-1.3.2.min.js"></script>
<script type = "text/javascript" src = "Scripts/jquery.blockUI.js"></script>
<script type = "text/javascript">
//to create a loading effect
function BlockUI(elementID) {
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(function() {
$("#" + elementID).block({ message: '<table align = "center"><tr><td>' +
'<img src="Images/loadingAnim.gif"/></td></tr></table>',
css: {},
overlayCSS: {backgroundColor:'#000000',opacity: 0.6, border:'3px solid #63B2EB'
}
});
});
prm.add_endRequest(function() {
$("#" + elementID).unblock();
});
}
$(document).ready(function() {
BlockUI("dvGrid");
$.blockUI.defaults.css = {};
});
</script>
<link href="Themes/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css" />
<link href="Themes/css/bootstrap.css" rel="stylesheet" type="text/css" />
<link href="Themes/css/bootstrap-theme.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div id = "dvGrid">
<asp:ScriptManager runat="server" ID="sm">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" Width = "900px" AlternatingRowStyle-BackColor = "#b3e6ff"
AutoGenerateColumns="false" AllowPaging="true" OnRowDataBound="OnRowDataBound"
HeaderStyle-BackColor = "#0080ff" DataKeyNames="ID"
PageSize="10" class="table table-bordered" ShowFooter="true"
onrowediting="EditEmployee" onrowupdating="UpdateEmployee"
onrowcancelingedit="CancelEdit" OnRowDeleting="DeleteEmployee"
EmptyDataText="No records Selected.">
<Columns >
<asp:TemplateField >
<ItemTemplate>
<asp:Label ID="lblID" runat="server"
Text='<%# Eval("ID") %>' /><!--Primary key,no update and insert,auto generated-->
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "30px" HeaderText = "EmpCode">
<ItemTemplate>
<asp:Label ID="lblEmpCode" runat="server"
Text='<%# Eval("EmpCode")%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmpCode" runat="server"
Text='<%# Eval("EmpCode")%>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtEmpCode" class="form-control" Width = "60px"
MaxLength = "5" runat="server">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "30px" HeaderText = "Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server"
Text='<%# Eval("EmpName")%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server"
Text='<%# Eval("EmpName")%>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtName" class="form-control" Width = "60px"
MaxLength = "5" runat="server">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "30px" HeaderText = "DOJ">
<ItemTemplate>
<asp:Label ID="lblDOJ" runat="server"
Text='<%# Eval("DOJ")%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDOJ" runat="server"
Text='<%# Eval("DOJ")%>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtDOJ" class="form-control" Width = "100px"
MaxLength = "15" runat="server">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "120px" HeaderText = "Department">
<ItemTemplate>
<asp:Label ID="lblDepartment" runat="server"
Text='<%# Eval("DeptName")%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID = "Depddl" runat="server" Width="100px" >
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox class="form-control" ID="txtDepartment" Width = "140px"
MaxLength = "5" runat="server">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "30px" HeaderText = "Country">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server"
Text='<%# Eval("CoName")%>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCountry" class="form-control" Width = "60px"
MaxLength = "5" runat="server">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "30px" HeaderText = "State">
<ItemTemplate>
<asp:Label ID="lblState" runat="server"
Text='<%# Eval("SName")%>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtState" class="form-control" Width = "60px"
MaxLength = "5" runat="server">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "30px" HeaderText = "City">
<ItemTemplate>
<asp:Label ID="lblCity" runat="server"
Text='<%# Eval("CiName")%>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCity" class="form-control" Width = "60px"
MaxLength = "5" runat="server">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "30px" HeaderText = "Qualification">
<ItemTemplate>
<asp:Label ID="lblQualification" runat="server"
Text='<%# Eval("QName")%>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtQualification" class="form-control" Width = "80px"
MaxLength = "5" runat="server">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server"
CommandArgument = '<%# Eval("ID")%>' OnClick = "DeleteEmployee"
OnClientClick = "return confirm('Do you want to delete?')"
Text = "Delete" >
</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add"
class="btn-primary" type="button" OnClick = "AddNewEmployee"/>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID = "GridView1" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
代码隐藏(aspx.cs / C#)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
string constr = ConfigurationManager.ConnectionStrings["SQLDbConnection"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindData();
}
}
protected void EditEmployee(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
{
//Find the DropDownList in the Row
SqlConnection con = new SqlConnection(constr);
DropDownList Depddl = (DropDownList)e.Row.FindControl("Depddl");
string query = "SELECT ID,DeptName FROM Department;";
SqlCommand cmd = new SqlCommand(query);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
Depddl.DataSource = GetData(cmd);
Depddl.DataTextField = "DeptName";
Depddl.DataValueField = "ID";
Depddl.DataBind();
con.Close();
//Add Default Item in the DropDownList
Depddl.Items.Insert(0, new ListItem("Please select"));
//Select the Department ofEmployee in DropDownList
string Department = (e.Row.FindControl("lblDepartment") as Label).Text;
Depddl.Items.FindByValue(Department).Selected = true;
}
}
protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
protected void UpdateEmployee(object sender, GridViewUpdateEventArgs e)
{
string city = (GridView1.Rows[e.RowIndex].FindControl("Depddl") as DropDownList).SelectedItem.Value;
string ID = ((Label)GridView1.Rows[e.RowIndex]
.FindControl("lblID")).Text;
string EmpCode = ((TextBox)GridView1.Rows[e.RowIndex]
.FindControl("txtEmpCode")).Text;
string Name = ((TextBox)GridView1.Rows[e.RowIndex]
.FindControl("txtName")).Text;
string DOJ = ((TextBox)GridView1.Rows[e.RowIndex]
.FindControl("txtDOJ")).Text;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update Empdetail set EmpName=@EmpName," + "EmpCode=@EmpCode,"+ "DOJ=@DOJ where ID=@ID; " +
"select Empdetail.ID, Empdetail.EmpCode, Empdetail.EmpName,Empdetail.DOJ, "+
"Department.DeptName, Country.CoName, State.SName, City.CiName, Qualification.QName from "+
"Empdetail JOIN Department on Empdetail.DepartmentID=Department.ID JOIN Country on Empdetail.CountryID=Country.ID "+
"JOIN State on Empdetail.StateID=State.ID JOIN City on Empdetail.CityID=City.ID "+
"JOIN Qualification on Empdetail.QualificationID=Qualification.ID;";
cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID;
cmd.Parameters.Add("@EmpCode", SqlDbType.VarChar).Value = EmpCode;
cmd.Parameters.Add("@EmpName", SqlDbType.VarChar).Value = Name;
cmd.Parameters.Add("@DOJ", SqlDbType.VarChar).Value = DOJ;
GridView1.EditIndex = -1;
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
protected void DeleteEmployee(object sender, EventArgs e)
{
LinkButton lnkRemove = (LinkButton)sender;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "delete from Empdetail where " +
"ID=@ID;" +
"select Empdetail.ID, Empdetail.EmpCode, Empdetail.EmpName,Empdetail.DOJ, Department.DeptName, Country.CoName, State.SName, City.CiName, Qualification.QName from Empdetail JOIN Department on Empdetail.DepartmentID=Department.ID JOIN Country on Empdetail.CountryID=Country.ID JOIN State on Empdetail.StateID=State.ID JOIN City on Empdetail.CityID=City.ID JOIN Qualification on Empdetail.QualificationID=Qualification.ID;";
cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value
= lnkRemove.CommandArgument;
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
private void BindData()
{
string query = "select Empdetail.ID, Empdetail.EmpCode, Empdetail.EmpName,Empdetail.DOJ,"+
"Department.ID, Department.DeptName, Country.CoName, State.SName, City.CiName, Qualification.QName from "+
"Empdetail JOIN Department on Empdetail.DepartmentID=Department.ID JOIN Country on Empdetail.CountryID=Country.ID"+
" JOIN State on Empdetail.StateID=State.ID JOIN City on Empdetail.CityID=City.ID"+
" JOIN Qualification on Empdetail.QualificationID=Qualification.ID;";
SqlCommand cmd = new SqlCommand(query);
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(constr);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
protected void AddNewEmployee(object sender, EventArgs e)
{
string ID = ((TextBox)GridView1.FooterRow.FindControl("txtID")).Text;
string EmpCode = ((TextBox)GridView1.FooterRow.FindControl("txtEmpCode")).Text;
string Company = ((TextBox)GridView1.FooterRow.FindControl("txtCompany")).Text;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
/*cmd.CommandText = "insert into Empdetail(EmpCode, CompanyName) " +
"values(@CustomerID, @ContactName, @CompanyName);" +
"select CustomerID,ContactName,CompanyName from customers";
cmd.Parameters.Add("@CustomerID", SqlDbType.VarChar).Value = CustomerID;
cmd.Parameters.Add("@ContactName", SqlDbType.VarChar).Value = Name;
cmd.Parameters.Add("@CompanyName", SqlDbType.VarChar).Value = Company;*/
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
}
注意: - 我已经在多个网站上进行了广泛搜索,所以如果您能告诉我确切的问题而不是将我导航到某个链接,我会非常感激。我是Gridview概念的新手,所以请以开放的心态批评代码。提前致谢
答案 0 :(得分:0)
在aspx文件中,我删除了每个下拉列表的数据源,例如SqlDataSource。在标记中,您可以完全定义每个DDL,每行使用相同的数据源单个实例。在编辑时,您只需要捕获所选的值进行更新。
所以...部门示例的数据源......
. . .
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SQLDbConnection %>"
SelectCommand="SELECT ID,DeptName FROM Department"
EnableCaching="True">
</asp:SqlDataSource>
然后在部门的GridView模板中......
<EditItemTemplate>
<asp:DropDownList ID = "Depddl" runat="server" Width="100px" >
DataSourceID="SqlDataSource1"
DataTextField="DeptName"
DataValueField="ID"
</asp:DropDownList>
</EditItemTemplate>
在后面的代码中,你可以选择从当前值中选择DDL的当前值......或者不是......由你决定。
protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e )
{
if ( e.Row.RowType == DataControlRowType.DataRow )
{
if ( e.Row.RowState.HasFlag( DataControlRowState.Edit ) )
{
DataRowView drv = (DataRowView) e.Row.DataItem;
DropDownList ddl = (DropDownList) e.Row.FindControl( "DropDownList1" );
ddl.SelectedValue = ((int) drv[ "ID" ]).ToString();
}
}
}
答案 1 :(得分:0)
在对代码的几个部分进行评论后,我意识到问题出在&#34; OnRowDataBound&#34;功能。 注释这些行会使更新按钮再次触发: -
string Department = (e.Row.FindControl("lblDepartment") as Label).Text;
Depddl.Items.FindByValue(Department).Selected = true;
&#13;
我可以从中收集到的是我无法以这种方式访问Item模板控件。评论这些线条使页面运行顺畅。然而,这是以失去自动选择原始数据值的功能为代价的。我想实现这一点,并且一旦我能实现这一点就会发布更新。