ASP.NET When using updatepanel should the page refresh? - asp.net

I have follow two listboxes and and a left and right button.. which are contained in a updatepanel. The only problem is when clicking on the left or right button the page refreshes which is not desired.
<asp:UpdatePanel runat="server" ID="ExportUpdatePanel">
<ContentTemplate>
<div class="exportWrapper">
<table class="exportFilter">
<tr>
<td>
<h2>
<%= ExportSelectDateLabel %></h2>
</td>
</tr>
<tr>
<td>
<label>
<%= ExportFromDateLabel %></label>
<asp:TextBox runat="server" ID="exportFilterFromDate" CssClass="exportDates"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<label>
<%= ExportToDateLabel %></label>
<asp:TextBox runat="server" ID="exportFilterToDate" CssClass="exportDates"></asp:TextBox>
</td>
</tr>
</table>
<table class="exportFilter">
<tr>
<td colspan="3">
<h2>
<%= ExportSelectColumnsLabel %></h2>
</td>
</tr>
<tr>
<td>
<label>
<%= ExportAvailableColumnLabel %></label>
</td>
<td>
</td>
<td>
<label>
<%= ExportSelectedColumnLabel %></label>
</td>
</tr>
<tr>
<td>
<asp:ListBox runat="server" ID="exportFilterAvailableColumns" SelectionMode="Multiple" CssClass="exportListBox">
</asp:ListBox>
</td>
<td class="exportButtonsTd">
<div>
<asp:LinkButton runat="server" OnClick="MoveSelectedClick"><span><img src="/images/source/arrow-right.png" alt="Move Right"/></span></asp:LinkButton>
</div>
<div class="mt_10">
<asp:LinkButton runat="server" OnClick="RemoveSelectedClick"><span><img src="/images/source/arrow-left.png" alt="Move Left"/></span></asp:LinkButton>
</div>
</td>
<td id="exportedSelectedColumn">
<asp:ListBox runat="server" ID="exportFilterSelectedColumns" SelectionMode="Multiple" CssClass="exportListBox">
</asp:ListBox>
</td>
</tr>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
Any ideas ?

As per your code ExportUpdatePanel section will refresh when you click on link button inside UpdatePanel. it is not full page refresh. default update mode of update panel is always. that means:
the UpdatePanel control's content is updated on every postback that originates from anywhere on the page. This includes asynchronous postbacks from controls that are inside other UpdatePanel controls and postbacks from controls that are not inside UpdatePanel controls.
here is sample test:
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
On click event both Label1 and Label2 will update.
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToLongTimeString();
Label2.Text = DateTime.Now.ToLongTimeString();
}
but only Label2 will change because it will refresh by the update panel.

Before using update panels, please read some article on how update panels work. For example, this one: http://www.asp.net/web-forms/tutorials/aspnet-ajax/understanding-asp-net-ajax-updatepanel-triggers
Add UpdateMode="Conditional" and ChildrenAsTriggers="true" attributes to your update panel, and it should work as you want.
<asp:UpdatePanel runat="server"
ID="ExportUpdatePanel"
UpdateMode="Conditional"
ChildrenAsTriggers="true">

Related

button cancel doesn't work before I fill all the field (asp.net)

After fill all the fields , submit button doesn't work and cancel also do nothing.
This function in my _.aspx.cs code:
protected void cancel_Click1(object sender, EventArgs e)
{
Response.Redirect("Home.aspx");}
and this is my aspx code
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="DonorSignUp.aspx.cs"> Inherits="BloodBank.DonorSignUp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
Donor Sign Up Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="contentBody" runat="server">
<
<h1> DONOR REGISTRATION<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStr %>" SelectCommand="SELECT * FROM [donor_table]"></asp:SqlDataSource>
</h1>
<table>
<tr>
<td> First Name </td>
<td> <asp:TextBox ID="name" runat="server" Width="139px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" ControlToValidate="name" ErrorMessage="*"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Last Name</td>
<td> <asp:TextBox ID="last" runat="server" Width="140px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="last" ErrorMessage="*"></asp:RequiredFieldValidator>
</td>
</tr>
<td>Username</td>
<td><asp:TextBox ID="username" runat="server" Width="176px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="username" ErrorMessage="Please enter the usename"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Password</td>
<td><asp:TextBox ID="password" runat="server" Width="170px" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="password" ErrorMessage="password"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Confirm</td>
<td> <asp:TextBox ID="confirm" runat="server" Width="155px" TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="password" ControlToValidate="confirm" ErrorMessage="password doesn't match"></asp:CompareValidator>
</td>
</tr>
<tr>
<td colspan="2"><asp:Label ID="msgDSignUp" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="Label2" runat="server" Text="Fill all the blanks"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button ID="SignUpDonor" runat="server" Text="Submit" BackColor="#ff99cc" BorderColor="#ff99cc" OnClick="SignUpDonor_Click1" />
</td>
<td>
<asp:Button ID="cancel" runat="server" Text="Cancel" OnClick="cancel_Click1"/>
</td>
</tr>
</table>
</asp:Content>
When I press cancel button , it does nothing unless I fill all the field in the page ,, if I filled them and press cancel it does the code inside cancel_Click
Can you help me to manipulate this?
This is correct behavior. If you want to test it by going straight to the Cancel event handler, then just comment out all the validators or disable client-side validation temporarily using CausesValidation="false". Validation can occur on both client and server - always best to use both.
Check out this question.

Strange UpdatePanel and Extender control error

I have the below markup in a master page, and when I access a page using that master, I get the error:
An extender can't be in a different UpdatePanel than the control it
extends.
<asp:UpdatePanel ID="UpdatePanel99" runat="server">
<ContentTemplate>
<script type="text/javascript">
function OnAutoCompleteSelected(source, eventArgs) {
$("#autoCompleteHidden").val(eventArgs._text);
__doPostBack("autoCompleteHidden", "");
}
</script>
<table>
<tr>
<td style="height: 25px">Search By
</td>
<td>
<asp:DropDownList ID="ddlSearchBy" runat="server" OnSelectedIndexChanged="ddlSearchBy_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem>Emp No</asp:ListItem>
<asp:ListItem>ID No</asp:ListItem>
<asp:ListItem>Surname</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:ImageButton ID="imgBtnSearch" runat="server" Height="20px" ImageUrl="~/Images/Apps/mySearch2.png"
Width="20px" OnClick="imgBtnSearch_Click" />
</td>
</tr>
<tr>
<td colspan="3" style="height: 25px; margin-left: 80px;">
<div>
<asp:HiddenField runat="server" ID="autoCompleteHidden" OnValueChanged="autoCompleteHidden_ValueChanged" />
<asp:TextBox ID="txtSearchField" runat="server" Width="200px"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server" TargetControlID="txtSearchField"
ServicePath="~/AutoComplete.asmx" ServiceMethod="GetEmployees" Enabled="False" CompletionInterval="100"
MinimumPrefixLength="1" CompletionSetCount="10000" OnClientItemSelected="OnAutoCompleteSelected"
CompletionListCssClass="CompletionList" CompletionListItemCssClass="CompletionListItem" CompletionListHighlightedItemCssClass="CompletionListHighlightedItem">
</ajaxToolkit:AutoCompleteExtender>
</div>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
There is only one UpdatePanel on the master page, and the extended control, txtSearchField is contained well within it only. What am I doing wrong?
I found another AutoCompleteExtender for the same textbox outside of the UpdatePanel.

asp.net ajax Partial rendering not working

I'm trying to do some ajax code but am failing miserably,the code below works
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<tr>
<td>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="This is a label!"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Click Me" OnClick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
But if i was to change the code like below,partial rendering of the page doesn't work
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="This is a label!"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Click Me" OnClick="Button1_Click" />
</td>
</tr>
</ContentTemplate>
</asp:UpdatePanel>
Could someone please look into the code and tell me if im doing something wrong
UpdatePanel generates a div. So put you table inside and it should be fine
OK Code #1
<tr>
<td>
<div />
</td>
</tr>
Wrong Code #2
<table>
<div>
<tr>
<td>
</td>
</tr>
</div>
</table>
What you should do (put the whole table inside the UpdatePanel)
<div>
<table>
<tr>
<td>
</td>
</tr>
</table>
</div>

