【问题标题】:Setting the previous gridview data of unbound data fields设置未绑定数据字段之前的gridview数据
【发布时间】:2016-04-24 04:09:12
【问题描述】:

我又卡住了!我正在制作一个销售应用程序,通过选择几个复选框从数据库中获取项目。在选择某些复选框后,我会单击一个按钮,然后在网格视图中获取项目。我有 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>

这里是代码隐藏:

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


}

【问题讨论】:

    标签: asp.net gridview


    【解决方案1】:

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

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


    您可能会特别感兴趣:

    应用参考HttpApplicationState

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

    Cookie 参考:“会话”cookie: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);
    

    【讨论】:

      猜你喜欢
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      • 2019-06-11
      • 1970-01-01
      • 2012-09-16
      • 2014-02-09
      相关资源
      最近更新 更多