C# 无法将数据添加到Gridview
我正在尝试制作一个简单的Gridview应用程序,我在internet上找到了一个示例,并做了一些更改。我有一个问题,对一件事感到困惑。问题是我无法在编辑模式下向gridview添加数据。我在Gridview RowCommand属性上设置了一个断点,并看到所有的文本框值都是空的。这可能是个愚蠢的错误,但我不知道它在哪里。还有一件事,在添加数据时,我们使用以下代码:C# 无法将数据添加到Gridview,c#,asp.net,C#,Asp.net,我正在尝试制作一个简单的Gridview应用程序,我在internet上找到了一个示例,并做了一些更改。我有一个问题,对一件事感到困惑。问题是我无法在编辑模式下向gridview添加数据。我在Gridview RowCommand属性上设置了一个断点,并看到所有的文本框值都是空的。这可能是个愚蠢的错误,但我不知道它在哪里。还有一件事,在添加数据时,我们使用以下代码: (e.CommandName.Equals("ADD")) 我不明白,但代码中没有(e.CommandName.Equals(“
(e.CommandName.Equals("ADD"))
我不明白,但代码中没有(e.CommandName.Equals(“更新”)
或类似的内容。在这种情况下,更新按钮是如何触发的
提前谢谢
这是我的代码:
<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>
///不要发布这个部分,它可以工作
}
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;
}
您正在页面加载上绑定gridview,因此每次页面加载时,文本和其他控件都会被清除。 只有在未返回并检查的情况下才能绑定数据吗
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}
关于你的问题,,
您有一个命令名为“编辑”的链接按钮。这是“栅格视图”用于触发选定行的编辑模式的命令的名称。Youn可以在此处添加任何类型的命令控件,只要命令名为“编辑”,编辑模式将被触发。如果不添加此控件,并将AutoGenerateEditButton设置为true,则默认情况下gridview将生成并编辑按钮,命令名为“edit” @Thakn you Kiran,对不起,我的英语不好,但是你明白另一个问题吗?我想,我明白你的问题,更新了上面的答案。
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}