Couldn't find updatepanel inside hidden div

I have an Updatepanel inside hidden Div, but I am getting "Could not find UpdatePanel with ID 'ctl00_ContentPlaceHolder_ctl04_UpdatePanel1'. I am trying to show and hide this panel from code behind. Thanks for any help. Here is my code.
<div id="div1" runat="server" style="display:none">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table id="Table1">
<tr>
<td>
<telerik:RadComboBox ID="RadComboBox1" runat="server"
AutoPostBack="true" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged">
</telerik:RadComboBox>
</td>
<td valign="top">
<asp:Button ID="button1" runat="server" OnClick="button1_Click" Text="Test1"
/><br />
<asp:Button ID="button2" runat="server" OnClick="button2_Click" Text="Test2"
/>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
you can only manipulate the the code inside the ContentTemplate from the updatepanel's postbacks, so the "div1"s display property can't be changed by the updatepanel's postbacks.
you could move the div inside the updatepanel and then it should work
I'd try this:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel runat="server" id="div1" runat="server" style="display:none">
<table id="Table1">
<tr>
<td>
<telerik:RadComboBox ID="RadComboBox1" runat="server"
AutoPostBack="true" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged">
</telerik:RadComboBox>
</td>
<td valign="top">
<asp:Button ID="button1" runat="server" OnClick="button1_Click" Text="Test1"
/><br />
<asp:Button ID="button2" runat="server" OnClick="button2_Click" Text="Test2"
/>
</td>
</tr>
</table>
<asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
Keep in mind the client id of div1 will no longer be div1 (unless your using .net 4 in which case you can specify that it is'div1') you can fix this a variety of ways but basically you just need to add a little bit of javascript to the page that associates the actual WebControl.ClientID with 'div1'.

asp.net model popup extender

I am using ajax model popup extender in my asp.net page. From my page , on click of Save button , i popup panel using model popup extender.
If i cause postback from model popup , my backend (asp.net) form controls are coming front (ie) in the panel.
Here is my code snippet,
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="up_Save" runat="server">
<ContentTemplate>
<div>
<table border="0" cellpadding="2" cellspacing="2" width="100%">
<tr>
<td align="center" colspan="4">
<asp:Button ID="btn_Save" runat="server" Text="Save" Width="15%" OnClick="btn_Save_Click" />
</td>
</tr>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<cc1:ModalPopupExtender ID="BarrierRelayPopup" runat="server" PopupControlID="PopUp_Panel" TargetControlID="hiddenfield" CancelControlID="btn_Cancel_PopUp">
</cc1:ModalPopupExtender>
<asp:HiddenField ID="hiddenfield" runat="server" />
<asp:Panel runat="server" ID="PopUp_Panel" Width="50%" Height="30%" BackColor="Gainsboro" BorderStyle="Groove" >
<asp:UpdatePanel runat="server" ID="PopUp">
<ContentTemplate>
<table>
<tr>
<td align="left">
<asp:Label ID="lbl_Heading" runat="server" Text="Select Relay and Barrier :"></asp:Label>
</td>
</tr>
<tr>
<td>
<B_R:Barrier_Relay ID="Barrier_Relay" runat="server"/>
</td>
</tr>
<tr>
<td align="center">
<asp:Button ID="btn_OK" runat="server" Width="60px" Text="OK" OnClick="btn_OK_Click" /> <asp:Button ID="btn_Cancel_PopUp" runat="server" Width="60px" Text="Cancel" OnClick="btn_Cancel_PopUp_Click"/>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</form>
Can anybody help me in this.
If I understand you right I think what you want to do is wrap the controls inside the modal in an updatepanel. This will prevent the modal from closing if you need to do a postback from within the popup.

Resources