2014-05-12 28 views
0

我一直在解決我的問題,但我似乎沒有找到解決我的問題的充分辦法。我有以下的代碼:如何在ListView中正確使用UpdatePanel?

<asp:ScriptManager EnablePartialRendering="true" ID="ScriptManager1" runat="server"></asp:ScriptManager> 

<asp:UpdatePanel ID="UpdatePanel" UpdateMode="Conditional" runat="server"> 
    <ContentTemplate> 
    <asp:ListView ID="productList" OnItemDataBound="pharmaciesList_ItemDataBound" runat="server"> 
    <ItemTemplate> 
     <asp:TextBox CssClass="span1" ID="Units" runat="server" AutoPostBack="true" OnTextChanged="Units_TextChanged" /> 
     <asp:Literal runat="server" ID="Discount" /> 
    </ItemTemplate> 
    <Triggers> 
    <asp:AsyncPostBackTrigger ControlID="Units" EventName="TextChanged" /> 
    </Triggers> 
    </asp:ListView> 

後面的代碼:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     productList.DataSource = this.LineItems; 
     DataBind(); 
    } 
} 

protected void Unidades_TextChanged(object sender, EventArgs e) 
{ 
    TextBox qtt = (TextBox)sender; 
    var parent = qtt.Parent; 
    while (!(parent is IDataItemContainer)) 
     parent = parent.Parent; 

    ListViewDataItem listitem = parent as ListViewDataItem; 

    Literal lit = listitem.FindControl("Descuento") as Literal; 
    lit.Text = "A"; 
} 

每個項目包含了單位的文本框,並應與新的折扣取決於量進入更新的文字(用於測試的目的,我只是希望這個標籤暫時變成「A」)。即使在使用調試器之後,在更改該列表中的文本框之後執行lit.Text =「A」,但文字未被更新。

我在做什麼錯了,該怎麼做才能正確更新它?

謝謝

回答

0

對於之前的代碼,我假設ListView爲Listbox,對不起。 Actucally它將永遠無法找到列表視圖中的文本框。

雖然有一點是肯定的,你需要在<ContentTemplate>之外寫<trigger>

您使用Listview的任何特定原因。如果你可以使用Listbox/Grid?

+0

你的方法引發了一個異常:在UpdatePanel'UpdatePanel'中找不到觸發器的ID爲'Units'的控件。 –

+0

ok讓我再次檢查 –

+0

請檢查我編輯的答案 –

相關問題