Home > Back-end >  ASP Custom Validator error stays even after page refresh
ASP Custom Validator error stays even after page refresh

Time:11-13

  protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void cusCustom_ServerValidate(object source, ServerValidateEventArgs args)
    {
        var dropDownValue = drpState.SelectedItem.Text;
        if (dropDownValue == ""  && args.Value.Length < 1  )
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
----------------------------------------
<asp:DropDownList ID="drpState" runat="server" CausesValidation="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="IL">Illinois</asp:ListItem>
<asp:ListItem Value="IN">Indiana</asp:ListItem>
<asp:ListItem Value="IA">Iowa</asp:ListItem>
</asp:DropDownList>

<asp:TextBox ID="txtRegion"  runat="server"></asp:TextBox>
        
    
<asp:Button ID="btnSubmit"  ValidationGroup="test" auto Text="Submit" OnClientClick=" validateFields()" runat="server" />
    <asp:CustomValidator ValidationGroup="test" ID="CustomValidator" OnServerValidate="cusCustom_ServerValidate"
                    ForeColor="Red" ErrorMessage="Please fill all fields." ValidateEmptyText ="true" 
                    ControlToValidate="txtRegion" runat="server">
                </asp:CustomValidator>

I have two fields textbox and drop down and after that I have asp custom validator when both fields are empty and I try to submit that it fire error. But the error still shows after page refresh how can I remove it?

CodePudding user response:

I think you should create a dummy class that contains the names you want to de-serialze. You only need to put those properties in the dummy class you actually want to use and not all of them e. g.

public DummyClass {
  public string Name {get;set;}
}

and then you need to adapt your code in these lines:

var result = jsonSerialization.DeserializeObject<DummyClass>(res);
MyLiteral.Text = result.Name;

CodePudding user response:

void Main()
{
    // Using Newtonsoft.Json
    var result = " {\"Distance\":0.0,\"BookingLink\":null,\"Images\":null,\"HotelID\":105304,\"Name\":\"Hyatt Regency Century Plaza\",\"AirportCode\": \"\"}";
    var hotel = JsonConvert.DeserializeObject<Hotel>(result);
    Console.WriteLine(hotel.Name);
}

// Create the object class structure based on your JSON string results
public class Hotel
{
    public double Distance { get; set; }
    public string BookingLink { get; set; }
    public object Images { get; set; }
    public int HotelID { get; set; }
    public string Name { get; set; }
    public string AirportCode { get; set; }
}
  • Related