locked
button onclick event no working RRS feed

  • Question

  • User-1456577918 posted

    I have a page with various text fields dropdowns  with required field validator and custom validator. Some fields are shown dynamically on index change so are the validator.

    problem is when button property causes validation is set to true the onclick event of button is not triggered, but if set to false it is done.

     

    Thanks for the help

    Friday, April 27, 2012 3:07 AM

All replies

  • User1320101480 posted

    You can set the ValidationGroup property of all controls which take part into validation as one validationgroup.

    Friday, April 27, 2012 3:16 AM
  • User-68639941 posted

    hi, as you have mentioned that you are using some required field validator and custom validator , if anu of the validation fails then button onclick will not fire.

    if you want to fire button event irrespective of validation controls then set button's CausesValidation="false"

    or else to do partial valdation you can set ValdiationGroup

    Friday, April 27, 2012 3:17 AM
  • User-1456577918 posted

    tried both the solution but still no success.

    Friday, April 27, 2012 4:47 AM
  • User1320101480 posted

    Did you set ValidationGroup property for dynamically generated controls as well?

    If not, do that as well and give another ValidationGroup to the Button which is causing the problem.

    Friday, April 27, 2012 5:07 AM
  • User-1456577918 posted

    I have assigined validation group for specific control and check the same is working fine.

    But some textboxes are displayed on indexchange method of dropdown. This controls are not validated onclick event.

    Friday, April 27, 2012 5:08 AM
  • User1320101480 posted

    I didnt get you on this point.

    If you dont want a postback on selection of a item in dropdownlist, use autopostback=false.

    Friday, April 27, 2012 5:11 AM
  • User-1456577918 posted

    i have wrote code in codebehind for onSelectedIndexChanged so i have to use AutoPostBack=true

    for e.g

    <asp:DropDownList ID="ddlpurpose1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlpurpose1_SelectedIndexChanged">
                                                    <asp:ListItem Value="Select"></asp:ListItem>
                                                    <asp:ListItem Value="Savings">Savings</asp:ListItem>
                                                    <asp:ListItem Value="Investment">Investment</asp:ListItem>
                                                    <asp:ListItem Value="Others">Others Please Specify</asp:ListItem>
                                                </asp:DropDownList>
                                                <br />
                                                <asp:RequiredFieldValidator runat="server" ID="rfvpurpose1" ControlToValidate="ddlpurpose1" ErrorMessage="Please select Purpose" InitialValue="Select" ValidationGroup="val_cpf"></asp:RequiredFieldValidator> <br />
                                                <asp:TextBox ID="txtacc_purpose1" runat="server" Visible="false" ValidationGroup="acc_purp1"></asp:TextBox><br />
                                               
                                                <asp:CustomValidator ID="cvtxt_purpose1" ValidateEmptyText="true" ControlToValidate="txtacc_purpose1" runat="server" Display="Dynamic" ValidationGroup="acc_purp1" ErrorMessage="Enter purpose of opening account" Text="Enter purpose of opening account" ClientValidationFunction="Validate"></asp:CustomValidator>

    cs file code:

    protected void ddlpurpose1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (ddlpurpose1.SelectedValue == "Others")
                {
                    txtacc_purpose1.Visible = true;
                    cvtxt_purpose1.ErrorMessage = "Enter purpose of opening account";
    
                }
                else
                {
                    txtacc_purpose1.Text = "";
                    txtacc_purpose1.Visible = false;
                    cvtxt_purpose1.ErrorMessage = "";
                }
            }


    Friday, April 27, 2012 5:14 AM
  • User1320101480 posted

    Code is fine now what is not working?

    Friday, April 27, 2012 5:21 AM
  • User-1456577918 posted

    as per the code the dropdown is validated on button click.

    but on page load text box visibility is false, it is visible only if "other" is selected from dropdown.

    so if the textbox is visible it is no validated on button click.

    Friday, April 27, 2012 5:40 AM
  • User-1456577918 posted

    hey the issue is resolved by setting SetFocusOnError="true" property of custom validator

    Friday, April 27, 2012 6:49 AM
  • User1320101480 posted

    :-) Grt!

    Friday, April 27, 2012 7:41 AM