Page still refreshes after wrapping the repeater in an update panel
I've two SqlDataSources and two Repeaters, each repeater contains one hyperlink (i also tried using web server button and anchors).
The hyperlinks fetch from the database some values and in the NavigationUrl property I use a string.Format
method to create a parameterized url, to pass for the browser, then second repeater is populated according to the value passed in the url which is originally passed by the first repeater's hyperlink
this is my sample code : https://gist.github.com/726213
<asp:ScriptManager id="Scrptmanagr" runat="server"></asp:ScriptManager>
<asp:UpdatePanel id="updtpanl" runat="server">
<ContentTemplate>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [arrange_by_id], [arrange_by] FROM [arrange_by]">
</asp:SqlDataSource>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:HyperLink ID="HyperLink3" NavigateUrl='<%# string.Format("{0}?SortingType={1}",Request.AppRelativeCurrentExecutionFilePath, Eval("arrange_by_id"))%>' runat="server"><%# Eval("arrange_by") %></asp:HyperLink>
</ItemTemplate>
<SeparatorTemplate>
|
</SeparatorTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource2" runat="server开发者_如何学Go" ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [alphabet_id],[arrange_by_id], [value] FROM [alphabet] WHERE ([arrange_by_id] = @arrange_by_id)">
<SelectParameters>
<asp:QueryStringParameter Name="arrange_by_id" QueryStringField="SortingType" Type="Int32" DefaultValue="1" />
</SelectParameters>
</asp:SqlDataSource>
<br /><br />
<asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource2">
<ItemTemplate>
<asp:HyperLink ID="hyper1" runat="server" NavigateUrl='<%#string.Format("{0}?SortingType={1}&SortBy={2}",Request.AppRelativeCurrentExecutionFilePath, Eval("arrange_by_id"),Eval("value"))%>'><%# Eval("value")%></asp:HyperLink>
</ItemTemplate>
<SeparatorTemplate>
|
</SeparatorTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
Now! everytime I click any of the hyperlinks it causes a full post back and refreshes the page! Am I missing something ?
Pretty sure an <asp:HyperLink>
isn't going to get you a partial update, it renders to HTML as an <a href="..">
tag. You'll need a control that actually causes a postback, an <asp:Button>
or <asp:LinkButton>
.
Firstly any value you want to pass back don't use query strings, its the same page. Place the content in hidden field or the buttons CommandArgument
<asp:HiddenField ID="hdnFieldName" Value='<%# Eval("columnName") %>' runat="server" />
then on the comand behind
protected void rptName_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if(e.CommandName.Equals("ButtonCommandName"))
{
RepeaterItem objItem = e.Item;
var objFieldValue = (HiddenField)objItem.FindControl("hdnFieldName");
}
}
And remember to set the update panel Mode="conditional' this will cause the updatepanel to only update when one of the following occurs:
1) If a control within the updatepanel causes a postback e.g. asp.net button.
2) If a trigger on the updatepanel occurs (about triggers: http://www.asp.net/web-forms/tutorials/aspnet-ajax/understanding-asp-net-ajax-updatepanel-triggers)
3) Finally if the "Update()" method is called
Otherwise, it won't update and refresh. When its set to always, any postback outside the updatepanel or another updatepanel can trigger the updatepanel to refresh.
精彩评论