响应后.直接选择的索引更改保持激发

本文关键字:索引 选择 响应 | 更新日期: 2023-09-27 18:00:12

我正在构建一个web表单,用于使用response.redirect()从Srss中调出报告。此后,每当发生另一次回发时,所选的索引更改事件都会继续激发。是否需要采取措施来清除事件?

例如,当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"/>

响应后.直接选择的索引更改保持激发

您尝试过以下操作吗?

Response.Redirect(url, false);

我建议您使用Page_Load event:当此事件触发时,页面及其控件将完全初始化,并恢复其以前的值同时使用IsPostBack property。但对于Page_Init event:框架使用此事件通过回发或查看状态数据将web控件的属性设置为以前的值。

所以你可以试试这个:

    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为空

     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;
 }

我对此进行了"部分"修复。这不是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。