Response.redirect Onselectedindexchanged继续触发后

时间:2014-04-24 18:35:33

标签: c# asp.net

我正在构建一个Web表单,用于使用response.redirect()从Srss调出报告。在此之后,每当发生另一次回发时,相同的onselectedindexchanged事件将继续触发。是否需要采取措施来清除事件?

例如,当ddlBlankForms索引发生更改时,事件将触发,而response.redirect将使文件下载。但是下拉列表不会像代码所示那样返回到索引0。现在,在更多事件(例如其他一个下拉列表)之后,ddlBlankForms_OnSelectedIndexChanged会继续尝试触发。如果我删除了response.redirect它似乎工作但是它打破了打开文件的目的。

代码背后。

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        ddlBusinessUnit.DataSource = sqlCommands.GetBusinessUnits();
        ddlBusinessUnit.DataValueField = "BusinessUnit";
        ddlBusinessUnit.DataBind();
        ddlBusinessUnit.Items.Insert(0, new ListItem("", ""));

        ddlBlankForms.DataSource = sqlCommands.GetForms();
        ddlBlankForms.DataTextField = "FormDesc";
        ddlBlankForms.DataValueField = "FormReport";
        ddlBlankForms.DataBind();
        ddlBlankForms.Items.Insert(0, new ListItem("", ""));
    }
}

protected void Page_Init(object sender, EventArgs e)
{
    //ddlBusinessUnit.DataSource = sqlCommands.GetBusinessUnits();
    //ddlBusinessUnit.DataValueField = "BusinessUnit";
    //ddlBusinessUnit.DataBind();
    //ddlBusinessUnit.Items.Insert(0, new ListItem("", ""));

    //ddlBlankForms.DataSource = sqlCommands.GetForms();
    //ddlBlankForms.DataTextField = "FormDesc";
    //ddlBlankForms.DataValueField = "FormReport";
    //ddlBlankForms.DataBind();
    //ddlBlankForms.Items.Insert(0, new ListItem("", ""));
}

protected void ddlBusinessUnit_OnSelectedIndexChanged(object sender, EventArgs e)
{
    ddlType.DataSource = sqlCommands.GetTypes(ddlBusinessUnit.SelectedValue);
    ddlType.DataValueField = "FormID";
    ddlType.DataTextField = "TypeDesc";
    ddlType.DataBind();
    ddlType.Items.Insert(0, new ListItem("", "")); 
}

protected void ddlType_OnSelectedIndexChanged(object sender, EventArgs e)
{
    ddlTask.DataSource = sqlCommands.GetTasks(ddlType.SelectedValue, ddlBusinessUnit.SelectedValue);
    ddlTask.DataValueField = "TaskID";
    ddlTask.DataBind();
    ddlTask.Items.Insert(0, new ListItem("", "")); 
}

protected void ddlTask_OnSelectedIndexChanged(object sender, EventArgs e)
{
    if (ddlTask.SelectedIndex == 0) return;
    string url = "http://reportserver";
    url += sqlCommands.GetSpecificFormReportName(ddlType.SelectedValue);
    url += "&BU=" + ddlBusinessUnit.SelectedValue;
    url += "&TID=" + ddlTask.SelectedValue;
    url += "&rs:Format=PDF";
    Response.Redirect(url);


    //Response.Redirect("http://mje-imtmisql03/reportserver?/QAQC_Reports/QCBore&BU=251Template&TID=Required1&rs:Format=PDF");
}

protected void ddlBlankForms_OnSelectedIndexChanged(object sender, EventArgs e)
{
    if (ddlBlankForms.SelectedIndex == 0) return;
    string url = "http://reportserver";
    url += ddlBlankForms.SelectedValue;
    url += "&BU=";
    url += "&TID=";
    url += "&rs:Format=PDF";
    ddlBlankForms.SelectedIndex = 0;
    Response.Redirect(url);
    //Response.End();
}

ASP。

<asp:DropDownList runat="server" ID="ddlBlankForms" OnSelectedIndexChanged="ddlBlankForms_OnSelectedIndexChanged" AutoPostBack="True" Width="300px"/>
<asp:DropDownList runat="server" ID="ddlTask" OnSelectedIndexChanged="ddlTask_OnSelectedIndexChanged" AutoPostBack="true" EnableViewState="True" Width="200px"/>
<asp:DropDownList runat="server" ID="ddlType" OnSelectedIndexChanged="ddlType_OnSelectedIndexChanged" AutoPostBack="True" EnableViewState="True" Width="200px"/>
<asp:DropDownList runat="server" OnSelectedIndexChanged="ddlBusinessUnit_OnSelectedIndexChanged" AutoPostBack="True" EnableViewState="true" ID="ddlBusinessUnit" Width="200px"/>

3 个答案:

答案 0 :(得分:0)

我建议您使用Page_Load event当此事件触发时,页面及其控件将完全初始化,并恢复之前的值。也可以使用IsPostBack property也是Page_Init event。 但对于 protected void Page_Load(object sender, EventArgs e) { If(!IsPostBack) { ddlBusinessUnit.DataSource = sqlCommands.GetBusinessUnits(); ddlBusinessUnit.DataValueField = "BusinessUnit"; ddlBusinessUnit.DataBind(); ddlBusinessUnit.Items.Insert(0, new ListItem("", "")); ddlBlankForms.DataSource = sqlCommands.GetForms(); ddlBlankForms.DataTextField = "FormDesc"; ddlBlankForms.DataValueField = "FormReport"; ddlBlankForms.DataBind(); ddlBlankForms.Items.Insert(0, new ListItem("", "")); } } Page_Init:框架使用此事件通过回发或查看状态数据将Web控件的属性设置为其先前的值。

所以你可以试试这个:

     the protected void Page_Init(object sender, EventArgs e)
    {
     //empty   
    }

protected void ddlTask_OnSelectedIndexChanged(object sender, EventArgs e) { if (ddlTask.SelectedIndex == 0) return; string url = "http://reportserver"; url += sqlCommands.GetSpecificFormReportName(ddlType.SelectedValue); url += "&BU=" + ddlBusinessUnit.SelectedValue; url += "&TID=" + ddlTask.SelectedValue; url += "&rs:Format=PDF"; Response.Redirect(url, false);//false to allow the code to continue execution Return; } 留空

{{1}}

我希望它会对你有所帮助。这只是众多人之间的解决方案。

更新

{{1}}

答案 1 :(得分:0)

你试过吗?

Response.Redirect(url, false);

答案 2 :(得分:0)

我对此有一个“部分”修复。它不是100%,应该被视为“黑客”。

基本上,页面必须在浏览器中呈现,以便ViewState包含“已清除”的下拉列表。

public class ResponseStuff
{
    public static void ClientRedirect(Page page,string url)
    {
        string script = "<script type='text/javascript' runat='client'>$(function() {window.location='" + page.ResolveUrl(url) + "';});</script>";
        page.RegisterClientScriptBlock("client_redirect",script);
    }
}

通过

调用
ddl.ClearSelection();
ResponseStuff.ClientRedirect(this,url);

黑客警报警告:在此修复之前,按两次(快速)浏览器后退按钮会导致相同的行为。单击后退按钮时,将再次执行重定向,但下拉列表将“清除”。这需要JQuery。