I have a big problem that is when I use full post-back I can find controls in my …_ItemDataBound(…) event in my DataList, but when use
AsyncPostBackTrigger I can’t find controls and gives me null.
Here’s my aspx code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DataList ID="DataListGallery" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" OnItemDataBound="DataListGallery_ItemDataBound" >
<ItemTemplate>
<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>
<asp:HiddenField ID="FieldPhoneId" Value='<%# Eval("Phone_InfoID") %>' runat="server" />
<img src="../images/cart.gif" alt="" title="" border="0" class="left_bt" /></a>--%>
<asp:ImageButton ID="btnShop" OnClick="btnShop_Click" ImageUrl="images/cart.gif" CssClass="left_bt_item" title="header=[خريد] body=[ ] fade=[on]" runat="server" />
<img src="../images/favs.gif" alt="" title="" border="0" class="left_bt" /></a>--%>
<asp:ImageButton CssClass="left_bt_item" title="header=[مورد علاقه] body=[ ] fade=[on]" OnClick="btnFavourite_Click" ID="btnFavourite" ImageUrl="images/unfav.png" runat="server" />
<img src="../images/favorites.gif" alt="" title="" border="0" class="left_bt" /></a>--%>
</LoggedInTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="LinkButton1" EventName="Command"></asp:AsyncPostBackTrigger>
<asp:AsyncPostBackTrigger ControlID="LinkButton2" EventName="Command" />
<asp:AsyncPostBackTrigger ControlID="LinkButton3" EventName="Command" />
<asp:AsyncPostBackTrigger ControlID="LinkButton4" EventName="Command" />
<asp:AsyncPostBackTrigger ControlID="LinkButton5" EventName="Command" />
<asp:AsyncPostBackTrigger ControlID="LinkButton6" EventName="Command" />
<asp:AsyncPostBackTrigger ControlID="LinkButton7" EventName="Command" />
<asp:AsyncPostBackTrigger ControlID="LinkButton0" EventName="Command" />
<asp:AsyncPostBackTrigger ControlID="btnSearchHead" EventName="Command" />
<asp:AsyncPostBackTrigger ControlID="LinkButton8" EventName="Command" />
<asp:AsyncPostBackTrigger ControlID="lnkNext" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="lnkPrevious" EventName="Click" />
</Triggers>
and Code-Behind:
protected void DataListGallery_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (User.Identity.IsAuthenticated)
{
// Get LoginView for access to ImageButton on it.
var loginView = e.Item.FindControl("LoginView1");
ImageButton btnFav = (ImageButton)loginView.FindControl("btnFavourite");
HiddenField hf = (HiddenField)loginView.FindControl("FieldPhoneId");
List<int> listFav = (List<int>)Session["Fav"];
if (listFav.Contains(int.Parse(hf.Value)))
btnFav.ImageUrl = "~/images/favs.gif";
}
}
When I’m login and use AsyncPostBackTrigger, I can’t access to these controls:
btnFav and hf. Notice that they are inside LoginView.
Thanks.
Looks like your LoggedInTemplate is not rendered, i.e. you are still with AnonymousTemplate. Can you confirm that these buttons are displayed on a page once you have logged in?
Have you tried placing a breakpoint within your code listed above to ensure that it is getting past the IsAuthenticated section? I can only assume that the current user is not logged in and that the LoginView is properly rendered.
I would also ensure that you have the appropriate closing tag for your LoginView Control as I currently do not see one within your code :
<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>
<asp:HiddenField ID="FieldPhoneId" Value='<%# Eval("Phone_InfoID") %>' runat="server" />
<img src="../images/cart.gif" alt="" title="" border="0" class="left_bt" /></a>
<asp:ImageButton ID="btnShop" OnClick="btnShop_Click" ImageUrl="images/cart.gif" CssClass="left_bt_item" title="header=[خريد] body=[ ] fade=[on]" runat="server" />
<img src="../images/favs.gif" alt="" title="" border="0" class="left_bt" /></a>
<asp:ImageButton CssClass="left_bt_item" title="header=[مورد علاقه] body=[ ] fade=[on]" OnClick="btnFavourite_Click" ID="btnFavourite" ImageUrl="images/unfav.png" runat="server" />
<img src="../images/favorites.gif" alt="" title="" border="0" class="left_bt" /></a>
</LoggedInTemplate>
</asp:LoginView>
I closed tag LoginView, sorry, I didn’t close tag in my question.
I changed my code with added try catch block:
protected void DataListGallery_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (User.Identity.IsAuthenticated)
{
try
{
// Get LoginView for access to ImageButton on it.
var loginView = e.Item.FindControl("LoginView1");
ImageButton btnFav = (ImageButton)loginView.FindControl("btnFavourite");
HiddenField hf = (HiddenField)loginView.FindControl("FieldPhoneId");
List<int> listFav = (List<int>)Session["Fav"];
if (listFav.Contains(int.Parse(hf.Value)))
btnFav.ImageUrl = "~/images/favs.gif";
}
catch(Exception ex)
{
}
}
}
and it caused to solve biding problem.see this code:
// Enable pageing, becuase when page is posted back you must re-create controls again
static bool enable = false;
/// <summary>
/// Method to bind Data To Repeter Control
/// </summary>
private void BindDataToRepeter<T>(List<T> query)
{
//Create an object of PagedDataSource Object
PagedDataSource objPagedDataSource = new PagedDataSource();
objPagedDataSource.DataSource = query;
objPagedDataSource.AllowPaging = true;
objPagedDataSource.PageSize = 1; //Set the Page Size, Means the number os records you want to dispay in a page
// Get Query string
string brand = Request["brand"];
string model = Request["model"];
if (GetCurrentPageNumber >= objPagedDataSource.PageCount)
{
// Set link to LinkButton
if (brand != "" && brand != null)
Response.Redirect("Default.aspx?brand=" + brand + "&Page=" + (objPagedDataSource.PageCount).ToString());
else if (model != "" && model != null)
Response.Redirect("Default.aspx?model=" + model + "&Page=" + (objPagedDataSource.PageCount).ToString());
else
Response.Redirect("Default.aspx?Page=" + (objPagedDataSource.PageCount).ToString());
}
objPagedDataSource.CurrentPageIndex = GetCurrentPageNumber;
if (objPagedDataSource.PageCount > 0)
{
try
{
DataListGallery.Visible = true;
string a = DataListGallery.DataSourceID;
DataListGallery.DataSource = objPagedDataSource; DataListGallery.DataBind();
}
catch(Exception ex)
{
}
}
I had problem with DataListGallery.DataBind(); because it calls DataListGallery_ItemDataBound() and this method took me null btnFav and hf, but I added try catch block and solve it.Notice that DataListGallery_ItemDataBound() calls two time. for first time it gives null again but for two time it doesn’t give null.
see this code:
protected void Page_Load(object sender, EventArgs e)
{
// enable property is re-creating page controls
if (!Page.IsPostBack || enable)
{
...
}}
I re-created controls for paging my DataList.and I have other problem that is my btnFav doesn’t work.
I solve my problem from this site :
http://www.aspdotnetfaq.com/Faq/how-to-determine-whether-an-asynchronous-partial-postback-has-occurred-on-page.aspx
protected void Page_Load(object sender, EventArgs e)
{
//http://www.aspdotnetfaq.com/Faq/how-to-determine-whether-an-asynchronous-partial-postback-has-occurred-on-page.aspx
// get a reference to ScriptManager and check if we have a partial postback
if (ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)
{
// partial (asynchronous) postback occured
// insert Ajax custom logic here
}
// enable property is re-creating page controls
else if (!Page.IsPostBack || enable)
{
//enable = false;
if (Page.Request["Page"] != null || Page.Request["Page"] != "")
{
...
] thank from this site.