使用onRowEditing / Updating的“编辑”按钮不起作用。

时间:2018-07-09 18:47:47

标签: asp.net

当我在textBox中编辑内容并按edit时,该文本不会得到更新,而是恢复为原始输入。我使用过editItemTemplate,但是这会使我的字段在按edit时消失,而使用itemTemplate会使字段保持打开状态,甚至可以在按下编辑按钮之前将其作为textBox编辑。这是它看起来的图像。 layout

请参见以下html页面代码-

<div class="GridviewDiv">
    <asp:GridView runat="server" ID="gvdeets" AllowPaging="true" PageSize="10" AutoGenerateColumns="false" Width="420px" onRowCancelingEdit="gvdeets_cancelEdit"
        OnRowUpdating="gvdeets_Update" OnRowEditing="gvdeets_RowEditing" OnPageIndexChanging="gvdeets_PageIndexChanging" Visible="true">
        <HeaderStyle CssClass="headerstyle"/>
        <Columns>
            <asp:TemplateField HeaderText="Select">
                <ItemTemplate>
                    <asp:CheckBox ID="cbSelect" CssClass="gridCB" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>



            <asp:BoundField DataField="Id" HeaderText="Id" />

                <asp:TemplateField HeaderText="Name" >

                     <ItemTemplate>

                          <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>

                     </ItemTemplate>
                    <EditItemTemplate>
                        <asp:Label ID="TextBox1" runat="server" Text='<%# Bind("Name") %>' Visible="false"></asp:Label>
                    </EditItemTemplate>


                </asp:TemplateField>

            <asp:TemplateField HeaderText="Password" >

                     <ItemTemplate>

                          <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Password") %>'></asp:TextBox>

                     </ItemTemplate>


                </asp:TemplateField>

            <asp:TemplateField HeaderText="City" >

                     <ItemTemplate>

                          <asp:TextBox ID="DropDownList1" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>

                     </ItemTemplate>


                </asp:TemplateField>

           <asp:BoundField DataField="Gender" HeaderText="Gender" />

            <asp:TemplateField HeaderText="Mail" >

                     <ItemTemplate>

                          <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Mail") %>'></asp:TextBox>

                     </ItemTemplate>


                </asp:TemplateField>
            <asp:TemplateField Headertext="Update" >
                <itemtemplate>

                

        </Columns>
    </asp:GridView>
</div>

这是后端-

            using System;
    using System.Configuration;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;


    namespace sms
    {
        internal class Method
        {
            private SqlCommand cmd;

            public Method(SqlCommand cmd)
            {
                this.cmd = cmd;
            }
        }

    public partial class deets : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGridview();
                }
                else
                {
                    if (hdn.Value == "ADD")
                    {
                        Button();
                        BindGridview();

                    }

                   else if (hdn.Value == "EDIT")
                    {
                        Button();
                        BindGridview();
                    }

                    else if (hdn.Value == "DELETE")
                    {
                        dlt();
                        BindGridview();
                    }


                    else if (hdn.Value == "save")
                    {
                        Button();
                        BindGridview();
                    }



                    hdn.Value = "";
                }
            }
protected void BindGridview()
        {
            DataSet ds = new DataSet();
            SqlConnection con = new SqlConnection("Server = localhost;Initial Catalog=Login;Integrated Security=True;");
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from TblLogin", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
                gvdeets.DataSource = ds;
                gvdeets.DataBind();
            }
        }
            protected void gvdeets_RowEditing(object sender,GridViewEditEventArgs
    e)
            {
                gvdeets.EditIndex = e.NewEditIndex;
                this.BindGridview();
            }
            protected void gvdeets_cancelEdit(object sender, 
    GridViewCancelEditEventArgs e)
            {
                gvdeets.EditIndex = -1;
                BindGridview();
            }
            public void gvdeets_Update(object sender, GridViewUpdateEventArgs e)
            {
                int Id = 
Convert.ToInt32(gvdeets.DataKeys[e.RowIndex].Value.ToString());
                string Name = (gvdeets.Rows[e.RowIndex].FindControl("txt1") as TextBox).ToString();
                string Password = (gvdeets.Rows[e.RowIndex].FindControl("TextBox2") as TextBox).ToString();
                string City = (gvdeets.Rows[e.RowIndex].FindControl("DropDownList1") as DropDownList).ToString();
                string Mail = (gvdeets.Rows[e.RowIndex].FindControl("TextBox4") as TextBox).ToString();
                string strConnString = 
        ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(strConnString))
                {
                    string query = "UPDATE TblLogin SET Name =@Name, 
        Password=@Password, City=@City, Mail=@Mail WHERE Id = @Id";
                    using (SqlCommand cmd = new SqlCommand(query))
                    {
                        cmd.Connection = con;
                        cmd.Parameters.AddWithValue("@Name", Name);
                        cmd.Parameters.AddWithValue("@Password", Password);
                        cmd.Parameters.AddWithValue("@City", City);
                        cmd.Parameters.AddWithValue("@Mail", Mail);
                        cmd.Parameters.AddWithValue("@Id", Id);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                        Response.Redirect(Request.Url.AbsoluteUri);
                    }
                }
            }
            protected void gvdeets_PageIndexChanging(object sender, 
        GridViewPageEventArgs e)
            {
                gvdeets.PageIndex = e.NewPageIndex;
                BindGridview();
            }
            } }

从本质上讲,我的编辑按钮无法正常工作。文本框是可编辑的,但是当我按下“编辑”按钮时不会更新。请在您认为我可以改善的地方帮助我。

感谢您的时间和帮助。

P.S-我只上传了我代码的相对部分,如果您需要全部内容,请告诉我。

0 个答案:

没有答案