PAssing QueryString to  Codebehind in Same aspx page

wint100
wint100 used Ask the Experts™
on
I'm developijg a web app in .NET 3.5SP1 using VS2008.

I'm having problems when passsing a parameter to the codebehind. The first part of the code below is the javascript used to render a chart by passing parameters to the codebehind, and retrieving the xaml data back that is required to render the chart. The second part is the code behind in the SAME aspx page.

The problem I have is that if I try to populate and bind the GridView within the 'if (Request.Querystring[DataSeriesIndex] == "none")' statement, the Grid isn't getting populated. The ChartXaml is returned OK, but the girdview is always empty. If I remove the GridView binding from the querystring statement, the gridview populates and binds fine. For some reason when trying to populate within this statement, it is preventing the GridView from being populated and binding.

I've tested this by debugging the code line by line, and the Values used to populate the gridview ARE set to the correct values, but for soe reason the gridview isn't binding. I've also tried taking the GridView1.DataBind() out of the statement, and putting it into the 'Finally' part, but this still doesn't work.

I've also tried:

string index =Request.Querystring[DataSeriesIndex];

And then using this for the IF statement, but this does the same thing. And also tried calling a seperate void, if Index =="none".

The code inside the IF statement IS firing, because the ChartXaml is being returned ok. Maybe I need to send a seperate call from javascript? I trying to call the dataTable into javascript and bind in javascript??

Can anyone help here??


function GetXMLHttpObj() {

        try {
            // Firefox, Opera 8.0+, Safari
            objXmlHttp = new XMLHttpRequest();
        }
        catch (e) {
            // Internet Explorer
            try {
                objXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {
                    alert("Your browser does not support AJAX!");
                    return null;
                }
            }
        }
        return objXmlHttp;
    }
    // Loads Visifire Chart
    function onLoad() {
        var xmlHttp1 = GetXMLHttpObj();

        xmlHttp1.onreadystatechange = function() {
            if (xmlHttp1.readyState == 4) {


                vChart.setDataXml(xmlHttp1.responseText.substr(0, xmlHttp1.responseText.indexOf("<!DOCTYPE ")));
                //Added for Drilldown------------------------------
                vChart.preLoad = function(args) {
                    chart = args[0];

                    chart.Series[0].MouseLeftButtonUp = function(e) {

                        OnDataSeries1MouseLeftButtonUp(e.AxisXLabel);
                    };


                    //}
                };
                //-------------------------------                
                vChart.render("Visifire1");
            }
        }

        // Sending request


        xmlHttp1.open("GET", "AnnualEnergyChart.aspx?action=GetXML1&DataSeriesIndex=none&id=" + rID, true); // Request sent for first chart data (GetXML1);
        xmlHttp1.send(null);


        var xmlHttp2 = GetXMLHttpObj();

        xmlHttp2.onreadystatechange = function() {
            if (xmlHttp2.readyState == 4) {

                vChart2.setDataXml(xmlHttp2.responseText.substr(0, xmlHttp2.responseText.indexOf("<!DOCTYPE ")));
        //Added for Drilldown------------------------------
                vChart2.preLoad = function(args) {
                    chart2 = args[0];


                    chart2.Series[0].MouseLeftButtonUp = function(e) {

                        OnDataSeries1MouseLeftButtonUp(e.AxisXLabel);
                    };


        //}
                };
        //----
                vChart2.render("Visifire2");
            }
        }

        // Sending request

        xmlHttp2.open("GET", "AnnualEnergyChart.aspx" + "?action=GetXML2&DataSeriesIndex=none&id=" + rID, true); // Request sent for first chart data (GetXML1);
        xmlHttp2.send(null);
    }


----------------------------------------------------------------
c# code behind in the same page

