locked
CustomValidator, OnServerValidate RRS feed

  • Question

  • 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
    Wednesday, September 21, 2011 4:30 PM

Answers

  • User533502567 posted

    <asp:CustomValidator ID="CustomValidator1" runat="server"  ControlToValidate="TxtItemId"  
    OnServerValidate="ItemIdExists" 
                          ErrorMessage="CustomValidator"></asp:CustomValidator>

    Set ValidateEmptyText to true for CustomValidator .

            <asp:CustomValidator ID="CustomValidator2" runat="server" ControlToValidate="TxtItemId"
                OnServerValidate="ItemIdExists" ErrorMessage="CustomValidator" ValidateEmptyText="true">
    </asp:CustomValidator>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 21, 2011 4:49 PM