我需要从数据库中检索记录并在网格视图中显示它。然后我需要编辑该特定记录。但是,当我尝试这样做时,会发生以下错误。
" GridView' GridView1'解雇了没有处理的事件RowEditing。"
非常感谢任何帮助。我尝试的代码如下。
ASP页面
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EnableModelValidation="True" >
<Columns>
<asp:BoundField DataField="empid" HeaderText="id" />
<asp:TemplateField HeaderText="name">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="age">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("age") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="gender">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("gender") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("gender") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="mobile">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Eval("mobile") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("mobile") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="qual">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Eval("qual") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("qual") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" ShowCancelButton="true" />
</Columns>
</asp:GridView>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testdb%>" UpdateCommand="update [empdetails] SET [name]=@name,[age]=@age,[gender]=@gender,[mobile]=@mobile,[qual]=@qual WHERE [empid]=@id">
<UpdateParameters>
<asp:Parameter Name="name" type="String" />
<asp:Parameter Name="age" Type="Int32" />
<asp:Parameter Name="gender" Type="String" />
<asp:Parameter Name="mobile" Type="Int32" />
<asp:Parameter Name="qual" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
代码背后:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("select * from empdetails where empid ='" + TextBox1.Text + "'", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
con.Close();
}
答案 0 :(得分:1)
通过显示编辑按钮:
<asp:CommandField ShowEditButton="true" ShowCancelButton="true" />
它希望它在RowEditing事件中处理。您需要添加此处理程序以设置正在编辑的行的索引。
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
EnableModelValidation="True"
OnRowEditing="GridView1_RowEditing">
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
GridView1.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindData();
}
答案 1 :(得分:1)
您应该了解ADO.NET参数以及如何准备它们:
var data = new DataTable();
using (var conn = new SqlConnection(constr))
{
try
{
conn.Open();
var cmd = new SqlCommand("dbo.pr_EC_SportsGroups_Search_Students", conn);
cmd.Text = "select * from empdetails where empid = @empId";
var empId = TextBox1.Text;
cmd.Parameters.AddWithValue("@empId", empId);
//create sql adapter by passing command object
var adapter = new SqlDataAdapter(cmd);
//fill the data table
adapter.Fill(data);
//bind data
GridView1.DataSource = data;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
至于错误本身,您只需将事件添加到标记中,有关详细信息,请参阅@j.f答案。