User-1906053058 posted
Hi Folks
I am trying to use the custom validator control
<asp:panel>
<td>
<asp:TextBox ID="TxtItemId" runat="server">
</asp:TextBox>
</td>
<td>
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TxtItemId" OnServerValidate="ItemIdExists"
ErrorMessage="CustomValidator"></asp:CustomValidator>
</td>
</asp:Panel>
<asp:Button ID="BtnSubmit" runat="server" Text="Submit" OnClick="BtnSubmit_Click" />
aspx.cs
protected void BtnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{.....
int i = objBLL.InsertComplaintInfo(T1.SelectedValue, T2.SelectedValue,TxtItemId.Text, DDLComplaintType.SelectedValue,);
}
protected void ItemIdExists(object sender, ServerValidateEventArgs e)
{
e.IsValid = true;
string itemId = e.Value;
string itemtype = DDLItemType.SelectedValue;
if (TxtItemId.Text == "")
{
e.IsValid = false;
return;
}
if (itemtype == "P")
{
string sqlcmd = "if exists(select profile_id from basic_details where profile_id = @TXTprofileid";
string conn = ConfigurationManager.ConnectionStrings["FMMADMINConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(conn);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = sqlcmd;
cmd.Connection = con;
cmd.Parameters.AddWithValue("@TxtItemId",TxtItemId.Text);
int value = (int)cmd.ExecuteScalar();
if (value == null)
{
e.IsValid = false;
}
}
here I am checking if the TxtItemId is null and the value entered in the textbox exists in the DB.
If it exists, then Page.Isvalid = true and data should be inserted.
But the serveronValidate function is not being invoked and it is going to the InsertComplaintInfo in the Submit_Click()
According to my understanding, on click of submit button,
if the ItemIdExists in the DB and is not null,(it has to execute the
protected void ItemIdExists(object sender, ServerValidateEventArgs e) function.
then Page.isvalid should either be true or false.
and then data should be inserted
Please correct me.
Thanks
Sun