【问题标题】:how to delete a row on gridview with a JavaScript button and remain on the same page如何使用 JavaScript 按钮删除 gridview 上的一行并保持在同一页面上
【发布时间】:2013-06-03 08:35:51
【问题描述】:

我正在通过gridview显示一组记录,以及editdelete 旁边的按钮。我在记录删除部分遇到问题。我想要的行为如下:用户单击按钮,调用 JavaScript 验证函数,单击按钮后记录被删除,但用户与其余记录保持在同一页面上。如何在保持在同一页面上的同时执行此操作?

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div>

        &nbsp;&nbsp;&nbsp;
        <br />
&nbsp;

        <asp:HyperLink NavigateUrl="~/Entry.aspx" runat="server" text="Add New Record" />
    </div>

    <div>
        <asp:GridView runat="server" ID="grdView" AutoGenerateColumns="false" Height="100%"
            Width="100%" onselectedindexchanged="grdView_SelectedIndexChanged" >
            <Columns>

                <asp:BoundField DataField="Prod_Id" HeaderText="product id " HeaderStyle-BackColor="Azure"  />
                 <asp:BoundField DataField="Prod_Name" HeaderText="Product Name" HeaderStyle-BackColor="Azure" />
                <asp:BoundField DataField="Unit_Price" HeaderText="Unit Price " HeaderStyle-BackColor="Azure" />
                <asp:BoundField DataField="In_Hand" HeaderText="In Hand" HeaderStyle-BackColor="Azure" />
                <asp:BoundField DataField="Fixed" HeaderText="Fixed" HeaderStyle-BackColor="Azure" />
                <asp:BoundField DataField="Status" HeaderText="Status" HeaderStyle-BackColor="Azure" />
                 <asp:HyperLinkField DataNavigateUrlFields="Prod_Id" DataNavigateUrlFormatString="edit.aspx?Prod_Id={0}" Text="Edit" />
                <asp:ButtonField   ButtonType="Link"  Text="Delete" />        

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

部分代码

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            binddata();
        }

        SqlConnection con;
        SqlDataAdapter da;
        DataSet ds;

        void binddata()
        {
            con = new SqlConnection("Data Source=.\\sqlexpress; initial catalog=PracticeDb; user id=sa; pwd=manager;");
            con.Open();
            da = new SqlDataAdapter("Select * from Products", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            grdView.DataSource = ds;
            grdView.DataBind();
        }

        protected void grdView_SelectedIndexChanged(object sender, EventArgs e)
        {


            var P_id = ds.Tables[0].Rows[0]["Prod_Id"].ToString();

            SqlConnection con = new SqlConnection();
            con.ConnectionString = ("Data Source=.\\sqlexpress; initial catalog=PracticeDb; user id=sa; pwd=manager;");
            con.Open();
            string qry = "DELETE FROM PRODUCTS WHERE Prod_Id='" +P_id+ "'";
            SqlCommand cmd = new SqlCommand(qry, con);
            cmd.ExecuteNonQuery();
            con.Close();


        }



    }
}

谢谢

【问题讨论】:

标签: asp.net webforms


【解决方案1】:

您可以使用行数据绑定事件来完成此任务。

  <asp:LinkButton ID="lnkBtnDel" runat="server" CommandName="DeleteRow" OnClientClick="return confirm('Are you sure you want to Delete this Record?');""CommandArgument='<%#Eval("Prod_Id") %>'>Delete</asp:LinkButton>

在 rowdatabound 事件中你可以拥有

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
 if (e.CommandName == "DeleteRow")
    {
        //incase you need the row index 
        int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
        int Prod_Id= Convert.ToInt32(e.CommandArgument);
        //followed by your code 
    }
}

【讨论】:

  • 我是否在项目模板中添加链接按钮?谢谢。尝试在这里完成:stackoverflow.com/questions/29372431/…
  • @SearchForKnowledge 哇,我已经有一段时间没有回答这个问题了。是的,您必须将链接按钮放在项目模板中。
  • 我能弄明白。感谢您的回复:)
【解决方案2】:

您可以在 GridView 中使用 LinkBut​​ton

<asp:LinkButton ID="lbtnDelete"  runat="server" CommandName="delete" title="Delete"
OnClientClick="return confirm('Do you Want to Delete this Record?');"
CommandArgument='<%#Eval("Prod_Id") %>'></asp:LinkButton>

代码背后

 protected void grdView_RowCommand(object sender, GridViewCommandEventArgs e)
 {
     int P_Id = e.CommandArgument;   \\ This will get the Product id in P_Id variable
     \\ do your delete code
 }

【讨论】:

  • Gridview中没有linkBut​​ton选项
【解决方案3】:

您好,您可以使用以下代码获取链接按钮:

<asp:TemplateField HeaderText=Quantity>
  <ItemTemplate>
   <asp:LinkButton ID="lbtnDelete"  runat="server" CommandName="delete" title="Delete"
    OnClientClick="return confirm('Do you Want to Delete this Record?');"
    CommandArgument='<%#Eval("Prod_Id") %>'></asp:LinkButton>
 </ItemTemplate>
</asp:TemplateField>

后面的代码使用下面的代码:

protected void grdView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "delete")
 {

 int P_Id = e.CommandArgument;   \\ This will get the Product id in P_Id variable
 \\ do your delete code
  }
 }

【讨论】:

    猜你喜欢
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 2011-12-29
    • 1970-01-01
    • 2022-10-04
    相关资源
    最近更新 更多