C# 通过 Repeater 中的 CommandArgument 传递文本框内容

发布于 2024-11-16 10:12:07 字数 683 浏览 3 评论 0原文

<asp:Repeater ID="Cartridges" runat="server" onitemcommand="Cartridges_ItemCommand">
    <ItemTemplate>
        <p class="cartprice"><%#String.Format("{0:C}", Eval("Price"))%></p>
        <hr class="hr4" /> 
        <p class="cartqty">QTY <asp:TextBox ID="cartQty" Text="0" runat="server"></asp:TextBox> </p>
        <div class="cartbuy2"><asp:LinkButton ID="buy" runat="server" CommandName="AddtoCart" CommandArgument=<%#Eval("cartID") %> Text="Buy"></asp:LinkButton></div>
   </ItemTemplate>
</asp:Repeater>

如何在 CommandArgument 中传递文本框值?抱歉彻底迷失了...

<asp:Repeater ID="Cartridges" runat="server" onitemcommand="Cartridges_ItemCommand">
    <ItemTemplate>
        <p class="cartprice"><%#String.Format("{0:C}", Eval("Price"))%></p>
        <hr class="hr4" /> 
        <p class="cartqty">QTY <asp:TextBox ID="cartQty" Text="0" runat="server"></asp:TextBox> </p>
        <div class="cartbuy2"><asp:LinkButton ID="buy" runat="server" CommandName="AddtoCart" CommandArgument=<%#Eval("cartID") %> Text="Buy"></asp:LinkButton></div>
   </ItemTemplate>
</asp:Repeater>

How can I pass the textbox value within CommandArgument? Sorry totally lost...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱已欠费 2024-11-23 10:12:07

您是否尝试过: CommandArgument='<%#Eval("cartID") %>'

这与您的不同,因为它由单引号引起来,我想这是正确的语法。

Did you try: CommandArgument='<%#Eval("cartID") %>'

this is different from yours as it is surrounded by a single quote, I guess this is the correct syntax.

摇划花蜜的午后 2024-11-23 10:12:07

使用 FindControl 获取中继器 Item 中的其他项目。例如:

 protected void repeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
 {
      LinkButton lb = (LinkButton)e.CommandSource;
      string textBoxValue = ((TextBox)lb.Parent.FindControl("cartQty")).Text;         
 }

Use FindControl to get other items in the repeater Item. For Example:

 protected void repeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
 {
      LinkButton lb = (LinkButton)e.CommandSource;
      string textBoxValue = ((TextBox)lb.Parent.FindControl("cartQty")).Text;         
 }
初见终念 2024-11-23 10:12:07

您需要将 cartId 绑定到 onItemDataBound 上的链接按钮,然后在 onItemCommand 上访问它,我已经为您修改了代码,请尝试一下

    <asp:Repeater ID="Cartridges" runat="server" onitemcommand="Repeater_OnItemCommand" OnItemDataBound="Repeater_OnItemDataBound">
<ItemTemplate>
    <p class="cartprice"><%#String.Format("{0:C}", Eval("Price"))%></p>
    <hr class="hr4" /> 
    <p class="cartqty">QTY <asp:TextBox ID="cartQty" Text="0" runat="server"></asp:TextBox> </p>
    <div class="cartbuy2"><asp:LinkButton ID="buy" runat="server" CommandName="AddtoCart" Text="Buy"></asp:LinkButton></div>

您的 onItemdatabound 应该如下所示

 protected void Repeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {

        //your code...

          LinkButton add = (LinkButton)e.Item.FindControl("buy");
                  add.CommandArgument = cartID.ToString();

    }

,然后您可以像这样访问 item 命令上的文本框

 protected void Repeater_OnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "AddtoCart")
            {
                LinkButton btnEdit = (LinkButton)e.CommandSource;
                if (btnEdit != null)
                {
                    string editId = btnEdit.CommandArgument;
                    string text = ((TextBox)e.Item.FindControl("cartQty")).Text;
                    //do some stuff with your cartid and quantity
                }
            }
}

您还可以通过添加更多链接按钮并将它们绑定到正确的命令,然后在 on item 命令中访问它们,使用编辑/删除命令参数扩展您的代码
谢谢

you need to bind the cartId to the linkbutton onItemDataBound and then access it onItemCommand, I have modified code for you, give this a go

    <asp:Repeater ID="Cartridges" runat="server" onitemcommand="Repeater_OnItemCommand" OnItemDataBound="Repeater_OnItemDataBound">
<ItemTemplate>
    <p class="cartprice"><%#String.Format("{0:C}", Eval("Price"))%></p>
    <hr class="hr4" /> 
    <p class="cartqty">QTY <asp:TextBox ID="cartQty" Text="0" runat="server"></asp:TextBox> </p>
    <div class="cartbuy2"><asp:LinkButton ID="buy" runat="server" CommandName="AddtoCart" Text="Buy"></asp:LinkButton></div>

your onItemdatabound should look like this

 protected void Repeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {

        //your code...

          LinkButton add = (LinkButton)e.Item.FindControl("buy");
                  add.CommandArgument = cartID.ToString();

    }

and then you can access the text box on item command like this

 protected void Repeater_OnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "AddtoCart")
            {
                LinkButton btnEdit = (LinkButton)e.CommandSource;
                if (btnEdit != null)
                {
                    string editId = btnEdit.CommandArgument;
                    string text = ((TextBox)e.Item.FindControl("cartQty")).Text;
                    //do some stuff with your cartid and quantity
                }
            }
}

You can also extend your code with edit/delete command arguments by adding more linkbuttons and binding them to the correct command and then accessing them in on item command
Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文