1

I get this error so my update button doesnt work and I havent updated my database progress because of this error.I want to be visible of textbox and I try to do in .cs side

This javascript codes using checkboxs and textboxs send to cs side with json

        function UpdateDetails1() {

            var bEnabled = GetCheckBoxVal($("#<%=CB_Enabled_Edit.ClientID%>"));
            var bDisplayDetails = GetCheckBoxVal($("#<%=CB_DisplayDetails_Edit.ClientID%>"));
            var b3rdParty = GetCheckBoxVal($("#<%=CB_b3rdParty_Edit.ClientID%>"));
            var bDisplayOpenNow = GetCheckBoxVal($("#<%=CB_DisplayOpenNow_Edit.ClientID%>"));
            var bReservation = GetCheckBoxVal($("#<%=CB_Reservation_Edit.ClientID%>"));
            var bPromotion = GetCheckBoxVal($("#<%=CB_Promotion_Edit.ClientID%>"));
            var bOnlyPromotion = GetCheckBoxVal($("#<%=CB_OnlyPromotion_Edit.ClientID%>"));
            var bAllowFollow = GetCheckBoxVal($("#<%=CB_AllowFollow_Edit.ClientID%>"));
            var bAlacarte = GetCheckBoxVal($("#<%=CB_Alacarte_Edit.ClientID%>"));
            var bDisplayOpenEat = GetCheckBoxVal($("#<%=CB_DisplayOpenEat_Edit.ClientID%>"));
            var Coord_Lat = $("#<%=TB_Coord_Lat_Edit.ClientID%>")[0].value;
            var Coord_Long = $("#<%=TB_Coord_Long_Edit.ClientID%>")[0].value;
            var Price = $("#<%=TB_Price_Edit.ClientID%>")[0].value;
            alert(Price);
            var Phone_Number = $('#<%=TB_Phone_Number_Edit.ClientID%>')[0].value;
            if (bReservation == true)
                $("#display_res_schedule").css("display", "block");
            else
                $("#display_res_schedule").css("display", "none");

            var jsonData = '{ bEnabled: "' + bEnabled + '",' +
                    'bDisplayDetails: "' + bDisplayDetails + '",' +
                    'b3rdParty:  "' + b3rdParty + '",' +
                    'Price:  "' + Price + '",' +
                    'bDisplayOpenNow: "' + bDisplayOpenNow + '",' +
                    'bReservation: "' + bReservation + '",' +
                    'bPromotion: "' + bPromotion + '",' +
                    'Phone_Number: "' + Phone_Number + '",' +
                    'bOnlyPromotion: "' + bOnlyPromotion + '",' +
                    'bAllowFollow: "' + bAllowFollow + '",' +
                    'bAlacarte: "' + bAlacarte + '",' +
                    'bDisplayOpenEat: "' + bDisplayOpenEat + '",' +
                    'Coord_Lat: "' + Coord_Lat + '",' +
                    'Coord_Long: "' + Coord_Long + '" }';
            
            alert(jsonData);
           // var jsonData = '{ bEnabled: "' + bEnabled + '",' +
           //       'bDisplayOpenNow: "' + bDisplayOpenNow + '",' +
           //         'bReservation: "' + bReservation + '",' +
           //         'Coord_Lat: "' + Coord_Lat + '",' +
           //         'Coord_Long: "' + Coord_Long + '" }';

            $.ajax({
                type: "POST",
                url: "Edit.aspx/UpdateDetails1",
                data: jsonData,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: GetDetails1,
                failure: function (response) {
                    alert(response.d);
                }
            });
        }

