locked
Check for empty data in c# RRS feed

  • Question

  • User1151703306 posted

    Hi there,

    On this webform aspx I need check if the box Destination is empty.

    If the  box Destination is empty I need alert user and stopping procedure.

    I am using c# and a Hidden Field for get the value from webform to code behind.

    <asp:HiddenField runat="server" ID="hidJsonHolder" ClientIDMode="Static" />

    How do I add a alert, so that I can check for empty data object obj get from Hidden Field ?

    I have tried with RegularExpressionValidator but reply error

       <asp:HiddenField runat="server" ID="hidJsonHolder" ClientIDMode="Static" />
    
        <asp:RegularExpressionValidator Display="Dynamic"
            ControlToValidate="hidJsonHolder"
            ID="RegularExpressionValidator1"
            runat="server" ErrorMessage="error"
            ValidationGroup="Validation2"></asp:RegularExpressionValidator>

    This other code not alert

    protected void btnFinal_Click(object sender, EventArgs e)
    {
        JavaScriptSerializer jsSer = new JavaScriptSerializer();
        object obj = jsSer.DeserializeObject(hidJsonHolder.Value);
    
        if (obj != null)
        {
            Movie[] listMovie = jsSer.ConvertToType<Movie[]>(obj);
    
            foreach (Movie p in listMovie)
            {
                string pattern = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
                Regex re = new Regex(pattern);
                if (p.ToString() != null)
                {
                    MatchCollection matches = re.Matches(p.ToString());
                    if (matches.Count > 0)
                    {
                        for (int i = 0; i < matches.Count; i++)
                        {
                            Response.Write(matches[i] + "; ");
                        }
                    }
                }
            }
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('Error.');", true);
        }
    }

    Please see this image

    Wednesday, May 6, 2020 4:23 PM

Answers

  • User475983607 posted

    You can check the Destination contents in JavaScript/jQuery or on the server.  We cannot see the markup so you'll need to use the correct selector and/or input name.

    var val = $('#DestinationId').val();
    string val = Request["DestinationName"];

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 6, 2020 5:57 PM

All replies

  • User475983607 posted

    You can check the Destination contents in JavaScript/jQuery or on the server.  We cannot see the markup so you'll need to use the correct selector and/or input name.

    var val = $('#DestinationId').val();
    string val = Request["DestinationName"];

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 6, 2020 5:57 PM
  • User1151703306 posted

    You can check the Destination contents in JavaScript/jQuery or on the server.  We cannot see the markup so you'll need to use the correct selector and/or input name.

    var val = $('#DestinationId').val();
    string val = Request["DestinationName"];

    Thanks you for reply.

    Solved with

       protected void btnFinal_Click(object sender, EventArgs e)
        {
            string val = hidJsonHolder.Value.Replace("[]","");
    
            if (!String.IsNullOrEmpty(val.ToString()))
            {
                JavaScriptSerializer jsSer = new JavaScriptSerializer();
                object obj = jsSer.DeserializeObject(hidJsonHolder.Value);
    
                if (obj != null)
                {
                    Movie[] listMovie = jsSer.ConvertToType<Movie[]>(obj);
                    foreach (Movie p in listMovie)
                    {
                        string pattern = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
                        Regex re = new Regex(pattern);
                        if (p.ToString() != null)
                        {
                            MatchCollection matches = re.Matches(p.ToString());
                            if (matches.Count > 0)
                            {
                                for (int i = 0; i < matches.Count; i++)
                                {
                                    Response.Write("<br />" + matches[i] + "; ");
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                Response.Write("Empty2");
                this.Master.ErrorMessage = "Your Error Message here";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('Error.');", true);
            }
        }

    Thursday, May 7, 2020 7:18 AM