Link to home
Start Free TrialLog in
Avatar of sana khan
sana khanFlag for India

asked on

Required field validator not working properly

I had this question after viewing Required field validator not working.

                    <div class="modal-body">
                        <p>Some text in the modal.</p>
                      <asp:TextBox ID="txtStandType" runat="server" name="StandType"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" Visible="false" ControlToValidate="txtStandType"></asp:RequiredFieldValidator>
                        <br />
                        <asp:CheckBoxList ID="chkFormNamesAdd" runat="server" RepeatColumns="2" RepeatDirection="Horizontal">
                        </asp:CheckBoxList>
                     
                    </div>
                    <div class="modal-footer">
                        <asp:Button ID="btnAddStandTypeModal" runat="server" Text="Save" Style="float: left" OnClick="btnAddStandTypeModal_Click" />
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

                    </div>

Open in new window


protected void btnAddStandTypeModal_Click(object sender, EventArgs e)
    {
        //try
        //{

        string txt = txtStandType.Text;

        if (txt == "")
        {
            RequiredFieldValidator1.Visible = true;
        }

        else
        {
                con.Open();
            SqlCommand cmd = new SqlCommand("insert into StandType (StandTypeName,Edition_Id) values('" + txtStandType.Text + "','" + Session["Edition_ID"] + "')", con);
            cmd.ExecuteNonQuery();
            cmd = new SqlCommand("select StandTypeID from StandType where StandTypeName='" + txtStandType.Text + "' and Edition_Id=" + Session["Edition_ID"].ToString() + " ", con);

            //grdManageStandType.DataBind();
            //Response.Write("<script>alert('Stand added Successfully');</script>");
            int addStandID = Convert.ToInt32(cmd.ExecuteScalar());

            foreach (ListItem item in chkFormNamesAdd.Items)
            {
                if (item.Selected)
                {
                    SqlCommand cmd1 = new SqlCommand("INSERT INTO StandType_FormDetails(Edition_Id,StandTypeID,formId) VALUES(" + Session["Edition_ID"].ToString() + ",'" + addStandID + "', '" + item.Value + "'); ", con);
                    cmd1.ExecuteNonQuery();

                }
            }



            //}
            //catch (Exception ex)
            //{


            //}
            //finally
            //
            con.Close();
            txtStandType.Text = "";
            grdManageStandType.DataBind();
            RequiredFieldValidator1.Visible = false;
        }

Open in new window


When first time i click on here button on page, modal popup loads first time on page and without entering when i click save button inside modal popup the error of required field validator is not displayed like below
User generated image
and modal popup goes away from page but second time when i click here button like below screenshot
User generated image
the required field validator is dispalyed as soon as modal popup loads  second time on the page like below screenshot

User generated image

i have tried all possible ways for error using javascript and asp.net but no other ways is working so i tried this way to show error.
This is working but at second time.
Avatar of sana khan
sana khan
Flag of India image

ASKER

Please help me out for this issue
Hi Sana,
You don't to hide the validator in the first place, the error message won't appear by default.
anyway, did you try to debug and check if your condition is met ?
When i am not hiding the validator when modal popup loads first time on page then it shows below screenshot User generated image
when i am debugging the code without entering anything in the textbox then it goes inside if condition of code and then after if it directly goes to last bracket of the of the  "btnAddStandTypeModal_Click" function.
but this debugging is for first time when i click on save for first time
When second time the modal popup loads on page along with error message on right side of textbox and i click on save button it doesn't allow me to save until i write something inside textbox
ASKER CERTIFIED SOLUTION
Avatar of M. Tariq
M. Tariq
Flag of Oman image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
save button inside modal popup and also to required validator
Sorry, I didn't get you
However, you should only add it to the validator and the save button, and also you must give them the same value.
yes its working for me thanks
The Author was able to solve the issue using this comment.