如何删除GridView的一排一个JavaScript按钮,并保持在同一页上 [英] how to delete a row on gridview with a JavaScript button and remain on the same page

查看:84
本文介绍了如何删除GridView的一排一个JavaScript按钮,并保持在同一页上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过GridView控件显示一组记录,而修改删除
 按钮的旁边。我有在记录删除部分的问题。我想要的行为如下:用户点击该按钮,一个JavaScript验证函数被调用,该按钮后,单击该记录被删除,但用户保持在同一页上有记录的其余部分。我怎样才能做到在同一页上剩下的这一段时间?

 < ASP:内容ID =HeaderContent=服务器ContentPlaceHolderID =HeadContent>
< / ASP:内容>
< ASP:内容ID =的BodyContent=服务器ContentPlaceHolderID =日程地址搜索Maincontent>
    < D​​IV>        &安培; NBSP;&安培; NBSP;&安培; NBSP;
        < BR />
&安培; NBSP;        < ASP:超链接NavigateUrl =〜/ Entry.aspx=服务器文本=添加新记录/>
    < / DIV>    < D​​IV>
        < ASP:GridView控件=服务器ID =grdView的AutoGenerateColumns =假HEIGHT =100%
            WIDTH =100%onselectedindexchanged =grdView_SelectedIndexChanged>
            <柱体和GT;                < ASP:BoundField的数据字段=PROD_ID的HeaderText =产品IDHeaderStyle-背景色=天青/>
                 < ASP:BoundField的数据字段=PROD_NAME的HeaderText =产品名称HeaderStyle-背景色=天青/>
                < ASP:BoundField的数据字段=UNIT_PRICE的HeaderText =单价HeaderStyle-背景色=天青/>
                < ASP:BoundField的数据字段=In_Hand的HeaderText =牵手HeaderStyle-背景色=天青/>
                < ASP:BoundField的数据字段=固定的HeaderText =固定HeaderStyle-背景色=天青/>
                < ASP:BoundField的数据字段=状态的HeaderText =状态HeaderStyle-背景色=天青/>
                 < ASP:HyperLinkField字段DataNavigateUrlFields =PROD_IDDataNavigateUrlFormatString =edit.aspx PROD_ID = {0}?文本=编辑/>
                < ASP:ButtonField字段按钮类型=链接文本=删除/>            < /专栏>
        < / ASP:GridView的>
    < / DIV>
< / ASP:内容>


  

后面部分code


 公共部分类_Default:System.Web.UI.Page
    {
        保护无效的Page_Load(对象发件人,EventArgs的发送)
        {
            binddata();
        }        SqlConnection的CON;
        SqlDataAdapter的DA;
        数据集DS;        无效binddata()
        {
            CON =新的SqlConnection(数据源= \\\\ sqlex preSS;初始目录= PracticeDb;用户ID = SA; PWD =经理;);
            con.Open();
            DA =新SqlDataAdapter的(从产品选择*,CON);
            DataSet的DS =新的DataSet();
            da.Fill(DS);
            con.Close();
            grdView.DataSource = DS;
            grdView.DataBind();
        }        保护无效grdView_SelectedIndexChanged(对象发件人,EventArgs的发送)
        {
            VAR P_ID = ds.Tables [0] .Rows [0] [PROD_ID]的ToString()。            SqlConnection的CON =新的SqlConnection();
            con.ConnectionString =(数据源= \\\\ sqlex preSS;初始目录= PracticeDb;用户ID = SA; PWD =经理;);
            con.Open();
            字符串QRY =DELETE FROM产品哪里PROD_ID ='+ P_ID +';
            CMD的SqlCommand =新的SqlCommand(QRY,CON);
            cmd.ExecuteNonQuery();
            con.Close();
        }    }
}

感谢


解决方案

您可以使用数据绑定排事件来完成这一任务。

 < ASP:LinkBut​​ton的ID =lnkBtnDel=服务器的CommandName =DeleteRow的OnClientClick =返回确认('?您确定要删除此记录 );CommandArgument ='<%#的eval(PROD_ID)%>'>删除< / ASP:LinkBut​​ton的>

和在RowDataBound事件,你可以有

 保护无效GridView1_RowCommand(对象发件人,GridViewCommandEventArgs E)
{
 如果(e.CommandName ==DeleteRow)
    {
        //柜面您需要的行索引
        INT的rowIndex =((GridViewRow)((LinkBut​​ton的)e.CommandSource).NamingContainer).RowIndex;
        INT PROD_ID = Convert.ToInt32(e.CommandArgument);
        //后跟您code
    }
}

I am displaying a set of records through gridview, and edit and delete buttons next to them. I am having a problem in the record deletion section. The behavior I want is the following: the user clicks the button, a JavaScript validation function is called and after the button click the records are deleted, but the user remains on the same page with the rest of the records. How can I do this while remaining on the same page?

<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>

code behind part

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();


        }



    }
}

Thanks

解决方案

You can use row databound event to accomplish this task.

  <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>

and in the rowdatabound event you can have

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 
    }
}

这篇关于如何删除GridView的一排一个JavaScript按钮,并保持在同一页上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