开发者

Call handler without Response.Redirect? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How do I call an ASHX from inside an ASPX.VB function?

Take a look at my code below, my problem is quite obvious, how can I make a call to the ashx file without redirecting? I need the code to execute in this order but when the page redirects, everything below the redirect does not get executed. Thanks in advance.

protected void btnAddMach_Click(object sender, EventArgs e)
{
    InsertMachine();

    if (cbMachIcal.Checked && !cbMachGcal.Checked)
    {开发者_JAVA百科
        Response.Redirect("iCal.ashx?type=Lead&leadID=" + Convert.ToInt32(hfLeadID.Value) + "&date=" +
                           txtMachTickDate.Text + "&time=" + txtMachTickTime.Text + "&user=" +
                           Request.Cookies["upProspektor"]["userName"] + "&model=" +
                           ddlModel.SelectedItem.Text);
    }
    else if (!cbMachIcal.Checked && cbMachGcal.Checked)
        CreateGcalEvent();
    else if (cbMachIcal.Checked && cbMachGcal.Checked)
    {
        CreateGcalEvent();
        Response.Redirect("iCal.ashx?type=Lead&leadID=" + Convert.ToInt32(hfLeadID.Value) + "&date=" +
                           txtLeadTickDate.Text + "&time=" + txtLeadTickTime.Text + "&user=" +
                           Request.Cookies["upProspektor"]["userName"] + "&model=" +
                           ddlModel.SelectedItem.Text);
    }

    ClearControls(upMachDetails);
    UpdateHasMach();

    btnUpComm.Style.Add("display", "none");
    btnNewMach.Style.Add("display", "none");
    btnUpMach.Style.Add("display", "none");

    if (hfAddMach.Value.ToString() == string.Empty)
    {
        hfAddMach.Value = "1";
        dvPl = dvProLeads(cmdProLeads());
        gvProLeads.DataSource = dvPl;
        gvProLeads.DataBind();
        this.upReports.Update();
    }

    Utilities.DisplayAlert(this, this.GetType(), "Machine Info Entered Successfully!");
}


My initial thought is that this sounds like a flawed design. You're redirecting the browser to the ASHX file, then trying to continue your current page's executing (including outputting things to the page). I would recommend refactoring the code to fix this.

...BUT...

If you absolutely MUST redirect early in a page and still execute some code in the original page, you can use Response.Redirect(string, bool) to cause the page to not automatically stop executing when called.

protected void btnButton_Click(object sender, EventArgs e){
   if(redirect_flag){
      Response.Redirect(newUrl, false);
   }
   // This will still execute, as well as the remainder of the entire page
}

I would still recommend refactoring the code instead of this method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