Data Pager + Update Panel + list view - asp.net

I need help, I have a DataPager inside the update panel and and working well, but when I refresh page or going on back button form some other page DataPager reset always to the first page.
For example if I was on the tenth page and refresh page, data pager reset to first.
Is there any way to keep the current page on data pager?
My code is:
<asp:SqlDataSource ID="SqlDataSource1" EnableCaching="True" CacheDuration="4000" CacheExpirationPolicy="Sliding"
runat="server" ConnectionString="<%$ ConnectionStrings:blabla %>" SelectCommand=""></asp:SqlDataSource>
<asp:UpdatePanel ID="UpdatePanelnajgornji" runat="server" >
<ContentTemplate>
<asp:ListView ID="ListView1" runat="server" ondatabound="ListView1_DataBound" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:HyperLink ID="HyperLink2" Visible='<%# !(Eval("Slika") is DBNull) %>' runat="server" NavigateUrl='<%# Eval("Link") %>' title='<%# Eval("Naslov") %>'>
<asp:Image ID="Image1" runat="server" AlternateText='<%# Eval("Naslov") %>' ImageUrl='<%# Eval("Slika") %>' /></asp:HyperLink>
</ItemTemplate>
</asp:ListView>
<br />
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1">
<Fields>
<asp:NextPreviousPagerField />
</Fields>
</asp:DataPager>
</ContentTemplate>
</asp:UpdatePanel>

Browser refresh creates new objects (and thus clears the state of the DataPager). You need to save the current page either in the Session or the ViewState, then initialize the control using those values if they exist in the codebehind. Store the values in an unload event listener.

Related

Access model bound item inside nested UpdatePanel

What is the correct method to access the data-bound item inside of a nested UpdatePanel? I've recently discovered model binding and love the strongly-typed binding it offers, but I have a scattering of UpdatePanels throughout the page, and it seems to break access to the Item object. If the entire databound control is inside the UpdatePanel I have no problem. This works:
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Repeater runat="server" ID="myRepeater" ItemType="Test.Person">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Item.Name %>' /><br />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
But if the UpdatePanel is inside the bound control, I no longer have access to Item. This does not work:
<asp:Repeater runat="server" ID="myRepeater" ItemType="Test.Person">
<ItemTemplate>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Label runat="server" Text='<%# Item.Name %>' /><br /> // <-- Problem line
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:Repeater>
CS0103: The name 'Item' does not exist in the current context
I fumbled my way onto a clunky solution, which is this
((Person)GetDataItem()).Name
But I desperately hope this is not the best way of accomplishing the task.

Gridview TemplateField checkbox postback behavior in asp.net

I have GridView inside UpdatePanel and UpdateMode of the UpdatePanel is set to conditional.
Gridview contains asp:CheckBox as TemplateField and rest of the columns are boundfields which are dynamically created.
Checbox AutoPostBack is set to true and I update a datatable (which is inside session) based on checkbox value.
Here is markup:
<asp:GridView ID="ObjList" runat="server" CssClass="ObjList" AutoGenerateColumns="false" OnRowDataBound="ObjList_RowDataBound" AutoGenerateSelectButton="false" AllowPaging="False">
<Columns>
<asp:TemplateField HeaderText="&nbsp">
<HeaderTemplate>
<asp:CheckBox AutoPostBack="true" ID="chkAll" runat="server" OnCheckedChanged="HeaderChk_Changed" />
<asp:HiddenField ID="LinkNumIndexHead" runat="server" Value="-1" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox AutoPostBack="true" ID="chkRow" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "Selection")%>'
OnCheckedChanged="ChkRow_OnCheckChange" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In Deployed version only:
Whenever user click 2 or more checkboxes in fast speed. Postback of first checkbox fires and rest of checkboxes get unchecked. How can I control this behavior?
When Local IIS is running:
Postback of every checkbox fires.
In Firebug debugging it is noticed that Postback of first checkbox takes quite a time.
Please tell me how can I avoid this situation.
Try this
<asp:GridView ID="ObjList" runat="server" CssClass="ObjList" AutoGenerateColumns="false"
OnRowDataBound="ObjList_RowDataBound" AutoGenerateSelectButton="false" AllowPaging="False">
<Columns>
<asp:TemplateField HeaderText="&nbsp">
<HeaderTemplate>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:CheckBox AutoPostBack="true" ID="chkAll" runat="server" OnCheckedChanged="HeaderChk_Changed" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:HiddenField ID="LinkNumIndexHead" runat="server" Value="-1" />
</HeaderTemplate>
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel5" runat="server">
<ContentTemplate>
<asp:CheckBox AutoPostBack="true" ID="chkRow" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "Selection")%>'
OnCheckedChanged="ChkRow_OnCheckChange" />
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Image Button does a Complete post-back on every click.

