Asp.net c#从列表视图中的文本框获取值,手动绑定数据

时间:2022-12-02 11:47:24

I'm trying to simply read a textbox in my listview. It's a shopping cart and I need to manually edit the cookie when the "new quantity" is typed in. I'm trying to use what I did for a dropdownbox, but I'm guessing because I manually bind the data instead of using a datasource is the difference preventing me access to the value in the textbox.

我想在listview中简单地阅读一个文本框。这是一个购物车,我需要在键入“新数量”时手动编辑cookie。我正在尝试使用我为dropdownbox所做的事情,但我猜是因为我手动绑定数据而不是使用datasource是阻止我访问文本框中的值的区别。

The weird thing is that it isn't getting null, it just isn't getting any value? I've added other things to the label so I know it's not how I'm calling label. All the results I find online are calling invalid things such as listview1.items[0].subitems[0], which are not members I can call.

奇怪的是,它没有得到空,它只是没有得到任何价值?我已经在标签上添加了其他东西,所以我知道这不是我打电话给标签的方式。我在网上找到的所有结果都是调用无效的东西,例如listview1.items [0] .subitems [0],这些不是我可以调用的成员。

Any help is greatly appreciated

任何帮助是极大的赞赏

Button Handler

按钮处理程序

 protected void editQ_Click(Object sender, CommandEventArgs e)
    {

        LinkButton lbSender = (LinkButton)sender;
        TextBox tb = (TextBox)lbSender.FindControl("tb1"); // this is the textbox
        productTableAdapter ad = new productTableAdapter();
        int idIn = int.Parse(e.CommandArgument.ToString());
        HttpCookie c = Request.Cookies["cart"];
        Label2.Text = tb.Text.ToString();
        // Label2.Text = tb.Text; doesn't work either.
        if (tb == null)
        {
            Label2.Text = "NULL ERROR";
        }

           ....



    }

Listview

列表显示

<asp:ListView ID="ListView1" runat="server" DataKeyNames="productNo" 
         >
        <AlternatingItemTemplate>
            <span style="">productNo:
            <asp:Label ID="productNoLabel" runat="server" Text='<%# Eval("productNo") %>' />
            <br />
            Name:
            <asp:Label ID="productNameLabel" runat="server" Text='<%# Eval("productName") %>' />
            <br />
            Quantity:
            <asp:Label ID="productQuantityLabel" runat="server" Text='<%# Eval("Quantity") %>' />

           <asp:TextBox id = "tb1" runat="server"></asp:TextBox>
           <asp:LinkButton id="editQ" runat="server" CommandArgument='<%# Eval("productNo") %>' onCommand ="editQ_Click">Change Quantity</asp:LinkButton>

            <br />
            price:
            <asp:Label ID="priceLabel" runat="server" Text='<%# Eval("price") %>' />

            <br />
            <asp:Image ID = "img" runat="server" height = "150" ImageUrl='<%# Eval("imgURL")%>'></asp:Image>

            <br />
<br /></span>
        </AlternatingItemTemplate>

1 个解决方案

#1


2  

FindControl is used to find controls in a container. From your markup, LinkButton is not a container for your textbox. Your textbox is in your ListView

FindControl用于查找容器中的控件。从您的标记,LinkBut​​ton不是您的文本框的容器。您的文本框位于ListView中

Try this, am not sure though

试试这个,但我不确定

TextBox tb = (TextBox)lbSender.NamingContainer.FindControl("tb1"); 

#1


2  

FindControl is used to find controls in a container. From your markup, LinkButton is not a container for your textbox. Your textbox is in your ListView

FindControl用于查找容器中的控件。从您的标记,LinkBut​​ton不是您的文本框的容器。您的文本框位于ListView中

Try this, am not sure though

试试这个,但我不确定

TextBox tb = (TextBox)lbSender.NamingContainer.FindControl("tb1");