如何删除横切表中的行而无需将页面发布到C#中的服务器? [英] how to delete row from transection table without posting page to the server in C#?

查看:74
本文介绍了如何删除横切表中的行而无需将页面发布到C#中的服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不将页面发布到C#中的服务器的情况下从事务表中删除行?

How to delete row from transaction table without posting page to the server in C#?

//this is a sales.aspx file:

<asp:GridView ID="gvproductinfo" runat="server">
                <Columns>
                    <asp:TemplateField HeaderText="Cancel">
                        <ItemTemplate>
                            <asp:CheckBox ID="chk1" runat="server"/>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Quantity">
                        <ItemTemplate>
                            <asp:TextBox ID="txtquantity" runat="server"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Price">
                        <ItemTemplate>
                            <asp:TextBox ID="txtprice" runat="server"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Commitement Date">
                        <ItemTemplate>
                            <asp:TextBox ID="txtcommitementdate" runat="server" style="margin-top: 0px"></asp:TextBox>
                            <asp:CalendarExtender ID="CalendarExtender1" runat="server" 
                                PopupButtonID="txtcommitementdate" TargetControlID="txtcommitementdate" 
                                Format="MM/dd/yyyy">
                            </asp:CalendarExtender>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
    </asp:GridView>


//this is a sales.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;



public partial class salesordertrancation : System.Web.UI.Page
{
    DataTable dt;
    SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!Page.IsPostBack)
        {
           
            loaddatatogrid();
        }
        dt = new DataTable("MyTable");
        dt.Columns.Add("ProductCode");
        dt.Columns.Add("ProductName");
    }
     
    protected void loaddatatogrid()
    {

        if (cn.State == ConnectionState.Closed)
            cn.Open();
        SqlCommand cm = new SqlCommand("select CustomerCode,CustomerName from customer ", cn);
        SqlDataReader dr = cm.ExecuteReader();
        gvcustomer.DataSource = dr;
        gvcustomer.DataBind();
        cn.Close();
    }
    protected void gvcustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = gvcustomer.SelectedRow;
        txtcustomercode.Text = row.Cells[1].Text;
        txtcustomername.Text = row.Cells[2].Text; 
    }
     protected  void databound()
    {
        if (cn.State == ConnectionState.Closed)
            cn.Open();
        SqlCommand cm = new SqlCommand("select ProductCode,ProductName from product ", cn);
        SqlDataReader dr = cm.ExecuteReader();
        if (dr.HasRows)
        {
            gvproduct.DataSource = dr;
            gvproduct.DataBind();
            
        }
        dr.Close();
        cn.Close();
    }
    protected void btnshowproducts_Click(object sender, EventArgs e)
    {
        databound();
        btnadd.Visible = true;
    }
    

    protected void btnAddproducts_Click1(object sender, EventArgs e)
    {
        SqlTransaction trans = null;
        try
        {
            if (cn.State == ConnectionState.Closed)
                cn.Open();

            trans = cn.BeginTransaction();
            int cnt;
            
            SqlCommand cm = new SqlCommand("select max(SalesOrderCode) from salesorder", cn);
            cm.Transaction = trans;
            if (cm.ExecuteScalar().ToString() == "")
            {
                cnt = 1;
            }
            else
            {
                cnt = (int)cm.ExecuteScalar();
                cnt = cnt + 1;
            }
            cm = new SqlCommand("insert into salesorder values(@p1,@p2,@p3,@p4) ", cn);
            cm.Transaction = trans;
            cm.Parameters.Add("@p1", SqlDbType.Int).Value = cnt;
            cm.Parameters.Add("@p2", SqlDbType.DateTime).Value = Convert.ToDateTime(txtdate.Text);
            cm.Parameters.Add("@p3", SqlDbType.Int).Value = Convert.ToInt16(txtcustomercode.Text);
            cm.Parameters.Add("@p4", SqlDbType.VarChar).Value = txtremarks.Text;
            cm.ExecuteNonQuery();
            
            for (int i = 0; i < gvproductinfo.Rows.Count; i++)
            {            
                GridViewRow row = gvproductinfo.Rows[i];

                TextBox t1 = (TextBox)row.FindControl("txtquantity");
                TextBox t2 = (TextBox)row.FindControl("txtprice");
                TextBox t3 = (TextBox)row.FindControl("txtcommitementdate");

                cm = new SqlCommand("insert into salesordertran(SalesOrderCode,ProductCode,SalesOrderTranQty,SalesOrderTranPrice,SalesOrderCommDate) values(@p1,@p2,@p3,@p4,@p5)", cn);
                cm.Transaction = trans;
                cm.Parameters.Add("@p1", SqlDbType.Int).Value = cnt;
                cm.Parameters.Add("@p2", SqlDbType.Int).Value = Convert.ToInt16(gvproduct.Rows[i].Cells[1].Text);
                cm.Parameters.Add("@p3", SqlDbType.Int).Value = Convert.ToInt16(t1.Text);
                cm.Parameters.Add("@p4", SqlDbType.Int).Value = Convert.ToInt16(t2.Text);
                cm.Parameters.Add("@p5", SqlDbType.DateTime).Value = Convert.ToDateTime(t3.Text);
                //cm2.Parameters.Add("@p6", SqlDbType.Bit).Value = Convert.ToBoolean(gvproduct.Rows[j].Cells[7].Text);
                //cm2.Parameters.Add("@p7", SqlDbType.DateTime).Value = Convert.ToDateTime(gvproduct.Rows[j].Cells[5].Text);
                //cm2.Parameters.Add("@p8", SqlDbType.Bit).Value = Convert.ToBoolean(gvproduct.Rows[j].Cells[6].Text);
                //cm2.Parameters.Add("@p9", SqlDbType.DateTime).Value = Convert.ToDateTime(gvproduct.Rows[j].Cells[7].Text);
                cm.ExecuteNonQuery();
            }
            trans.Commit();
            Label1.Text = "Order is Placed Successfully!!!!!!";
            ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('Production process completed!!');", true);

            cn.Close();
            
        }
        catch (Exception ex)
        {
            Response.Write(ex);
            trans.Rollback();
            cn.Close();
        }

        
    }

    protected void btnadd_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < gvproduct.Rows.Count; i++)
            {
                GridViewRow row = gvproduct.Rows[i];
                CheckBox c1 = (CheckBox)row.FindControl("chk");
                if (c1.Checked == true)
                {
                    DataRow drow = dt.NewRow();
                    drow["ProductCode"] = row.Cells[1].Text;
                    drow["ProductName"] = row.Cells[2].Text;
                    dt.Rows.Add(drow);
                    gvproductinfo.DataSource = dt;
                    gvproductinfo.DataBind();

                    
                }
            }
            btnAddproducts.Visible = true;
        }

        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }
    
}


现在我的问题是:

如果错误,我会在产品gridview中选择一个产品.

然后点击添加按钮.

所以我在productinfo gridview中有一个产品信息.

但是现在我想从productinfo gridview删除行.

在删除警报框之前,必须先弹出它.

因此,我该怎么办?


Now my problem is:

if by mistake, I select a product in product gridview.

And then clik on add button.

So I hav a product information in productinfo gridview.

But now i want to delete row from productinfo gridview.

And before deletion the alertbox has to b pop up.

So what should I do?

推荐答案

有很多方法可以完成此操作.看看下面的代码项目文章,了解如何使用Javascript中的WCF.

在JavaScript中使用WCF服务的入门指南使用ASP.NET AJAX [ ^ ]

选项2:将ASP.Net MVC与删除操作配合使用.
选项3:如果您不想使用WCF或MVC,以下文章是更好的选择之一

使用HTTP处理程序和jQuery在ASP.NET Web应用程序中进行CRUD操作 [ ^ ]
There are many ways to accomplish this. Take a look at the following code project article, for using WCF from Javascript.

A beginner’s guide for consuming a WCF service in JavaScript using ASP.NET AJAX[^]

Option 2: would be to use ASP.Net MVC with delete action.
Option 3: If you dont want to use WCF or MVC, following article is one of the better options

CRUD Operation in ASP.NET Web Applications Using HTTP Handler and jQuery[^]


这篇关于如何删除横切表中的行而无需将页面发布到C#中的服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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