if (Request.Querystring[DataSeriesIndex] == "none")
                        {
                            
                                dt = Single_Data(sDate, MeterName);
                                if (count == 1)//The following is called only when a single date is selected
                                {
                                    chartXaml1 = chart1_single_dt(dt, SDate, Units, MeterName_Name);
                                    chartXaml2 = chart2_single_dt(dt, SDate, Units, MeterName_Name);
                                }
                                
            DataTable Master = new DataTable();
            double Day_Total1 = new int();
            double Cost1 = new int();
            string Units = " kWh";
            if (dt.Compute("sum(MthData)", "") != DBNull.Value)
            {
                Day_Total1 = System.Convert.ToDouble((dt1.Compute("sum(MthData)", "")));
                Cost1 = System.Convert.ToDouble((dt1.Compute("sum(TotalCost)", "")));
            }
            DataColumn mycolumn1 = new DataColumn();
            mycolumn1.ColumnName = "Meter";
            Master.Columns.Add(mycolumn1);

            DataColumn mycolumn2 = new DataColumn();
            mycolumn2.ColumnName = "Total";
            Master.Columns.Add(mycolumn2);

            DataColumn mycolumn3 = new DataColumn();
            mycolumn3.ColumnName = "Cost";
            Master.Columns.Add(mycolumn3);

            DataRow row = Master.NewRow();
            row["Meter"] = MeterName_Name + " " + DateTime.Parse(sDate).ToString("yyyy");
            row["Total"] = Math.Round(Day_Total1, 0).ToString() + " " + Units;
            row["Cost"] = "£ " + Math.Round(Cost1, 2).ToString();
            Master.Rows.Add(row);
            BoundField Meter = new BoundField();
            Meter.DataField = "Meter";
            Meter.HeaderText = "";
            Meter.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
            Meter.ItemStyle.Width = 400;

            BoundField Total = new BoundField();
            Total.DataField = "Total";
            Total.HeaderText = "Total";
            Total.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            Total.ItemStyle.Width = 150;

            BoundField Cost = new BoundField();
            Cost.DataField = "Cost";
            Cost.HeaderText = "Cost";
            Cost.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            Cost.ItemStyle.Width = 150;

            GridView1.Columns.Add(Meter);
            GridView1.Columns.Add(Total);
            GridView1.Columns.Add(Cost);
            GridView1.DataSource = Master;
            GridView1.DataBind();
                        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
Try:
string index =Request.Querystring["DataSeriesIndex"];
or replace line 91 with:
if (Request.Querystring["DataSeriesIndex"] == "none")

You are missing the quotes.

Author

Commented:
That is a typo on my post only, the code has the quotes.

I've tried both of the above but still have the same issue.
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
Please put a breakpoint at:
if (Request.QueryString["DataSeriesIndex"] == "none")

and post  the Request.Querystring properties that you can see in the debugger

I also need the url that request is receiving (from debugger not from your code).

Also. let me know if your C# code is executing in Page_Load event.
http://dotnetperls.com/querystring-net
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Author

Commented:

The code is all executing in the pageLoad event yes.

OnLoad() in java the Request is sent as  xmlHttp1.open("GET", "AnnualEnergyChart.aspx?action=GetXML1&id=" + rID+"&index="+index, true); // Request sent for first chart data (GetXML1);
 And the dubugger shows:   Request.QueryString = {action=GetXML1&Date=Mon+19%2f10%2f09&id=1264831857076&index=0}    

OnEventRun, the code is run again but with DataSeriesIndex as a different value, on the event, the debugger showed that the querystring was receving the value. The Values being bound to the Gridview were all correct, and the grid columns were set to the correct size, width etc.. programatically, although the gridview on the page was still blank? Maybe the Gridview is being rendered before the codebehind has finished binding the data?

Here is the debug info from the OnEvent():
Request.QueryString = {action=GetXML1&Date=Mon+19%2f10%2f09&id=1264831857076&Index=1}

From the dubugger, it looks like the pageload event is fired straight away, before the Java Event for "Body onload ="onLoad()" is run. So the pageload event behind, first runs with index=null, and bind the gridview to a blank datasource. Then when the OnLoad() event fires, it isn't updating the gridview. Maybe I need an update panel??



Senior Software Engineer
Top Expert 2009
Commented:
The js code needs to be in a different page (AnnualEnergyChart.aspx  can not load itself twice). Remember that you are calling server functions that are still implemented as pages(I will suggest make your chart information in a separate page like AnnualEnergyChartHelper.aspx and Implement Page_load logic there and set response object appropiately.
Check:
http://luke.breuer.com/tutorial/xmlhttprequest.aspx
http://javascript-reference.info/how-to-use-xmlhttprequest.htm

Author

Commented:
My problem with using 2 pages was how to return a datatable from the helper page, the the chart page and also how to then populate the gridview.

Author

Commented:
I still do not understand why the Chartxaml is being returned, but the gridview is not binding, or being updated.
Miguel OzSenior Software Engineer
Top Expert 2009
Commented:
You can always refactor your code in a separate page and put any required information in the Session object so that your helper page can use it.
On your main page you can use a frame to load your gridview helper page there.

Author

Commented:
Sounds interesting.  I'll look into frames. thanks

Author

Commented:
Sounds interesting.  I'll look into frames. thanks

Author

Commented:
The iframe seems to have done the trick for the initial load. The only problem I have now is updating the iframe on button click.

onLoad of form I'm setting the source of the iframe as 'Data.aspx?index=0'. Data.aspx is my helper page. This works great when page is intitially loaded. OnClick i'm changing iframe source to 'Data.aspx?index=1', but the gridview just disappears from the page, so no data is being returned.

Am I missing something here? How can I reload Data.aspx, sending querystring 'index=1' onClick?

Author

Commented:
I wasn't sending the correct querytring back to the data.aspx page when trying to reload the iframe. This is now fixed.

So when the button is clicked, the source is altered:

var iframe = document.getElementById('frame1'); // just for clarity

        iframe.src = 'Data.aspx?index=0';

All working now, thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial