AsyncPostback Trigger within ListView Control - CheckBox (doing full postpack)

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I have searched everywhere and cant find a solution to this. Im using Visual Studio 2012 (ASP.NET 4.5 Framework). I have a ListView control with an LayoutTemplate in which has a CheckBox control (this is repeated for each databound row). I have an updatepanel outside the listview which has a label I was to update when the user checks/uncheckes any of the checkboxes in the listview control. This is working, however no matter what I do, it does a full postback.
To reproduce. In VS2012 start a New ASP.NET Web Forms Application. This has a Site.Master MasterPage and a few pages for a new asp.net project. Edit the About.aspx and replace with the following page and code below. What I have done in this example is made a Text Box control and button that shows that the partial postback is working. I update the Label2 (which is not in an updatepanel) on post back. If the trigger is working in the listview, label2s should not update until a full postback.
I have tried (as one suggestion): ClientIDMode="AutoID"on the updatepanel label, and also on the page as whole (which doesnt seem to make a difference).
<asp:Label ID="Label2" runat="server" Text="Label </asp:Label>
<asp:TextBox ID="TextBox1" runat="server </asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

<asp:ListView ID="ListViewProducts" runat="server" ItemPlaceholderID="ProductItem" OnItemDataBound="ListViewProducts_ItemDataBound" >
<ItemTemplate>
<div class="Product
<asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="true" ClientIDMode="AutoID" />

<asp:Label runat="server" ID="LabelId" Text=<%# Eval("Id") %>></asp:Label>
::
<asp:Label runat="server" ID="LabelName" Text=<%# Eval("Name") %>></asp:Label>

<br />

<asp:Label runat="server" ID="LabelDescription" Text=<%# Eval("Description") %>></asp:Label>


</ItemTemplate>
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="ProductItem </asp:PlaceHolder>
</LayoutTemplate>
<ItemSeparatorTemplate>
<hr />
</ItemSeparatorTemplate>
</asp:ListView>


<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional
<ContentTemplate>
Update Panel:
<asp:Label ID="Label1" runat="server" Text="Label </asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>

Code behind: public class Product
{
private int? _Id;
private string _Name;
private string _Descrition;

public Product() { }

public Product(int Id, string Name, string Description)
{
this._Id = Id;
this._Name = Name;
this._Descrition = Description;
}

/// <span class="code-SummaryComment <summary>
/// Product Id
/// <span class="code-SummaryComment </summary>
public int? Id
{
get { return _Id; }
set { _Id = value; }
}

/// <span class="code-SummaryComment <summary>
/// Product Name
/// <span class="code-SummaryComment </summary>
public string Name
{
get { return _Name; }
set { _Name = value; }
}

/// <span class="code-SummaryComment <summary>
/// Product Complete Description
/// <span class="code-SummaryComment </summary>
public string Description
{
get { return _Descrition; }
set { _Descrition = value; }
}
}

public class ProductList
{
private IList<Product> _ProductDB = new List<Product>();

public ProductList()
{
this._ProductDB.Add(new Product(1, "Computer", "Complete hardware with software included."));
this._ProductDB.Add(new Product(2, "Kitchen Calendar", "Beautiful caledar for your kitchen."));
this._ProductDB.Add(new Product(3, "Shoes", "Most advanced anti-impact system in a small shoe."));
this._ProductDB.Add(new Product(4, "Pen", "What you think, must be written. This pen is your helper."));
this._ProductDB.Add(new Product(5, "Cell Phone", "Powerfull comunication thing. Today is part of your body. Get one more."));
}

public IList<Product> GellAll()
{
return this._ProductDB;
}
}

public partial class About : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ProductList db = new ProductList();
this.ListViewProducts.DataSource = db.GellAll();
this.ListViewProducts.DataBind();
}

Label2.Text = DateTime.Now.Ticks.ToString();
}


protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = TextBox1.Text;
}

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.Ticks.ToString();
UpdatePanel1.Update();
}



protected void ListViewProducts_ItemDataBound(object sender, ListViewItemEventArgs e)
{
CheckBox cb = e.Item.FindControl("CheckBox1") as CheckBox;
ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(cb);
}

}

View the full article
 
Back
Top