Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Forms Masterpage Web
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.
 
<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
 
<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 4-Jan-13 21:29pm
bobb024310
Comments
Sandeep Mewara at 5-Jan-13 3:41am
   
And the issue is?
bobb024 at 5-Jan-13 12:27pm
   
the javascript values, ie documents.form[form1].hfAhed is coming back as null reference object.
bobb024 at 6-Jan-13 21:51pm
   
any help would be much appreciated
Suvabrata Roy at 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 at 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.
bobb024 at 7-Jan-13 12:03pm
   
Basically I am trying to access the asp hiddenfields values on what was the body declaration of the form with the ID of "form1". form1 is now part of the master page and now that value and $(window) are coming as null in the JavaScript code. hfAhead is a hidden field found on the aspx page. I am unable to seemingly access document.form[form1] now at runtime
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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()");
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 29-Apr-13 18:10pm
   
Not an answer, should be removed.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
bobb024 at 7-Jan-13 19:53pm
   
this does not work, it seems to be having a problem accessing the information within the asp:Contentplaceholder
bobb024 at 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
Marcus Kramer at 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.

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

  Print Answers RSS
0 OriginalGriff 385
1 Maciej Los 180
2 Tino Fourie 132
3 Richard MacCutchan 115
4 arvind mepani 104


Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid