Grid模板中的LinkBut​​tons用于递增和递减标签值

时间:2015-10-27 07:42:41

标签: c# asp.net gridview hyperlink

 protected void Gridproducts_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink hp = new HyperLink();
            hp = (HyperLink)e.Row.FindControl("linkSelectprd");
            var Pid = DataBinder.Eval(e.Row.DataItem, "product_id").ToString();
            var Catid = Request.QueryString["Cid"].ToString();
            hp.NavigateUrl = "Sales.aspx?Cid="+Catid+"&"+"Pid="+Pid;
            if (!IsPostBack && Request.QueryString["Pid"] != null)
            {
                this is the variable in which the value of quantity increments
                int i=0;
                lbltotalquantity.Text = i.ToString() + 1;
                }
            }
        }
    }

我在网格模板中使用LinkBut​​tons。我希望能够在单击LinkBut​​tons时引发事件,lable的值在+ link上递增并在on-link按钮上递减,因为lable包含添加到invoice中的Products数量。我怎么能做到这一点?

1 个答案:

答案 0 :(得分:0)

我相信这就是你想要的......
您不会在RowDataBound中执行增量操作,因为RowDataBound绑定GridView时会触发<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager> <asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:GridView ID="gv" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField HeaderText="Product"> <ItemTemplate> <asp:Label ID="lblProduct" runat="server" Text='<%# Eval("Product") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Quantity"> <ItemTemplate> <asp:Label ID="lblQuantity" runat="server" Text="0"></asp:Label> &nbsp; <asp:LinkButton ID="lbtnPlus" runat="server" Text="+" OnClick="lbtnPlus_Click"></asp:LinkButton> &nbsp; <asp:LinkButton ID="lbtnMinus" runat="server" Text="-" OnClick="lbtnMinus_Click"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="gv" /> </Triggers> </asp:UpdatePanel>

的.aspx

protected void Page_Load(object sender, EventArgs e)
{
    // Check
    if (!IsPostBack)
    {
        // Variable
        string[] product = { "Dell", "Asus", "Acer", "Toshiba", "Fujishu", "VAIO" };
        DataTable dt = new DataTable();
        dt.Columns.Add("Product");

        for (int i = 0; i < product.Length; i++)
            dt.Rows.Add(product[i]);

        gv.DataSource = dt;
        gv.DataBind();

        // Dispose
        dt.Dispose();

    }
}

private void DoTheMath(GridViewRow row, bool isAdd)
{
    // Variable
    bool isNumber = false;
    int currentValue = 0;

    // Find Control
    Label lblQuantity = row.FindControl("lblQuantity") as Label;

    // Check
    if (lblQuantity != null)
    {
        // Check
        if (lblQuantity.Text.Trim() != string.Empty)
        {
            isNumber = int.TryParse(lblQuantity.Text.Trim(), out currentValue);

            // Check
            if (isNumber)
            {
                // Is Add
                if (isAdd)
                    currentValue++;
                else
                {
                    // Check cannot be less than 0
                    if (currentValue > 0)
                        currentValue--;
                }
            }

            // Set to TextBox
            lblQuantity.Text = currentValue.ToString();
        }
    }
}

protected void lbtnPlus_Click(object sender, EventArgs e)
{      
    // Get
    LinkButton lbtn = sender as LinkButton;
    GridViewRow row = lbtn.NamingContainer as GridViewRow;

    DoTheMath(row, true);
}

protected void lbtnMinus_Click(object sender, EventArgs e)
{
    // Get
    LinkButton lbtn = sender as LinkButton;
    GridViewRow row = lbtn.NamingContainer as GridViewRow;

    DoTheMath(row, false);
}

的.cs

Buffer#array