sana khan
asked on
Required field validator not working properly
I had this question after viewing Required field validator not working.
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
and modal popup goes away from page but second time when i click here button like below screenshot
the required field validator is dispalyed as soon as modal popup loads second time on the page like below screenshot
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.
<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>
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;
}
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
and modal popup goes away from page but second time when i click here button like below screenshot
the required field validator is dispalyed as soon as modal popup loads second time on the page like below screenshot
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.
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 ?
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 ?
ASKER
ASKER
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_Clic k" 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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
However, you should only add it to the validator and the save button, and also you must give them the same value.
ASKER
yes its working for me thanks
The Author was able to solve the issue using this comment.
ASKER