Question

net application. With this application a user can add a guestconnection for a time. for this i have a button that open a modal dialog who a user can add a guest. the input must be a firstname,lastname,company and the time. i use the validation controls and a ValidationGroup. The Validationcontrols check if I forget a input but if I click on "add" the code doesn't run. I try this with a simple div but the same method:

Here is my aspx code:

<div id="add">

                    <div id="Div2" class="popupConfirmation" runat="server" style="width:350px; height:290px;">

                          <div class="bodycontrol">
                            <table>  
                        <tr>
                            <td><asp:Label ID="Label3" runat="server" Text="Vorname"></asp:Label></td>
                            <td><asp:TextBox ID="TextBox1" runat="server" ValidationGroup="valid" ></asp:TextBox></td>
                            <td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="valid" runat="server" ForeColor="red" ErrorMessage="*" ControlToValidate="TextBox1"></asp:RequiredFieldValidator></td>
                        </tr>
                        <tr>
                            <td><asp:Label ID="Label4" runat="server" Text="Nachname"></asp:Label></td>
                            <td><asp:TextBox ID="TextBox2" runat="server" ValidationGroup="valid"></asp:TextBox></td>
                            <td><asp:RequiredFieldValidator ID="RequiredFieldValidator2" ValidationGroup="valid" runat="server" ForeColor="red" ErrorMessage="*" ControlToValidate="TextBox2"></asp:RequiredFieldValidator></td>
                        </tr>
                        <tr>
                            <td><asp:Label ID="Label5" runat="server" Text="Firma"></asp:Label></td>
                            <td><asp:TextBox ID="TextBox3" runat="server" ValidationGroup="valid"></asp:TextBox></td>
                            <td><asp:RequiredFieldValidator ID="RequiredFieldValidator3" ValidationGroup="valid" runat="server" ForeColor="red" ErrorMessage="*" ControlToValidate="TextBox3"></asp:RequiredFieldValidator></td>
                        </tr>
                    </table>
                           <br />
                            <table>
                        <tr>
                            <td><asp:LinkButton ID="LinkButton1" runat="server" class="GuestButtons"  Text="Hinzufügen" ValidationGroup="valid" onclick="btn_GuestListViewAddDialog_YES_Click" ></asp:LinkButton></td>
                            <td><asp:LinkButton ID="LinkButton2" runat="server" class="GuestButtons" Text="Abbrechen" ValidationGroup="never"></asp:LinkButton></td>
                        </tr>
                    </table>
                           <br />
                            <table>
                        <tr>
                            <td><asp:Label ID="Label10" runat="server" Text="*Bitte alle Felder ausfüllen"></asp:Label></td>
                        </tr>
                    </table>
                          </div>

                    </div>          

               </div>

here is my c# code:

   protected void btn_GuestListViewAddDialog_YES_Click(object sender, EventArgs e)
            {
                if (Page.IsValid) //Here i make a breakpoit but it doesn't use this code :( 
                {

    ...//here is my code

}

}

this is in my script block if i click on the linkbutton:

WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$lw_content$LinkButton1", "", true, "valid", "", false, true))
Was it helpful?

Solution

If you want to force validation on serverside you need to call Page.Validate() before you check Page.IsValid.

protected void btn_GuestListViewAddDialog_YES_Click(object sender, EventArgs e)
{
    Page.Validate();
    if (Page.IsValid) 
    {

The LinkButton must also have a ValidationGroup, the same valid-group if you want to validate this group if the link-button was clicked or a different group if you don't want to trigger validation.

Edit: However, Page.Validate should be redundant since CausesValidation is true by default for a LinkButton and you have specified also a ValidationGroup for it. So i'm at a loss.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top