[英]Asp.net c# getting value from a textbox in a list view with data that is manually bound
我想在listview中簡單地閱讀一個文本框。 這是一個購物車,我需要在輸入“新數量”時手動編輯cookie。我正在嘗試使用我為下拉框做的,但我猜是因為我手動綁定數據而不是使用datasource是阻止我訪問文本框中的值的區別。
奇怪的是,它沒有變為空,它只是沒有得到任何價值? 我在標簽上添加了其他東西,所以我知道這不是我打電話給標簽的方式。 我在網上找到的所有結果都是調用無效的東西,例如listview1.items [0] .subitems [0],這些不是我可以調用的成員。
任何幫助是極大的贊賞
按鈕處理程序
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";
}
....
}
列表顯示
<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
用於查找容器中的控件。 從您的標記,LinkButton不是您的文本框的容器。 您的文本框位於ListView中
試試這個,我不確定
TextBox tb = (TextBox)lbSender.NamingContainer.FindControl("tb1");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.