Click here to Skip to main content
15,881,862 members
Please Sign up or sign in to vote.
1.80/5 (3 votes)
I am trying to access some javascript code inside an aspx page which has a master page reference. I created a ContentPlaceHolder in the head to hold the javascript references.

XML
<asp:ContentPlaceHolder ID="HeadContentPlaceHolder" runat="server">

        <title>Sign Matrix</title>
        <link rel="Stylesheet" href="StyleSheet.css" type="text/css" />

    <script src="Scripts/jquery.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery-ui.js" type="text/javascript"></script>
        <link href="Styles/jquery-ui.css" rel="stylesheet" type="text/css" />
    </asp:ContentPlaceHolder>


inside of my aspx page the code is as follows

C#
<asp:Content ID="Content2" ContentPlaceHolderID="HeadContentPlaceHolder" runat="server">



       <script language="javascript" type="text/javascript">
        // <!CDATA[
        function body_onLoad() {
            var radioButtons = document.getElementsByName("rblAd");
            if (radioButtons.length != 0) {
                if (document.forms['form1'].hfLevelTypeID.value == "4") {
                    radioButtons[3].style.visibility = 'hidden';
                }
                var which = 2;
                if (document.forms['form1'].hfAhead.value != "")
                    which = parseInt(document.forms['form1'].hfAhead.value, 10) + 2;
                if (which >= 0 && which <= 3)
                    radioButtons[which].checked = true;
            }
        }

        function setSelectedIndex(s, v) {
            for (var i = 0; i < s.options.length; i++) {
                if (s.options[i].value == v) {
                    s.options[i].selected = true;
                    return;
                }
            }
        }
        function rblAd_onClick(e) {
            if (!e)
                e = event;
            var obj = e.target || e.srcElement;
            document.forms['form1'].hfAhead.value = obj.value;
            if (document.forms['form1'].hfLevelTypeID.value != "4") {
            setSelectedIndex(document.forms['form1'].ddlWeek, document.forms['form1'].hfAhead.value);
            }
            //document.forms['form1'].tbSearch01.value = "";
            //document.forms['form1'].tbSearch02.value = "";
            //document.forms['form1'].tbSearch03.value = "";
            document.forms['form1'].action = 'listSignBeta.aspx?hfAction=' + document.forms['form1'].hfAction.value + '&hfDepartment=' + document.forms['form1'].hfDepartment.value + '&hfTier=' + document.forms['form1'].hfTier.value + '&hfSignTypeID=' + document.forms['form1'].hfSignTypeID.value + '&hfAhead=' + document.forms['form1'].hfAhead.value; //  + '&hfS1=' + encodeURI(document.forms['form1'].tbSearch01.value) + '&hfS2=' + encodeURI(document.forms['form1'].tbSearch02.value) + '&hfS3=' + encodeURI(document.forms['form1'].tbSearch03.value);
            document.forms['form1'].enctype = "application/x-www-form-urlencoded"; //"application/x-www-html-urlencoded";  //"text/plain"; //
            document.forms['form1'].submit();
        }
        function btnEnd_onclick() {
            var confirm = window.confirm("Are you sure you want to end Sign Share?")
            if (confirm) {
                document.forms['form1'].action = 'end.aspx';
                document.forms['form1'].enctype = "application/x-www-form-urlencoded"; //"text/plain";
                document.forms['form1'].submit();
                return true;
            }
            else
                return false
        }

        function getRadioCheckValue(cbIncludeBatch) {
            var oRadio = document.forms['form1'].elements[cbIncludeBatch];
            for (var i = 0; i < oRadio.length; i++) {
                if (oRadio[i].checked) {
                    return oRadio[i].value;
                }
            }
            return '';
        }
        function cbIncludeBatch_onclick(cbIncludeBatch)
        {
            var count = document.getElementsByName("cbIncludeBatch");
            // var checkbox_val = cbIncludeBatch.value;
            //var check_val = document.forms['form1'].elements[cbIncludeBatch];
            for (var i = 0; i < count.length; i++)
             {
                if (count[i].checked)
                {
                    var agree = window.confirm("This request involves a large number of signs and will take a few moments. Would you like to proceed?");
                    if (agree) {
                        //                        if (document.forms['form1'].hfIncludeBatch.value == true)
                        //                        {

                        document.forms['form1'].action = 'listSignBeta.aspx?hfAction=' + document.forms['form1'].hfAction.value + '&hfDepartment=' + document.forms['form1'].hfDepartment.value + '&hfTier=' + document.forms['form1'].hfTier.value + '&hfSignTypeID=' + document.forms['form1'].hfSignTypeID.value + '&hfAhead=' + document.forms['form1'].hfAhead.value + '&hfIncludeBatch=true'; //  + '&hfS1=' + encodeURI(document.forms['form1'].tbSearch01.value) + '&hfS2=' + encodeURI(document.forms['form1'].tbSearch02.value) + '&hfS3=' + encodeURI(document.forms['form1'].tbSearch03.value);
                        document.forms['form1'].enctype = "application/x-www-form-urlencoded"; //"application/x-www-html-urlencoded";  //"text/plain"; //
                        document.forms['form1'].submit();
                        return true;
                    }
                    else

                            count[i].checked = false;
                            return false
                }
                    else
                        document.forms['form1'].action = 'listSignBeta.aspx?hfAction=' + document.forms['form1'].hfAction.value + '&hfDepartment=' + document.forms['form1'].hfDepartment.value + '&hfTier=' + document.forms['form1'].hfTier.value + '&hfSignTypeID=' + document.forms['form1'].hfSignTypeID.value + '&hfAhead=' + document.forms['form1'].hfAhead.value + '&hfIncludeBatch=false'; //  + '&hfS1=' + encodeURI(document.forms['form1'].tbSearch01.value) + '&hfS2=' + encodeURI(document.forms['form1'].tbSearch02.value) + '&hfS3=' + encodeURI(document.forms['form1'].tbSearch03.value);
                        document.forms['form1'].enctype = "application/x-www-form-urlencoded"; //"application/x-www-html-urlencoded";  //"text/plain"; //
                        document.forms['form1'].submit();
                        count[i].checked = false;
                        return false
            }
        }

        function btnEdit_onClick(id) {
            var sep1 = id.indexOf("_");
            var sep2 = id.indexOf("_", sep1 + 1);
            var sep3 = id.indexOf("_", sep2 + 1);
            var sep4 = id.indexOf("_", sep3 + 1);
            document.forms['form1'].hfSignID.value = id.substring(0, sep1);
            document.forms['form1'].hfSignLevelID.value = id.substring(sep1 + 1, sep2);
            document.forms['form1'].hfStampID.value = id.substring(sep2 + 1, sep3);
            document.forms['form1'].hfSignLevelTypeID.value = id.substring(sep3 + 1, sep4);
            document.forms['form1'].hfSizeID.value = id.substring(sep4 + 1);
            document.forms['form1'].action = 'oneSign.aspx?hfSignID=' + document.forms['form1'].hfSignID.value + '&hfSignLevelID=' + document.forms['form1'].hfSignLevelID.value + '&hfStampID=' + document.forms['form1'].hfStampID.value + '&hfSignLevelTypeID=' + document.forms['form1'].hfSignLevelTypeID.value + '&hfSizeID=' + document.forms['form1'].hfSizeID.value + '&hfLevelID=' + document.forms['form1'].hfLevelID.value + '&hfLevelTypeID=' + document.forms['form1'].hfLevelTypeID.value + '&hfLevelUserInfoID=' + document.forms['form1'].hfLevelUserInfoID.value + '&hfAction=' + document.forms['form1'].hfAction.value + '&hfDepartment=' + document.forms['form1'].hfDepartment.value + '&hfTier=' + document.forms['form1'].hfTier.value + '&hfSignTypeID=' + document.forms['form1'].hfSignTypeID.value + '&hfAhead=' + document.forms['form1'].hfAhead.value;   //  + '&hfS1=' + encodeURI(document.forms['form1'].tbSearch01.value) + '&hfS2=' + encodeURI(document.forms['form1'].tbSearch02.value) + '&hfS3=' + encodeURI(document.forms['form1'].tbSearch03.value);
            document.forms['form1'].enctype = "application/x-www-form-urlencoded"; //"application/x-www-html-urlencoded";  //"text/plain"; //
            document.forms['form1'].submit();
        }
        function printWindow() {
            window.print();
        }

        $(window).bind('load', function () {
            var headerChk = $(".cssPrintAll input");
            var itemChk = $(".cssPrintItem input");
            headerChk.bind("click", function () {
                itemChk.each(function () { this.checked = headerChk[0].checked; })
            }
                );
            itemChk.bind("click", function () { if ($(this).checked == false) headerChk[0].checked = false; });

            //delete all
            var delheaderChk = $(".cssDeleteAll input");
            var delitemChk = $(".cssDeleteItem input");
            delheaderChk.bind("click", function () {
                delitemChk.each(function () { this.checked = delheaderChk[0].checked; })
            }
                );
            delitemChk.bind("click", function () { if ($(this).checked == false) delheaderChk[0].checked = false; });

            //select all
            var selheaderChk = $(".cssSelectAll input");
            var selitemChk = $(".cssSelectItem input");
            selheaderChk.bind("click", function () {
                selitemChk.each(function () { this.checked = selheaderChk[0].checked; })
            }
                );
            selitemChk.bind("click", function () { if ($(this).checked == false) selheaderChk[0].checked = false; });
        });
        // ]]>
