Asp.net C#从数据列表视图一个文本框是手动绑定中获得价值 [英] Asp.net c# getting value from a textbox in a list view with data that is manually bound

查看:88
本文介绍了Asp.net C#从数据列表视图一个文本框是手动绑定中获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想简单地读我的列表视图一个文本框。这是一个购物车,我需要手动编辑饼干当新数量中键入。我试图用我做什么了dropdownbox,但我猜,因为我手动绑定数据,而不是使用数据源的区别是preventing我接触到文本框中的值。

奇怪的是,它没有得到空,它只是没有得到任何价值?我已经添加了其他的事情标签,所以我知道这是我没怎么我打电话标签。所有我在网上找到的结果都呼吁无效的东西,如listview1.items [0] .subitems [0],这是不是会员,我可以打电话。

任何帮助是极大AP preciated

按钮处理程序

 保护无效editQ_Click(对象发件人,CommandEventArgs E)
    {        LinkBut​​ton的lbSender =(LinkBut​​ton的)寄件人;
        文本框TB =(文本框)lbSender.FindControl(TB1); //这是文本框
        productTableAdapter广告=新productTableAdapter();
        INT IDIN = int.Parse(e.CommandArgument.ToString());
        的HttpCookie C = Request.Cookies时[购物车];
        Label2.Text = tb.Text.ToString();
        // Label2.Text = tb.Text;也不行。
        如果(TB == NULL)
        {
            Label2.Text =NULL ERROR;
        }           ....    }

列表视图

 < ASP:ListView控件ID =ListView1的=服务器的DataKeyNames =productNo
         >
        < AlternatingItemTemplate>
            <跨度风格=>产品编号:
            < ASP:标签ID =productNoLabel=服务器文本='<%#的eval(productNo)%>' />
            < BR />
            名称:
            < ASP:标签ID =productNameLabel=服务器文本='<%#的eval(产品名称)%>' />
            < BR />
            数量:
            < ASP:标签ID =productQuantityLabel=服务器文本='<%#的eval(数量)%GT;' />           < ASP:文本框的id =TB1=服务器>< / ASP:文本框>
           < ASP:LinkBut​​ton的ID =editQ=服务器CommandArgument ='<%#的eval(productNo)%>'按需=editQ_Click>更改数量< / ASP:LinkBut​​ton的>            < BR />
            价钱:
            < ASP:标签ID =priceLabel=服务器文本='<%#的eval(价格)%GT;' />            < BR />
            < ASP:图片ID =IMG=服务器HEIGHT =150的ImageUrl ='<%#的eval(imgUrl的)%>'>< / ASP:图像>            < BR />
< BR />< / SPAN>
        < / AlternatingItemTemplate>


解决方案

的FindControl 是用来寻找在一个容器控件。从您的标记,LinkBut​​ton的是不是你的文本框的容器。您的文本框是你的ListView

试试这个,我不知道,虽然

 文本框TB =(文本框)lbSender.NamingContainer.FindControl(TB1);

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

这篇关于Asp.net C#从数据列表视图一个文本框是手动绑定中获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