CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Tips and Tricks - ASP.Net AJAX Applications

By Satheesh babu
Posted On Sep 08,2008
Article Rating:
Be first to rate
this article.
No of Comments: 4
Category: ASP.Net AJAX
Print this article.

Tips and Tricks - ASP.Net AJAX Applications

 

Introduction

My previous articles on ASP.Net AJAX helped you to understand the basics of implementing AJAX applications in ASP.Net. This article is aimed to provide some tips and tricks that are very useful while implementing ASP.Net AJAX applications. Moving forward, we will see about,

Ø       Setting External Triggers to UpdatePanel

Ø       Setting External Control’s Event as a Trigger to UpdatePanel

Ø       Making Particular Control as a Trigger by Suppressing other Child Controls of UpdatePanel control

Ø       Refreshing whole page through a child control of UpdatePanel

Ø       Initiating Postback on UpdatePanel control at Regular Intervals

Ø       Server Validations with ASP.Net AJAX

Setting External Triggers to UpdatePanel

By default, the postback caused by the child controls of an UpdatePanel control will update/Refresh the content of update panel. For example, if we have a button called “btnSave” external to the UpdatePanel control which should refresh the UpdatePanel. To set this button(btnSave) as a trigger to the UpdatePanel control, go to properties of UpdatePanel and click the button(…) seen in Triggers. Refer the below figure,

 

 

Click Add and select the ControlID as btnSave.

 

 

This will make the btnSave which resides in a different UpdatePanel as a trigger to the GridView’s UpdatePanel.

 

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">

<ContentTemplate>

//Contents go here

 </ContentTemplate>

        <Triggers>

               <asp:AsyncPostBackTrigger ControlID="btnSave" />                           

        </Triggers>

</asp:UpdatePanel>

 

The above example will set the control btnSave as the trigger of UpdatePanel control and it will refresh the UpdatePanel’s content for every postback caused by the control btnSave.

 

Setting External Control’s Event as a Trigger to UpdatePanel

Suppose, if we have a control external to an UpdatePanel control, say a DropDownList. To refresh the content of the UpdatePanel control for a particular server side event(say, SelectedIndexChanged) happened on the DropDownList, we can use AsyncPostBackTrigger trigger.

 

<asp:DropDownList ID="ddlEmpType" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlEmpType_SelectedIndexChanged">

                     <asp:ListItem>Permanent</asp:ListItem>

                     <asp:ListItem>Temporary</asp:ListItem>

</asp:DropDownList>

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">

<ContentTemplate>

                    <table>

                        <tr>

                            <td style="width: 100px">

                            Name

                            </td>

                            <td style="width: 100px">

                                <asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox></td>

                        </tr>

                        <tr>

                            <td style="width: 100px">

                            Department

                            </td>

                            <td style="width: 100px">

                                <asp:TextBox ID="txtdept" runat="server"></asp:TextBox></td>

                        </tr>

                        <tr>

                            <td style="width: 100px">

                            Age

                            </td>

                            <td style="width: 100px">

                                <asp:TextBox ID="txtAge" runat="server"></asp:TextBox></td>

                        </tr>

                        <tr>

                            <td style="width: 100px">

                            Address

                            </td>

                            <td style="width: 100px">

                                <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td>

                        </tr>

                        <tr>

                            <td colspan="2">

                                <asp:Panel ID="Panel1" runat="server" Height="50px" Width="100%">

                                <table><tr><td>Vendor Name:</td><td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td></tr></table></asp:Panel>

                            </td>

                        </tr>

                    </table>

            <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" /> </ContentTemplate>

      <Triggers>

                <asp:AsyncPostBackTrigger ControlID="ddlEmpType"

           EventName="SelectedIndexChanged" />

  </Triggers>

</asp:UpdatePanel>

 

CodeBehind

public void CheckEmployeeType()

    {

        if (ddlEmpType.SelectedValue == "Temporary")

        {

            Panel1.Visible = true;

        }

        else

        {

            Panel1.Visible = false;

        }

    }

    protected void ddlEmpType_SelectedIndexChanged(object sender, EventArgs e)

    {

        CheckEmployeeType();

    }

 

The above code will refreshes the content of UpdatePanel1 for every selected index changed event on the DropDownList. Hence, the Panel1 which is inside the UpdatePanel1 will be displayed only if the employee type is Temporary. Refer the below figure for better understanding.

 

For temporary employee, it displays “Vendor Name” to enter the parent company(vendor) name. For Permanent employee, it is not required. The enabling/disabling of vendor name is taken care in SelectedIndexChange event.

 




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.

 

 

Conclusion

Thus, we have understood some of the useful tips that can be used in our ASP.Net AJAX projects. There are so many little functionalities that can be very useful and can be reused in many places in our projects and this article lists some of such useful tasks in AJAX applications. Thanks for reading my article!

Happy Coding!!!

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
ajaxtips
Great Stuff
Tips and Tricks - ASP.Net AJAX Applications
very gud information mentioned..
Too much work
You are working too hard. Just use JQuery
Nice Article
Nice Article. It would have helped if you specified how to work with UpdatePanels programmatically. Probably a new article.