我正在尝试制作一个简单的Gridview应用程序,我在互联网上找到了一个例子并进行了一些更改。我有一个问题,我对一件事感到困惑。问题是我无法在编辑模式下向gridview添加数据。我在Gridview RowCommand Property中设置了一个断点,发现所有文本框值都是空的。这可能是一个愚蠢的错误,但我不知道它在哪里。还有一件事,在添加数据时我们使用此代码:
(e.CommandName.Equals("ADD"))
我不明白,但代码中没有(e.CommandName.Equals("Update"))
或类似内容。在这种情况下如何触发更新按钮?
提前致谢。
这是我的代码:
<asp:GridView ID="gvEmployeeDetails" runat="server" Width="600px"
AutoGenerateColumns="false" ShowFooter="true"
onrowcommand="gvEmployeeDetails_RowCommand"
onrowdeleting="gvEmployeeDetails_RowDeleting"
onrowupdating="gvEmployeeDetails_RowUpdating"
onrowcancelingedit="gvEmployeeDetails_RowCancelingEdit"
onrowediting="gvEmployeeDetails_RowEditing"
HeaderStyle-BackColor="#4D4D4D"
HeaderStyle-ForeColor="White">
<Columns>
<asp:TemplateField HeaderText="Employee ID">
<ItemTemplate>
<asp:Label ID="lblEmpID"
runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "empid") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblEditEmpID"
runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "empid") %>'>
</asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddEmpID" runat="server" Width="100px">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "name") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditName" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "name") %>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddName" runat="server" Width="100px">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<ItemTemplate>
<asp:Label ID="lblDesignation" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "designation") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditDesignation" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "designation") %>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddDesignation" runat="server" Width="150px">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblCity" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "city") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditCity" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "city") %>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddCity" runat="server" Width="80px">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "country") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditCountry" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "country") %>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddCountry" runat="server" Width="80px">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" runat="server" CommandName="Edit"
ImageUrl="~/Images/icon-edit.png" Height="32px" Width="32px"/>
<asp:ImageButton ID="imgbtnDelete" runat="server" CommandName="Delete"
ImageUrl="~/Images/Delete.png"/>
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="imgbtnUpdate" runat="server" CommandName="Update"
ImageUrl="~/Images/icon-update.png"/>
<asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel"
ImageUrl="~/Images/icon-Cancel.png"/>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lbtnAdd" runat="server" CommandName="ADD"
Text="Add" Width="80px">
</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是aspx.cs文件:
SqlConnection conn = new SqlConnection("Data Source=localhost\\sqlexpress;Database=Northwind;User Id=sa ;Password=1234");
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
void BindData() {
DataSet ds = new DataSet();
DataTable FromTable = new DataTable();
string cmd = "select * from EmployeeDetails";
SqlDataAdapter adp = new SqlDataAdapter(cmd, conn);
adp.Fill(ds);
FromTable = ds.Tables[0];
if (FromTable.Rows.Count > 0)
{
gvEmployeeDetails.DataSource = FromTable;
gvEmployeeDetails.DataBind();
}
else {
FromTable.Rows.Add(FromTable.NewRow());
gvEmployeeDetails.DataSource = FromTable;
gvEmployeeDetails.DataBind();
int TotalColumns = gvEmployeeDetails.Rows[0].Cells.Count;
gvEmployeeDetails.Rows[0].Cells.Clear();
gvEmployeeDetails.Rows[0].Cells.Add(new TableCell());
gvEmployeeDetails.Rows[0].Cells[0].ColumnSpan = TotalColumns;
gvEmployeeDetails.Rows[0].Cells[0].Text = "No records Found";
}
}
protected void gvEmployeeDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
///不要发布这部分它的作品
}
protected void gvEmployeeDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
gvEmployeeDetails.EditIndex = e.NewEditIndex;
BindData();
}
protected void gvEmployeeDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void gvEmployeeDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("ADD"))
{
TextBox txtAddEmpID = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddEmpID");
TextBox txtAddName = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddName");
TextBox txtAddDesignation = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddDesignation");
TextBox txtAddCity = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddCity");
TextBox txtAddCountry = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddCountry");
conn.Open();
string cmdstr = "insert EmployeeDetails values(@empid,@name,@designation,@city,@country)";
SqlCommand cmd = new SqlCommand(cmdstr, conn);
cmd.Parameters.AddWithValue("@empid", txtAddEmpID.Text);
cmd.Parameters.AddWithValue("@name", txtAddName.Text);
cmd.Parameters.AddWithValue("@designation", txtAddDesignation.Text);
cmd.Parameters.AddWithValue("@city", txtAddCity.Text);
cmd.Parameters.AddWithValue("@country", txtAddCountry.Text);
cmd.ExecuteNonQuery();
conn.Close();
BindData();
}
}
protected void gvEmployeeDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvEmployeeDetails.EditIndex = -1;
}
答案 0 :(得分:1)
您正在页面加载上绑定gridview,因此每次页面加载文本和其他控件都会被清除。 只有在没有ispostback的情况下才能绑定数据并检查吗?
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}
关于你的问题, 您有一个带有命令名称编辑的链接按钮。这是网格视图用于触发所选行的编辑模式的命令的名称。你可以在这里添加任何类型的命令控件,只要命令名称为“编辑”,编辑模式就会被触发。如果你没有添加这个控件,并设置AutoGenerateEditButton = true,那么默认情况下gridview将生成并编辑按钮,并使用“Edit”作为命令名称。