</script>
  </asp:Content>



after that i have another content placeholder that holds the buttons that require the javascript. Please advise
Posted
Comments
Sandeep Mewara 5-Jan-13 3:41am    
And the issue is?
bobb024 5-Jan-13 12:27pm    
the javascript values, ie documents.form[form1].hfAhed is coming back as null reference object.
bobb024 6-Jan-13 21:51pm    
any help would be much appreciated
Suvabrata Roy 7-Jan-13 0:16am    
where get error ?
I mean to say for which method you are getting error?
Or for all methods ?
Slava Khristich 7-Jan-13 2:35am    
What is hfAhead? How do you define it on your form?
Also, if you move your javascript into a separate file you'll get much better experience and reuse, also it would be easier to debug script.

If you are using jquery anyway, you should use the jquery syntax for selecting objects. It is much cleaner and easier to figure out.
Try referencing the object like this: $('#hfSignID').val() instead of document.forms['form1'].hfSignID.value
 
Share this answer
 
Comments
bobb024 7-Jan-13 19:53pm    
this does not work, it seems to be having a problem accessing the information within the asp:Contentplaceholder
bobb024 7-Jan-13 21:41pm    
does anyone know how to access the value of document.forms? I have searched high and low as when I debug it, I am able to see that the document.forms has a count of 1 but I cannot find the actual name. I have tried to put up a watch and still not able to find the value
fjdiewornncalwe 8-Jan-13 10:17am    
If you want to have a name on the form, just add an "id=YourFormName" attribute to the form tag in your html. You can then access the form directly by that id.
what i needed to do was two things. I needed to access the hiddenfields in this manner,

document.all["<%=hfAhead.ClientID%>"].value

and then i needed to build in my code behind a method to call a property i placed on the master.cs code.

master.cs - property
public HtmlGenericControl body
{
get
{
return this.bdyMaster;
}
}

aspx page .cs
HtmlGenericControl mybody = this.Master.body;
mybody.Attributes.Add("onload", "body_onLoad()");
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Apr-13 18:10pm    
Not an answer, should be removed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900