Is there anyway i can prevent a ImageButton control to avoid complete post back at the same time trying to fetch the some information on button click ?
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Literal ID="litRowStart" runat="server"></asp:Literal>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
<td >
<ul>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl='<%#String.Format(Eval("Image_Location")+".jpg") %>' CssClass="thumbnail" ToolTip = '<%# Eval("Team_Name")%>' />
<asp:Label ID="Label2" runat="server" Font-Bold="True" Text='<%# Eval("serno")%>' Visible="false"/>
</ContentTemplate>
</asp:UpdatePanel>
</ul>
</td>
<asp:Literal ID="Literal2" runat="server"></asp:Literal>
<asp:Literal ID="litRowEnd" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
On the backend i am doing :
Label1.Text = ((Label)e.Item.FindControl("Team_Id")).Text;
But every time I Click the image button it does a post back instead of a Asynchronous one.
There is a lot of data on the page which is depending on the Serno.
Can someone please suggest me a solution or an alternative to Image Button which will perform the same functionality with a button click.
Thank You !
You can use this code that can detect which control did post back and correct your code:
string ctrlName = Page.Request.Params.Get("__EVENTTARGET");
if (!String.IsNullOrEmpty(ctrlName))
ctrl = FindControl(ctrlName);
if (IsPostBack && ctrl!=ImageButton2)
{
//your code that will execute in postback
}
This should work. Can you make sure you have no js error and HTML is valid on the page?

ImageButton with PostBackUrl responds after the second click issue

Hello i have a problem with my Imagebutton i have to double click just to get it to post any one know how i can fix it so i only have to click it once to get a response?
thank you for your help!
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:smokeshopConnectionString %>"
ProviderName="<%$ ConnectionStrings:smokeshopConnectionString.ProviderName %>"
SelectCommand="SELECT ProductID, Name, Description, Price, ImageUrl FROM products"></asp:SqlDataSource>
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1"
RepeatDirection="Horizontal"
DataKeyField="ProductID" >
<ItemTemplate>
<div style="border-style:dotted; border-color:WhiteSmoke;">
<a onclick="popup('popUpDiv')" href="#" ><asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# Eval("ImageUrl", "Images\\{0}") %>' PostBackUrl='<%# Eval("ProductID", "Default5.aspx?ProductID={0}") %>' /></a><br />
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>'></asp:Label><br />
</div>
</ItemTemplate>
</asp:DataList>
I have test code from side ..its works fine ..I got image button event in single click. I think problem is with javascript function written over imagebutton i.e.
popup('popUpDiv')
Can you please explain javascript function "popup('popUpDiv')" .. or you can test it with remove anchor link tag from image button..

Selecting items of a Listview using checkboxes in ASP .NET

I am trying to use checkboxes to select the items in a Listview. I have added a checkbox control in the , and they are displayed properly.
The problem is that Checked property never changes when I click on them. Why does this happen? And is there a workaround?
Here is the code:
<asp:ListView ID="ListView1" runat="server"
onitemcommand="ListView1_ItemCommand"
onitemdatabound="ListView1_ItemDataBound">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBoxSelect" runat="server" OnCheckedChanged="CheckBoxSelect_checkchanged"/>
<asp:LinkButton ID="LinkButtonOpen" CommandArgument='<%#Eval("MessageID") %>' runat="server">
<asp:Label ID="Label1" Text="[]" runat="server"/>
<%#Eval("FirstName" )%> <%#Eval("LastName")%>
<%#Eval("Subject") %>
<%#Eval("Timestamp") %>
<asp:HiddenField runat="server" ID="ReadStatus" Value='<%#Eval("IsRead") %>' />
</asp:LinkButton>
</ItemTemplate>
</asp:ListView>
I think you are missing the AutoPostBack="true"
<asp:CheckBox ID="CheckBoxSelect" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBoxSelect_checkchanged"/>
The problem was that I was binding the ListView during page load. When that happened, the checkboxes would get cleared, and I got the Checked property as False in all subsequent functions.

Resources