简体   繁体   中英

Asp.net c# getting value from a textbox in a list view with data that is manually bound

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.

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.

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>

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

Try this, am not sure though

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM