Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display message then redirect 1

Status
Not open for further replies.

dvannoy

MIS
May 4, 2001
2,765
US
I am using crystal to export some reports to excel. the user has chioces on what location he/she wants to run the report on. I then pass a value to the form that gets exported to excel.

now, the way it is now is if there is no records

If ds.Tables("Table").Rows.Count = 0 Then

it display's a message and in my code I exit sub, but stll goes to the screen that would hold the data to export. since there is no records then screen is blank just showing the value I passed.

what I want to do is If ds.Tables("Table").Rows.Count = 0
Then redirect the user back to the previous screen where he/she selects the locations.


thanks in advance

 
Well I guess I would setup the page to accept QueryStrings as follows:

Code:
    protected void Page_Load(object sender, EventArgs e)
    {
        string Err;

        if (Request.QueryString.Keys.Count > 0)
        {
            Err = Request.QueryString["Error"].ToString();

            if (Err == "whatever")
            {
                lblError.Visible = true;
            }
            else
            {
                //whatever
            }
        }
    }

(C# but should be easy to port to VB)

Then in your if statement add this:
Code:
Response.Redirect("[URL unfurl="true"]http://www.mywebsite.com/mypage.aspx?Error=whatever");[/URL]
Then when the page redirects you can display an error message via a label. Probably not the best way to do this by a long way, but it works quite well.

Regards, Daniel.
 
the problem is when I include the response.redirect in my if statement, the message never get displayed. it just stays on the current page.

 
Response.Redirect will do that, due to order of events. Your "best" option IMO is javascript. This will alert a message you pass in, and when the user clicks ok, it will send them to the page you pass in.

<asp:Label id="lblJSMsg" runat="server" EnableViewState="false" />

put that at the end of your html (above the </form>)

add these routines (c#)
Code:
    public void YourDataRetrievalThingy()
    {
        DataSet ds = getMyDataSet();
        if (ds.Tables[0].Rows.Count == 0)
        {
            jsAlert(lblJSMsg, "No Rows Mr Joe", "index.aspx");
        }
        else
        {
            //start loading your data thingy
        }
    }

    public void jsAlert(Control lblWriteTo, string msg, string redir)
    {
        ((Label)(lblWriteTo)).Text = "<script language='javascript'>alert('" + msg + "');parent.document.location.href='" + redir
            + "';</script>";
    }

vb (
Code:
    Public  Sub YourDataRetrievalThingy()
        Dim ds As DataSet =  getMyDataSet() 
        If ds.Tables(0).Rows.Count = 0 Then
            jsAlert(lblJSMsg, "No Rows Mr Joe", "index.aspx")
        Else 
            'start loading your data thingy
        End If
    End Sub
 
    Public  Sub jsAlert(ByVal lblWriteTo As Control, ByVal msg As String, ByVal redir As String)
        (CType((lblWriteTo), Label)).Text = "<script language='javascript'>alert('" & msg & "');parent.document.location.href='" & redir & "';</script>"
    End Sub
 
Hi,

Just to sort your query, so you basically have 2 pages: a selection page (PageA), and the output page (PageB) which processes the parameters from pageA.

On PageB, if there are no records, a message gets displayed and goes back to PageA.

How about this: On PageB if there were no records, you go back to PageA with the error/warning code, maybe passing it via querystring. PageA parses the parameter and flushes the error message that corresponds the error code.

To flush the error message back to the browser, you can either set a hidden control or set it in the html page in mix-mode. Point is, you need to send the error message to the browser and let the browser display it.

** PageB codebehind **
Code:
void Page_Load(...)
{
    if (ds.Tables("Table").Rows.Count = 0)
        Redirect("selection_page.aspx?err=norecord");
}

** selection_page.aspx **
Code:
// code behind, server
void Page_Load(...)
{
    if (Request.QueryString("err") == "norecord")
        hiddenControl.Value = "Some error message";
}


// html javascript, client
window.onload = function()
{
  var txt = document.getElementById('hiddenControl').innerHTML;
  if (txt.length == 0)
    alert(txt);
};
I'm sure there are better solutions but I just want to share my thoughts on this. PageB should only bother with processing the report. If processing fails because of invalid parameters, it informs PageA what was the problem. PageA only takes care of parameters including displaying of parameter-related errors. Notice that this sequence well utilize server roundtrips too.

I prefer using hidden control to hold the alert message and display it from the onload event because then I can move this code in a separate js file and take advantage of the browser's file caching. It's easier to debug too.


Just my 2cents.
hope this helps
[wink]
 
sorry guys I was on vacation... yes vacation

adamroof,

thanks!! exactly what I needed.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top