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

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?

Related

Asyn. LinkButton with controller

I have a updatePanel with a LinkButton and in the LinkButton are some Controller.
<asp:UpdatePanel ID="UpdateRepTrack" runat="server">
<ContentTemplate>
<asp:UpdateProgress ID="updateProgress" runat="server" AssociatedUpdatePanelID="UpdateRepTrack" DynamicLayout="true">
....
</asp:UpdateProgress>
<ItemTemplate>
....
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server">
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
....
</ItemTemplate>
</ContentTemplate>
</asp:UpdatePanel>
Now when I click on the Text(Label) the page do a Full Postback, but when I click next to the Text(on the Link directly) then the page do an asynchronous postback.
Can me help someone with a solution so that I get asynchronous postback everywhere.
Thanks.
Solution I have tried
I have tried to add this code behinde
With CType(e.Item.FindControl("LinkButton1"), LinkButton)
Dim trigger As New AsyncPostBackTrigger
trigger.ControlID = .UniqueID
UpdateRepTrack.Triggers.Add(trigger)
End With
I have found a solution. When you add the "clientIDMode" attribute to the linkbutton it works fine.
<asp:LinkButton ID="LinkButton1" clientIDMode="AutoID" runat="server">
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</asp:LinkButton>
I have tried a little round with a clear page. Then I have removed the repeater and it works. After this I found this page
Leonid's space and this was the solution.

Data Pager + Update Panel + list view

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.

UpdatePanel causing full postback. Trying to do a partial postback

I am working on a page that is using a gridview to display data. I have some dynamically created textboxes inside an ItemTemplate which contains several textboxes per row. Now I also have an update panel that is using ajax and should only render once my link button is clicked. The datalist is bound in my code behind after the I would like this to occur without causing a full postback. However, right now when I click the link button it causes a full post-back which eliminates my dynamically created controls.
I feel I am very close to a solution. I need one of these to happen (option 1 seems more useful):
Do not cause a postback when the linkbutton is clicked, but still render my full datalist in the update panel
or
my dynamically created controls are not removed during post back.
Here is my code:
<ItemTemplate>
[ <asp:LinkButton ID="SelectCommand" CommandName="Select" runat="server" Text="+" CssClass="sunocoBold"/> ]
<%-- start sub panel--%>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Vertical"
OnItemCommand="DataList_OnItemCommand">
<ItemTemplate>
<asp:LinkButton ID="Select" CommandName="SelectCommand" CommandArgument='<%#Eval("ship_to_num")%>' runat="server" Text='<%#Eval("ship_to_num")%>' />
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
<%-- end sub panel--%>
</ItemTemplate>
<asp:TemplateField HeaderText="Site Owner" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:Panel ID="pnlNames" runat="server">
</asp:Panel>
<%-- <asp:Literal ID="NameList" runat="server" /> --%>
</ItemTemplate>
</asp:TemplateField>
UpdatePanel.Triggers is made for this!
Take a look at it here: Understanding UpdatePanel.Triggers

Make update panel component change visibility of item outside of the update panel

I have a repeater and the items are editable through an edit button which opens a FormView in edit mode. The formView is initially invisible and the repeater visible. Once edit is pressed the repeater goes invisible then the formview becomes visible with the item to edit.
Once changes have been made the user presses update. This sets the formview invisible and the repeater visible.
The problem is the formview goes invisible but the repeater doesn't become visible. This I think is caused by the fact the formview is within an update panel and the repeater isn't? Only the items in the update panel are being altered on clicking update because it is only a partial page update.
I can't put the repeater within the update panel because there is a requirement that the public view doesn't use javascript.
Does anyone know how I could make the repeater reappear?
<asp:Repeater id="resultsRepeater" runat="server" DataSourceID="vehiclesDataSource" >
<ItemTemplate>
<asp:Label id="makeLabel" runat="server" Text='<%# Eval("Make") %>' />
<asp:Button id="editButton" runat="server" Text="Edit" CommandArgument='<%# Eval("Id") %>' OnClick="EditButton_Click" />
</ItemTemplate>
<asp:Repeater>
<asp:UpdatePanel ID="updatePanel" runat="server">
<ContentTemplate>
<asp:Panel id="insertUpdatePanel" runat="server" Visible="false">
<asp:FormView id="editformview" runat="server" DataKeyNames="Id" Datasourceid="VehiclesEditDataSource" >
<EditItemTemplate>
<uc:VehiclesEdit ID="VehiclesEdit" runat="server" />
<asp:Button id="updateButton" runat="server" OnClick="Update_Click" />
</EditItemTemplate>
</asp:FormView>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
protected void EditButton_Click(object sender, EventArgs e)
{
resultsRepeater.Visible = false;
insertUpdatePanel.Visible = true;
}
protected void Update_Click(object sender, EventArgs e)
{
resultsRepeater.Visible = true;
insertUpdatePanel.Visible = false;
}
This might help. I had a similar problem and this worked for me. I simply used ScriptManager to register the button (even iterated by row) as a postback control like this:
ScriptManager.GetCurrent(Page).RegisterPostBackControl(updateButton)
This caused a full postback and allowed me to set the visibility of a panel outside the update panel. I hope it works!
REVISED: Add a PostBackTrigger to your UpdatePanel to force a full post-back when your UpdateButton is clicked. This will hide your UpdatePanel and reveal your Repeater again. See final code below:
For more info refer to: https://stackoverflow.com/questions/2545508/how-do-i-force-full-post-back-from-a-button-within-an-updatepanel
<asp:UpdatePanel ID="updatePanel1" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="updateButton" />
</Triggers>
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" Visible="false">
<asp:FormView ID="editformview" runat="server" DataKeyNames="Id" DataSourceID="VehiclesEditDataSource">
<EditItemTemplate>
<uc:vehiclesedit id="VehiclesEdit" runat="server" />
</EditItemTemplate>
</asp:FormView>
<asp:Button ID="updateButton" runat="server" OnClick="Update_Click" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>

Listview inside UserControl raises full postback

I have UserControl and within that control i have asp:ListView. Inside the ListView i have a asp:LinkButton. When i click on the LinkButton the control raises full postback, no matter if the UserControl is inside UpdatePanel or is not.
UserControl:
<asp:ListView ID="lvImages" runat="server" OnItemCommand="lvImages_ItemCommand">
<ItemTemplate>
<div>
<asp:Image runat="server" ID="imgImageThumb" ImageUrl='<%#Eval("Image") %>' GenerateEmptyAlternateText="true" />
<asp:LinkButton runat="server" ID="lbtnImageAdd" CommandName="Add" CommandArgument='<%#Container.DisplayIndex %>'
CausesValidation="false" Text="Add" />
</div>
<ItemTemplate>
</asp:ListView>
Page:
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<cuc:UserControl ID="cucUserControl" runat=server/>
</ContentTemplate>
</asp:UpdatePanel>
You need to set the properties to let the update panel know what to trigger off of. Try setting ChildrenAsTrigger=true.

Resources