ASP.NET change a Repeater's DataSource without a postback? - asp.net

I have a repeater that displays some data from a SQL query:
<asp:Repeater runat="server" ID="damQuickList" OnItemDataBound="damQuickList_OnItemDataBound">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><asp:HyperLink runat="server" ID="damAnchor" /></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
In the codebehind:
damQuickList.DataSource = (Data.RunSelectQuery("SELECT * FROM Table ORDER BY " + radioButton.Value));
damQuickList.DataBind();
Is there a way to change the DataSource and have it update in the Repeater, without having to do a postback in the page (like how AJAX does it)? I've been using the Async controls I found here: http://www.asynccontrols.com/, but there's some issues using them with IE6/7.

Use the ASP.NET AJAX components. Put a ScriptManager on your page, then put a UpdatePanel on your page. Inside the update panels ContentTemplate put your repeater.
A quick example would look something like this...
ASPX Markup
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td>
<%# Eval("Data") %>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
C# Code Behind
protected void Button1_Click(object sender, EventArgs e)
{
Repeater1.DataSource = yourDataSource;
Repeater1.DataBind();
}
Note the button that 'refreshes' your data source is also inside the content template. Hope it helps.

Off-topic: I hope that's not your real code; I'm pretty sure a malicious user could put anything they wanted into the radioButton.Value field and SQL-Inject you.

Do you mean a full round trip? Can you not put the repeater inside an UpdatePanel?

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.

AsyncPostBackTrigger in nested repeater

Good day everyone,
This is my first post in here and I would like to thank you all for the great efforts in this forum by which I have already gaind a lot of skills.
I have a smalle issue with two nested repeaters. Basically, I have a dropdownlist in a child repeater which contains rating values and every time the dropdownlist is changed in the child repeater the new percentange is calculated and presented in a label in the parent repeater. This will cause full postback which is really frustrating when going through too many dropdownlists. My question is how to reflect the new calculated percentange in the label without postback. I have tried to use AsyncPostBackTriggers but no luck. Any suggestions would be appreiciated
<asp:Repeater ID="rptParent" runat="server">
<ItemTemplate>
<asp:Label ID="lblAvg" runat="server" Text='<%# Eval("TrialScore")%>'></asp:Label>
<asp:Repeater ID="rptChild" runat="server">
<ItemTemplate>
<asp:DropDownList ID="lstRate" runat="server" OnSelectedIndexChanged="lstRate_SelectedIndexChanged" />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Wrap your aspx mark up inside update panel like this.
<asp:UpdatePanel runat="sever" ID="upParentChild" >
<ContentTemplate>
<asp:Repeater ID="rptParent" runat="server">
<ItemTemplate>
<asp:Label ID="lblAvg" runat="server" Text='<%# Eval("TrialScore")%>'></asp:Label>
<asp:Repeater ID="rptChild" runat="server">
<ItemTemplate>
<asp:DropDownList ID="lstRate" runat="server" OnSelectedIndexChanged="lstRate_SelectedIndexChanged" />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
This will make sure that only controls inside update panel are posted back and not the whole page.

Asp.net page still flickers with UpdatePanel

I have a page which contains a Listview,. The ItemTemplate will contain many Checkboxes for each returned value from it's datasource. These Checkboxes has a OnCheckedChanged function and when a user selects the checkbox, the page flickers...I have investigated UpdatePanels, but to no avail. Also, this page contains a MasterPage.
Code:
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:ListView ID="lvTypes" runat="server" DataSourceID="XXX" GroupItemCount="4">
<ItemTemplate>
<td style="background-color: #4b6c9e;" align="left">
<asp:CheckBox ID="Type" runat="server" Text='<%# Eval("Type") %>' ForeColor="White" OnCheckedChanged="chk_CheckedChanged" AutoPostBack="True" CssClass="myCheck" Font-Size="45px" />
</td>
...
...
...
Obviously, I am not grasping the concept of the UpdatePanel. Can you direct me on how to implement this for this scenario"?
Thanks.
If you use UpdateMode = "Conditional" it'll do a partial refresh.
<asp:UpdatePanel ID="UpdatePanel2" UpdateMode = "Conditional" runat="server">
Read MSDN to get an idea of the UpdatePanel control.
And, also have a look at this comprehensive explanation in MSDN

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?

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

Resources