Link to home
Start Free TrialLog in
Avatar of Nick_011
Nick_011Flag for South Africa

asked on

Cannot call C# function on closing Asp.net web page

Hi

I get the following error "Microsoft JScript runtime error: 'null' is null or not an object"
at this line btn.click(btnHidden_Click) when running the code below ;

I just need some way of calling a server C# function to dispose a few variables
when a user closes the form. I think the issue is with the master form however
all variations of  document.getElementById('btnHidden'); thatI've tried doesnt seam
to work

Any assistance would be appreciated

Kind regards
Nick

Asp.net 2008 Aspx
<%@ Page Theme="MainTheme" Language="C#"  MasterPageFile="~/Yyyyyy.Master"   . . . . .
. . . . . . . . . . . .. . . .. . . ..
<asp:Content ID="Content1" ContentPlaceHolderID="cphTitle" runat="server" >   
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="cphBody" runat="server" >

<script type="text/javascript">

 window.onunload = ReleaseVariables()
 
 function ReleaseVariables ()
 
 {
    var btn = document.getElementById('btnHidden');
   
    btn.click(btnHidden_Click);

 }  

</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

.  .  .  .  .  .  .  .  

</table>

    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

    <input type="button" id="btnHidden" value="button" onclick=btnHidden_Click/>

</asp:Content>

c# code behind
protected void btnHidden_Click(object sender, EventArgs e)
        {
            if (dv != null) dv.Dispose();  
            if (dv2 != null) dv2.Dispose();
        }
ASKER CERTIFIED SOLUTION
Avatar of Jesse Houwing
Jesse Houwing
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nick_011

ASKER

Thanks for the Info.

After looking at my source code again, I cant understand why the session variables arn't
preserving the dataview variables.  It used to work before the dataview variables were
disposed

                OraAdapter = new OracleDataAdapter(OraCommand);
                OraDataset = new DataSet();
                OraDataset.Clear();
                OraAdapter.Fill(OraDataset);
                // set some gridview properties
                GridView1.AllowPaging = true;
                GridView1.PageSize = 14;
                // bind dataset to grid
                GridView1.DataSourceID = null;
                dv = OraDataset.Tables[0].AsDataView();
                Session["dataView"] = dv;
                GridView1.DataSource = dv;
                GridView1.DataBind();

        protected void GridView1_PageIndexChanging(object sender,  
        GridViewPageEventArgs e)
        {
            // get dataview from session
            dv = (DataView)Session["dataView"];
            GridView1.DataSource = dv;
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind(); // re-bind data
            /*if (dv != null)
            {
                dv.Dispose();
                dv = null;
            }*/
        }

        public void bindGridView(string sortExp, string sortDir)
        {
            dv = (DataView)Session["dataView"];
            GridView1.DataSource = dv;

            if (sortExp != string.Empty)
            {
                dv.Sort = string.Format("{0} {1}", sortExp, sortDir);
            }
            GridView1.DataSource = dv;
            GridView1.DataBind();
            /*if (dv != null)
            {
                dv.Dispose();
                dv = null;
            }*/
        }
You're not storing a copy of the object in the session, but only a reference to it. As soon as you call Dispose on the DataView, it is also disposed inside the Session collection.
Thanks that does explain the situation.