设置未绑定数据字段的先前gridview数据

时间:2014-07-12 04:11:35

标签: asp.net gridview

我再次陷入困境!我正在制作一个销售应用程序,我通过选择几个复选框从数据库中获取项目。选中某些复选框后,我会单击一个按钮,并在gridview中获取这些项目。我有2个未绑定的字段 - “数量”(有下拉列表)和“金额”(作为标签)。我选择数量,金额在“金额”列中计算。我还完美地计算了“金额”列的总和。

当我取消选中复选框或想要删除某个项目时,问题就开始了,回发事件会触发并删除该项目,或者该行但是并排显示下拉列表中的先前选定值以及金额标签也会被撤消 - 我的意思是先前的值丢失,因为它将它作为一个全新的查询。

有没有办法保存以前的值,无论我删除或添加网格中的新项目.... ??请帮忙!

aspx如下:

<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" 
        OnRowDataBound="GridView1_RowDataBound" ShowFooter="true">
          <footerstyle backcolor="LightCyan"
      forecolor="MediumBlue"/>
<Columns>
    <asp:BoundField DataField="item_ID" HeaderText="ID" />
    <asp:TemplateField HeaderText="Quantity">
        <ItemTemplate>
<asp:DropDownList ID="ddlQuantity" runat="server" AutoPostBack="true"      OnSelectedIndexChanged="ddlQuantity_SelectedIndexChanged"></asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="purchase_price">
        <ItemTemplate>
            <asp:Label ID="lblPrice" Text='<%#Eval("purchase_price") %>' runat="server" ></asp:Label>
        </ItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField HeaderText="Amount">
        <ItemTemplate>                        
            <asp:Label ID="lblAmount" Text='<%#Eval("purchase_price") %>' runat="server" ></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

</Columns>

    

这是CodeBehind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {

    }

    else
    {
        //this is where I am stuck I think so....

        //GridViewRow gvr = ((DropDownList)sender).NamingContainer as GridViewRow;

        //for (int i = 0; i < GridView1.Rows.Count; i++)
        //{
        //    var ddlQuantity = gvr.FindControl("ddlQuantity") as DropDownList;
        //    Label2.Text = ddlQuantity.SelectedValue.ToString();

       //}
    }

}
protected void Button2_Click(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        List<String> wheres = new List<String>();

        if (CheckBox1.Checked)
        {
            wheres.Add("'pepsi'");
        }

        if (CheckBox2.Checked)
        {
            wheres.Add("'coke'");
        }

        if (CheckBox3.Checked)
        {
            wheres.Add("'juice'");
        }
        String whereclause = String.Join(",", wheres.ToArray());



        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=xxx; Initial Catalog=xxx; Integrated Security=True";
        conn.Open();
        //SqlCommand cmd = new SqlCommand("select * from item_purchase where item_name in ("+whereclause+")", conn);

        SqlCommand cmd = new SqlCommand("select item_purchase.purchase_date,item_purchase.purchase_price,item_purchase.item_ID,items.item_name from item_purchase inner join items on items.item_ID = item_purchase.item_ID where items.item_name in (" + whereclause + ") ", conn);

        //cmd.Parameters.AddWithValue("@item_names", whereclause);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();

        ViewState["currentTable"] = ds;


    }

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var ddl = e.Row.FindControl("ddlQuantity") as DropDownList;
        if (ddl != null)
        {
            ddl.DataSource = new List<string>() { "0", "1", "2", "3", "4" };
            ddl.DataBind();
        }
    }
}

protected void ddlQuantity_SelectedIndexChanged(object sender, EventArgs e)
{
    int quantity;
    GridViewRow gvr = ((DropDownList)sender).NamingContainer as GridViewRow;
    var ddlQuantity = gvr.FindControl("ddlQuantity") as DropDownList;
    var lblAmount = gvr.FindControl("lblAmount") as Label;
    var lblPrice = gvr.FindControl("lblPrice") as Label;
    int.TryParse(ddlQuantity.SelectedValue, out quantity);
    //Label1.Text = quantity.ToString();

    int pr = int.Parse(lblPrice.Text);
    int qt = int.Parse(ddlQuantity.SelectedValue);

    //Label2.Text += (pr*qt).ToString();


    lblAmount.Text = (pr * qt).ToString();

    int sum = 0;
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        gvr = GridView1.Rows[i] as GridViewRow;
        var lblrowamnt = gvr.FindControl("lblAmount") as Label;
        sum += int.Parse(lblrowamnt.Text);
    }
    Label2.Text = sum.ToString();


}

1 个答案:

答案 0 :(得分:0)

我倾向于避免Web表单开发以及回发中的所有asp.net服务器控件(页面除外)。当然,我建议也这样做。

无论如何,您对 ASP.NET State Management的概念感兴趣:


您可能对以下内容特别感兴趣:

应用参考 HttpApplicationState

会话参考 :( inproc)HttpSessionState对象:ASP.NET Session Overview

Cookie参考 :&#39;会话&#39; cookies:ASP.NET Cookies Overview


System.Web.UI.Page类本身(您可能在页面后面的代码中继承)继承了已公开这些实例的属性..

申请方法代码示例

this.Application("SomeObject") = SomeObject;
SomeType SomeObject = (SomeType)this.Application("SomeObject");

会话方法代码示例

this.Session("SomeObject") = SomeObject;    
SomeType SomeObject = (SomeType)this.Session("SomeObject");

Cookie方法代码示例

HttpCookie aCookie = new HttpCookie("SomeSettingValue");    
aCookie.Value = DateTime.Now.ToString();    
aCookie.Expires = DateTime.Now.AddDays(1);    
this.Response.Cookies.Add(aCookie);