调用处理程序而不使用 Response.Redirect?

发布于 2024-11-30 20:01:27 字数 1906 浏览 1 评论 0原文

可能的重复:
如何从内部调用 ASHX ASPX.VB 函数?

看看我下面的代码,我的问题很明显,如何在不重定向的情况下调用ashx文件?我需要按此顺序执行代码,但是当页面重定向时,重定向下方的所有内容都不会执行。提前致谢。

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

    if (cbMachIcal.Checked && !cbMachGcal.Checked)
    {
        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!");
}

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)
    {
        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!");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的绿洲 2024-12-07 20:01:27

我最初的想法是,这听起来像是一个有缺陷的设计。您将浏览器重定向到 ASHX 文件,然后尝试继续执行当前页面(包括向页面输出内容)。我建议重构代码来解决这个问题。

...但是...

如果您绝对必须在页面早期重定向并仍然在原始页面中执行一些代码,则可以使用 Response.Redirect(string, bool) 使页面在调用时不会自动停止执行。

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
}

我仍然建议重构代码而不是这种方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文