为什么Gridview的更新控件行为异常?

时间:2019-04-05 05:49:58

标签: c# sql asp.net model-view-controller

这是我的代码,用于更新gridview行。但是它所做的事情表明这种行为,我想这不是通常的行为。请指出我做错了什么。

This is an image showcasing what is happening.

代码:

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
    bindgrid();
  }
}

private void bindgrid()
{
  con.Open();

  MySqlCommand cmd = con.CreateCommand();
  cmd.CommandType = CommandType.Text;
  cmd.CommandText = "select * from vendor";

  MySqlDataReader dr = cmd.ExecuteReader();

  DataTable dt = new DataTable();
  dt.Load(dr);

  GridView1.DataSource = dt;
  GridView1.DataBind();

  con.Close();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
  GridView1.EditIndex = e.NewEditIndex;
  bindgrid();
}

这是ASPX代码:

<asp:TemplateField>
  <ItemTemplate>
    <asp:Button Text="Edit Element" CssClass="btn btn-success fa fa-edit" ID="Editbutton" runat="server" CommandName="Edit" />
  </ItemTemplate>
  <EditItemTemplate>
    <asp:Button Text="Update" CssClass="btn btn-success fa fa-edit" ID="updatebutton" runat="server" CommandName="Update" />
    <asp:Button Text="Cancel" CssClass="btn btn-warning fa fa-close" ID="cancelbutton" runat="server" CommandName="Cancel" />
  </EditItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Vendor ID">
  <ItemTemplate>
    <asp:Label ID="idlbl" runat="server" Text='<%# Eval("v_id")%>'></asp:Label>
  </ItemTemplate>
  <EditItemTemplate>
    <asp:TextBox ID="idtext" runat="server" Text='<%# Eval("v_id")%>'></asp:TextBox>
  </EditItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Vendor Name">
  <ItemTemplate>
    <asp:Label ID="namelbl" runat="server" Text='<%# Eval("v_name")%>'></asp:Label>
  </ItemTemplate>
  <EditItemTemplate>
    <asp:TextBox ID="nametext" runat="server" Text='<%# Eval("v_name")%>'></asp:TextBox>
  </EditItemTemplate>
</asp:TemplateField>

0 个答案:

没有答案