Making Particular Control as a Trigger by
Suppressing other Child Controls of UpdatePanel control
There is a property in UpdatePanel
control called ChildrenAsTriggers which accepts Boolean to enable/disable
the child controls as its triggers. We can make this property to false and
specify the child control as an AsyncPostBackTrigger.
<asp:UpdatePanel ID="UpdatePanel4"
runat="server" ChildrenAsTriggers="False"
UpdateMode="Conditional">
<ContentTemplate>
//Child controls go here
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
Note
To have this working, we need to make
the UpdateMode property to Conditional. Setting UpdateMode property to Always
will give the following error.
ChildrenAsTriggers cannot be set to
false when UpdateMode is set to Always on UpdatePanel
'UpdatePanel4'.
Refreshing whole page through a child
control of UpdatePanel
In previous sections, we used
AsyncPostBackTrigger to set a new trigger to UpdatePanels. As the name suggests,
the AsyncPostBackTrigger is used to refresh the UpdatePanel asynchronously. At
times, we will have requirements to refresh the page fully instead of
partially/asynchronously. To do this, we have another trigger to UpdatePanel
controls called PostbackTrigger.
When we are adding a control as a
Trigger to UpdatePanel, we will get an option for adding a new trigger as
PostBackTrigger.
<asp:UpdatePanel ID="UpdatePanel4"
runat="server" ChildrenAsTriggers="False"
UpdateMode="Conditional">
<ContentTemplate>
//Child controls go here
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="ddlEmpType"
EventName="SelectedIndexChanged" />
<asp:PostBackTrigger
ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
In the above code, Button1 will
generate postback that refreshes the whole page.
Note:
You can have a control as an
Asynchronous trigger to one UpdatePanel and PostBack trigger to another
UpdatePanel simultaneously. It will throw the following error!
Control with ID 'btnOut' cannot be
registered through both RegisterAsyncPostBackControl and
RegisterPostBackControl. This can happen if you have conflicting triggers
associated with the target control.
Initiating Postback on UpdatePanel
control at Regular Intervals
Whenever we need to refresh our page in
asp.net we use meta tags. ASP.Net 2.0 introduced a new class called HtmlMeta
that helps in using this functionality in codebehind. ASP.Net AJAX equivalent is
to refresh UpdatePanel control instead of whole page.
To achieve this, there is a control called Timer control
with ASP.Net AJAX which helps us to refresh the UpdatePanel after the specified
times in milliseconds contained in Interval property.
<asp:UpdatePanel ID="UpdatePanel4"
runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button
ID="Button1" runat="server" OnClick="Button1_Click" Text="Button"
/>
<asp:Button
ID="Button2" runat="server" OnClick="Button2_Click" Text="Button"
/>
<asp:Label ID="Label1"
runat="server" Text="Label"></asp:Label>
<asp:Timer ID="Timer1"
runat="server" Interval="1000" OnTick="Timer1_Tick">
</asp:Timer>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
CodeBehind
protected void Timer1_Tick(object sender,
EventArgs e)
{
Label1.Text =
DateTime.Now.ToString();
}
Server Validations with ASP.Net
AJAX
As we all know, not all validations can
be done on client side. Some of the validations should be done in serverside and
these validations are done based on some data or business rules on server side.
For example, we have enabled “Vendor Name” only for temporary employee in
previous example. Suppose, if we want to throw an error message when the user
enters a vendor name who are not in our vendor list then we can include a
CustomValidator with a server side validation function.
ASPX
<asp:TextBox AutoPostBack="true"
ID="txtVendor" runat="server"></asp:TextBox>
<asp:CustomValidator
ID="CustomValidator1" ControlToValidate="txtVendor" runat="server"
ErrorMessage="Vendor Not in List"
OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
CodeBehind
protected void Page_Load(object sender,
EventArgs e)
{
this.Validate();
}
protected void
CustomValidator1_ServerValidate(object source, ServerValidateEventArgs
args)
{
string[] vendors =
{"wipro","tcs","satyam","hcl"};
int index = Array.IndexOf(vendors,
txtVendor.Text);
if (index == -1)
{
args.IsValid =
false;
}
else
{
args.IsValid =
true;
}
}
When executed, the validation function
will be called when the user types in a vendor name and tabs out because we have
specified AutoPostBack="true" for the textbox. This gives an impression
that the validation is client side like other validations. Refer the below
figure.
|