Click here to Skip to main content
15,915,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello All,
I have a dropdown in my aspx page from which I am downloading data.
From dropdown I will choose the file format and data will be downloaded in that particular format

Here is my drop-down.
C#
<asp:DropDownList ID="ddlExportRprts" runat="server" AutoPostBack="true"  OnSelectedIndexChanged="ddlExportRprts_SelectedIndexChanged">
    <asp:ListItem Text="Select" Value="0" Selected="True"></asp:ListItem>
    <asp:ListItem Text="Excel" Value="1"></asp:ListItem>
    <asp:ListItem Text="CSV" Value="2"></asp:ListItem>
</asp:DropDownList>


and here is the back-end method of the dropdown.

C#
protected void ddlExportRprts_SelectedIndexChanged(object sender, EventArgs e)
{
 string strFileName="Test";
 string strDataFilePath = Server.MapPath("./AttachedFiles/") + "\\" + strFileName + ".xls";
 DownLoadFile(strDataFilePath, strFileName);
}

private void DownLoadFile(string strFilePath, string strFileName)
{
  if (System.IO.File.Exists(strFilePath))
  {
      Response.ContentType = "application/octet-stream";
      Response.AppendHeader("Content-Disposition", "attachment; filename =" + strFileName + ".xls");
      Response.TransmitFile(strFilePath);
      Response.End();
  }
}


when ever I am selecting any option from drop down file is getting downloaded in selected format..no issues with that.

but after file is getting download and then I m clicking on any server side control like button, link button, check-box, radio-button any other drop down, this method
"ddlExportRprts_SelectedIndexChanged()" is being called where as these controls has there on click events.

I am confuse why these method is being fired when I m not calling it.

I am stuck in this problem from long time, I hope some1 out there knew how to solve it..

cheers in advance..
: )
Posted

Please check here[^]:

Quote:
Thanks for your feedback. If ViewState is disabled on the page or on the DropDownList control, the selected index cannot be saved, so each postback looks like the selected index has been changed. You can save the selected index yourself and compare against it to see if the selection has really changed, or you can enable ViewState on the DropDownList.

Try enabling viewstate.

Good luck,
OI
 
Share this answer
 
Comments
salah9 25-Jun-13 3:32am    
Sorry buddy,
havent got you clearly,
but my problem is solved...
When we write a file to the response, only the file is sent. The rest of the page doesn't get refreshed.

Due to this, the drop down selection event will be fired again at the server.

To avoid the SelectedIndexChanged event being triggered, we need to reset the value of the dropdown to its original value.

But we cannot reset it at server side in C#, because changes we make there won't reach the browser cause we are calling Response.End

So what i did was, in javascript, used 'setTimeOut' function and after 3 second of drop-down getting triggered resting the value to default value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900