I need to do visible false checkboxes and textboxes that I don't use.But TB_Phone_Number_Edit.Visible gets Uncaught TypeError: Cannot read property 'value' of undefined error

 protected void Page_Load(object sender, EventArgs e)
    {
        LoadType = Request.QueryString["t"].ToString();
        if (LoadType == "Agency")
        {
            CB_DisplayOpenNow.Visible = false;
            CB_DisplayOpenNow_Edit.Visible = false;
            CB_Alacarte.Visible = false;
            CB_Alacarte_Edit.Visible = false;
            CB_DisplayOpenEat.Visible = false;
            CB_DisplayOpenEat_Edit.Visible = false;
            CB_Reservation.Visible = false;
            CB_Reservation_Edit.Visible = false;
            CB_Promotion.Visible = false;
            CB_Promotion_Edit.Visible = false;
            CB_OnlyPromotion.Visible = false;
            CB_OnlyPromotion_Edit.Visible = false;
            TB_Price_Edit.Visible = false;
            DDL_Currency.Visible = false;
            DDL_Camera.Visible = false;
            CB_AllowFollow.Visible = false;
            CB_AllowFollow_Edit.Visible = false;
            WhatDoNow.Visible = false;
            Alacarte.Visible = false;
            WhatEatNow.Visible = false;
            Promotion.Visible = false;
            Reservation.Visible = false;
            OnlyPromotion.Visible = false;
            Price.Visible = false;
            Camera.Visible = false;
            AllowFollow.Visible = false;
            Label23.Visible = false;
            Label24.Visible = false;
            Label25.Visible = false;
            Label26.Visible = false;
            Label27.Visible = false;
            Label29.Visible = false;
            Label30.Visible = false;
            Label33.Visible = false;
            Label34.Visible = false;
            TB_Price_Edit.Visible = false;
        }
        else if(LoadType == "Tour"){
            CB_b3rdParty.Visible = false;
            CB_b3rdParty_Edit.Visible = false;
            CB_Alacarte.Visible = false;
            CB_Alacarte_Edit.Visible = false;
            DDL_Camera.Visible = false;
            CB_AllowFollow.Visible = false;
            CB_AllowFollow_Edit.Visible = false;
            CB_DisplayOpenEat.Visible = false;
            CB_DisplayOpenEat_Edit.Visible = false;
            b3rdParty.Visible = false;
            Alacarte.Visible = false;
            WhatEatNow.Visible = false;
            Camera.Visible = false;
            AllowFollow.Visible = false;
            Label18.Visible = false;
            Label24.Visible = false;
            Label25.Visible = false;
            Label33.Visible = false;
            Label34.Visible = false;
            Phone_Number.Visible = false;
            TB_Phone_Number_Edit.Visible = false;
            Label8.Enabled = false;

        }
wotmn
  • 25
  • 6
  • I change codes as in this article but this mistake has continued.How I fix this mistake.Can I help me? – wotmn Nov 17 '16 at 11:37

2 Answers2

0

Setting a textbox to Visible = false makes it not render in your html. So when you try to get the value in your javascript, it effectively does not exist, making it undefined.

To fix this, either replace the textbox by a hidden field or change the opacity so that the textbox is there but transparent.

AsheraH
  • 474
  • 10
  • 15
  • I know textbox being visible false isn't rendered in my html.But how I fix this mistake? – wotmn Nov 17 '16 at 11:43
0

Assuming the problem is not in GetCheckBoxVal any of the following statements is causing your issue:

var Coord_Lat = $("#<%=TB_Coord_Lat_Edit.ClientID%>")[0].value;
var Coord_Long = $("#<%=TB_Coord_Long_Edit.ClientID%>")[0].value;
var Price = $("#<%=TB_Price_Edit.ClientID%>")[0].value;
var Phone_Number = $('#<%=TB_Phone_Number_Edit.ClientID%>')[0].value;

Why? Because if the css selector given to jQuery returns no matches, the array will have zero elements, aka, its length will be 0. That does mean that even on element[0] there is nothing, its value is undefined. Trying to get a property or invoke a method on an undefined value gives you the exception you see.

As the suggested duplicate suggest you should guard against this situation. One possible way is to introduce a utility function:

// this gets the value property of an object and if object is undefined
// returns a default
function getValueOrDefault(someObject, reasonableDefault) {

    return someObject === undefined ? reasonableDefault : someObject.value;
}

// your original function
function UpdateDetails1() { 
    // rest of your code
}

With that function in place your code becomes:

var Coord_Lat = getValueOrDefault($("#<%=TB_Coord_Lat_Edit.ClientID%>")[0], '0.0');
var Coord_Long = getValueOrDefault($("#<%=TB_Coord_Long_Edit.ClientID%>")[0], '0.0');
var Price = getValueOrDefault($("#<%=TB_Price_Edit.ClientID%>")[0],0);
var Phone_Number = getValueOrDefault($('#<%=TB_Phone_Number_Edit.ClientID%>')[0],'+1 010');

That should resolve the Uncaught type error. It is up to you to come up with reasonable defaults.

Graham
  • 7,431
  • 18
  • 59
  • 84
rene
  • 41,474
  • 78
  • 114
  • 152