【问题标题】:LinkButtons inside a Grid template to increment and decrement the lables value网格模板内的链接按钮以增加和减少标签值
【发布时间】:2016-01-26 12:43:04
【问题描述】:
 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;
                }
            }
        }
    }

我在 Grid 模板中使用 LinkBut​​tons。我希望能够在单击 LinkBut​​tons 时引发事件,lable 的值在 + 链接上增加并在 - 链接按钮上减少,因为 lable 包含添加到发票的产品数量。我怎样才能做到这一点?

【问题讨论】:

  • 这里是来源@Rahul Singh
  • 我猜是gridview 而不是Datagridview

标签: c# asp.net gridview hyperlink


【解决方案1】:

我相信这就是你想要的......
您不会在RowDataBound 中进行增量,因为RowDataBound 会在您绑定GridView 时触发

.aspx

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

.cs

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2018-01-12
    • 1970-01-01
    相关资源
    最近更新 更多