Multiple UpdatePanels and Conditional Validation

The Scenario:  Very often we have more than one UpdatePanel in a page. So when a given UpdatePanel does a postback, we would want all other validations in any other UpdatePanels not to be fired.

The Solution:   Below is an example which uses “ValidationGroup” to acheive this. Each UpdatePanel has it’s own ValidationGroup & ValidationSummary control. In the code behind , I run PageValidate call (which is optional) by passing the relevant ValidationGroup. The Validate method is fired automatically by all controls which have ‘CausesValidation’ property set as true. And by default, the controls have “CausesValidation” property as always true. For more on “PageValidate” please refer this msdn site. http://msdn.microsoft.com/en-us/library/dwzxc386.aspx . Here’s a working example

 

<form id="form1" runat="server">
<asp:ScriptManager ID="scriptManager" runat="server" />
<script language="javascript" type="text/javascript">
</script>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:ValidationSummary ID="Summary1" runat="server" ValidationGroup="ValSumm1"/>
<asp:Label ID="Label1" runat="server" Text="Grp1"></asp:Label>
<asp:TextBox ID="txt1" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="Req1" runat="server" ControlToValidate="txt1" ErrorMessage="Text1 is required." 
Text="*" ValidationGroup="ValSumm1"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn1" runat="server" Text="Submit1" ValidationGroup="ValSumm1" OnClientClick="" onclick="btn1_Click"/>
</ContentTemplate>
</asp:UpdatePanel>
 
 &nbsp;&nbsp;
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label2" runat="server" Text="Grp2"></asp:Label>
<asp:ValidationSummary ID="Summary2" runat="server" ValidationGroup="ValSumm2"/>
<asp:TextBox ID="txt2" runat="server" CausesValidation="false"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt2" ErrorMessage="Text2 is required." 
Text="*" ValidationGroup="ValSumm2"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn2" runat="server" Text="Submit2" ValidationGroup="ValSumm2" OnClientClick="" onclick="btn2_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btn1_Click(object sender, EventArgs e)
    {
        Label1.Text = "";
        Page.Validate("ValSumm1");
        if (!Page.IsValid)
        {
            return;
        }
        Label1.Text = "UpdatePanel1 validated. New Text = "+txt1.Text+"<br>";
    }
    protected void btn2_Click(object sender, EventArgs e)
    {
        Label2.Text = "";
        Page.Validate("ValSumm2");
        if (!Page.IsValid)
        {
            return;
        }
        Label2.Text = "UpdatePanel2 validated. New Text = "+txt2.Text+"<br>";
    }

Advertisements
This entry was posted in General ASP.Net C#. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s