I am using a dropdownlists to display and on selectedindexchange download specific PDF files that are stored in my SQL Server table. I have 6 dropdownlists that display different PDF's groups, If I select the first dropdownlist box it downloads the appropriate
PDF. If I go to one of my other dropdownlist boxes and select a PDF file from it it downloads the file from the first dropdownlist.
So my thinking is that I have to reset each dropdownlist to default upon selectedindexchanged or tell the page to lose focus on the dropdownlist upon completion for the selectedindexchanged event.
Any suggestions would be greatly appreciated.
Here is the code for my first dropdownlist:
Protected Sub BM_DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BM_DDL.SelectedIndexChanged
Try
Dim fileName As String = ""
Dim fileExtention As String = ".pdf"
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as DocName,DocData FROM Documents WHERE DocName = '" & BM_DDL.SelectedValue + ".pdf'"
Dim cmd As New SqlCommand(SQLSelectStr, Gov_conn)
Gov_conn.Close()
Gov_conn.Open()
Dim dReader As SqlDataReader = cmd.ExecuteReader()
Dim dt As DataTable = New DataTable
dt.Load(dReader)
Dim bytes() As Byte = CType(dt.Rows(0)("DocData"), Byte())
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = ReturnExtension(fileExtention)
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)
Response.BinaryWrite(bytes)
Response.Flush()
Response.Close()
' Response.End()
Gov_conn.Close()
Catch generatedExceptionName As System.Threading.ThreadAbortException
Console.WriteLine(generatedExceptionName)
End Try
End Sub
Any help would be greatly appreciated.
Hi,
set drop down index to zero after selected index event(if you want to reset the value of drop down)
for ex : BM_DDL.SelectedIndex = 0 ( please correct me if I understood wrongly).
I've tried that, it isn't changing anything and the focus is still on the first dropdownlist. This is why I posted the question on here. I've fought for the last few days trying to get this to work.
Unfortunaltely i've already tried that approach. It should be making a difference or at least do something however it is not.
You could follow the steps below to simplify your steps,
Protected Sub BM_DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BM_DDL.SelectedIndexChanged
Try
Dim SelectedItemValue As String = CType(sender,DropDownList).SelectedValue
Dim fileName As String = ""
Dim fileExtention As String = ".pdf"
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as DocName,DocData FROM Documents WHERE DocName = '" & SelectedItemValue + ".pdf'"
.........
.........
End Try
End Sub
That seems like a good option. However it keeps on giving me this error.
Compiler Error Message: BC30577: 'AddressOf' operand must be the name of a method (without parentheses).
I have updated my post above. Please have a look.
Thank you.
Unfortunately the focus still remains on the first dropdownlist, even after I select a different dropdownlist.
Any suggestions?
I think you might be setting the focus somewhere down the line in your code Anyway, just set the focus before the "End Sub".
Protected Sub BM_DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BM_DDL.SelectedIndexChanged
..............
..............
End Try
CType(sender,DropDownList).focus() End Sub
If above does not work. Could you post your aspx code.
That didn't work so here is my aspx page:
<%@ Page Language="VB" MasterPageFile="~/BOC.master" AutoEventWireup="True" CodeFile="GovernanceDocuments.aspx.vb" Inherits="_GovernanceDocuments" title="Governance Documents" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
Text="All Documents are 10MB or Less"> |
onclick="btnUpload_Click" Text="Upload"/> |
|
|
|
|
AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged"> |
AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged"> |
AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged"> |
AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged"> |
AutoGenerateColumns="False" onrowcommand="GridView1_RowCommand" DataKeyNames="DocID" Width="300px">
InsertVisible="False" ReadOnly="True" SortExpression="DocID" Visible="False" /> HeaderText="Document Name" SortExpression="DocName" ItemStyle-Wrap="False" /> HeaderText="Last Update" SortExpression="Loaded" DataFormatString="{0:d}" /> HeaderText="Document Size in KB" SortExpression="DocData" DataFormatString="{0:F2}" >
SortExpression="Type" Visible="False" /> ImageUrl="~/Images/download.png" CommandName="Download" HeaderText="Download File" > ImageUrl="~/Images/Delete.jpg" CommandName="DeleteRow" HeaderText="Delete" />
|
AutoGenerateColumns="False" onrowcommand="DM_Gridview_RowCommand" DataKeyNames="DocID" Width="300px">
InsertVisible="False" ReadOnly="True" SortExpression="DocID" Visible="False" /> HeaderText="Document Name" SortExpression="DocName" ItemStyle-Wrap="False" /> HeaderText="Last Update" SortExpression="Loaded" DataFormatString="{0:d}" /> HeaderText="Document Size in KB" SortExpression="DocData" DataFormatString="{0:F2}" >
SortExpression="Type" Visible="False" /> ImageUrl="~/Images/download.png" CommandName="Download" HeaderText="Download File" > ImageUrl="~/Images/Delete.jpg" CommandName="DeleteRow" HeaderText="Delete" />
|
AutoGenerateColumns="False" onrowcommand="G_Gridview_RowCommand" DataKeyNames="DocID" Width="300px">
InsertVisible="False" ReadOnly="True" SortExpression="DocID" Visible="False" /> HeaderText="Document Name" SortExpression="DocName" ItemStyle-Wrap="False" /> HeaderText="Last Update" SortExpression="Loaded" DataFormatString="{0:d}" /> HeaderText="Document Size in KB" SortExpression="DocData" DataFormatString="{0:F2}" >
SortExpression="Type" Visible="False" /> ImageUrl="~/Images/download.png" CommandName="Download" HeaderText="Download File" > ImageUrl="~/Images/Delete.jpg" CommandName="DeleteRow" HeaderText="Delete" />
|
AutoGenerateColumns="False" onrowcommand="R_Gridview_RowCommand" DataKeyNames="DocID" Width="300px">
InsertVisible="False" ReadOnly="True" SortExpression="DocID" Visible="False" /> HeaderText="Document Name" SortExpression="DocName" ItemStyle-Wrap="False" /> HeaderText="Last Update" SortExpression="Loaded" DataFormatString="{0:d}" /> HeaderText="Document Size in KB" SortExpression="DocData" DataFormatString="{0:F2}" >
SortExpression="Type" Visible="False" /> ImageUrl="~/Images/download.png" CommandName="Download" HeaderText="Download File" > ImageUrl="~/Images/Delete.jpg" CommandName="DeleteRow" HeaderText="Delete" />
|
AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">
|
AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">
|
|
AutoGenerateColumns="False" onrowcommand="Po_Gridview_RowCommand" DataKeyNames="DocID" Width="300px">
InsertVisible="False" ReadOnly="True" SortExpression="DocID" Visible="False" /> HeaderText="Document Name" SortExpression="DocName" ItemStyle-Wrap="False" /> HeaderText="Last Update" SortExpression="Loaded" DataFormatString="{0:d}" /> HeaderText="Document Size in KB" SortExpression="DocData" DataFormatString="{0:F2}" >
SortExpression="Type" Visible="False" /> ImageUrl="~/Images/download.png" CommandName="Download" HeaderText="Download File" > ImageUrl="~/Images/Delete.jpg" CommandName="DeleteRow" HeaderText="Delete" />
|
AutoGenerateColumns="False" onrowcommand="MM_Gridview_RowCommand" DataKeyNames="DocID" Width="300px">
InsertVisible="False" ReadOnly="True" SortExpression="DocID" Visible="False" /> HeaderText="Document Name" SortExpression="DocName" ItemStyle-Wrap="False" /> HeaderText="Last Update" SortExpression="Loaded" DataFormatString="{0:d}" /> HeaderText="Document Size in KB" SortExpression="DocData" DataFormatString="{0:F2}" >
SortExpression="Type" Visible="False" /> ImageUrl="~/Images/download.png" CommandName="Download" HeaderText="Download File" > ImageUrl="~/Images/Delete.jpg" CommandName="DeleteRow" HeaderText="Delete" />
|
|
HI,
On which method to use bind dropdownlist?
Please put your bind code.
I think you have not check Page.IsPostBack. at a binding time.
I have a sub-routine called initializedatasource() upone Page_Load.
Here is the dropdownlists databind code:
Private sub InitializeDatasource()
'Populate Board Management (BM_DDL) dropdownlist.
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as BM_Name from Governance_Documents where DocName like 'BMC%' order by BM_Name"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn
Gov_conn.Close()
Gov_conn.Open()
BM_DDL.DataSource = Sqlcommand.ExecuteReader()
BM_DDL.DataTextField = "BM_Name"
BM_DDL.DataValueField = "BM_Name"
BM_DDL.Items.Insert(0, New ListItem("---Select Board Management---", String.Empty))
BM_DDL.DataBind()
Gov_conn.Close()
'Populate Delegations to Management (DM_DDL) dropdownlist.
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as DM_Name,cast(substring(Left(docname,LEN(docname)-4),5,2) as integer) as DM_Order from Governance_Documents where DocName like 'DM%' order by DM_Order"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn
Gov_conn.Close()
Gov_conn.Open()
DM_DDL.DataSource = Sqlcommand.ExecuteReader()
DM_DDL.DataTextField = "DM_Name"
DM_DDL.DataValueField = "DM_Name"
DM_DDL.Items.Insert(0, New ListItem("---Select Delegation to Management---", String.Empty))
DM_DDL.DataBind()
Gov_conn.Close()
'Populate Governance (G_DDL) dropdownlist.
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as G_Name,cast(substring(Left(docname,LEN(docname)-4),5,1) as integer) as G_Order from Governance_Documents where DocName like 'G%' order by G_Order"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn
Gov_conn.Close()
Gov_conn.Open()
G_DDL.DataSource = Sqlcommand.ExecuteReader()
G_DDL.DataTextField = "G_Name"
G_DDL.DataValueField = "G_Name"
G_DDL.Items.Insert(0, New ListItem("---Select Governance---", String.Empty))
G_DDL.DataBind()
Gov_conn.Close()
'Populate Results (R_DDL) dropdownlist.
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as R_Name,cast(substring(Left(docname,LEN(docname)-4),5,1) as integer) as R_Order from Governance_Documents where DocName like 'R%' order by R_Order"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn
Gov_conn.Close()
Gov_conn.Open()
R_DDL.DataSource = Sqlcommand.ExecuteReader()
R_DDL.DataTextField = "R_Name"
R_DDL.DataValueField = "R_Name"
R_DDL.Items.Insert(0, New ListItem("---Select Result---", String.Empty))
R_DDL.DataBind()
Gov_conn.Close()
'Populate Policies (Policies_DDL) dropdownlist.
SQLSelectStr = "Select DocName from Governance_Documents where DocName like 'A%' or DocName like 'E%' or DocName like 'M%' order by DocName"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn
Gov_conn.Close()
Gov_conn.Open()
Policies_DDL.DataSource = Sqlcommand.ExecuteReader()
Policies_DDL.DataTextField = "DocName"
Policies_DDL.DataValueField = "DocName"
Policies_DDL.Items.Insert(0, New ListItem("---Select Policy---", String.Empty))
Policies_DDL.DataBind()
Gov_conn.Close()
currentYear = Now.AddYears(0).ToString("yy")
Lastyear = Now.AddYears(-1).ToString("yy")
'Populate Policies (Policies_DDL) dropdownlist.
SQLSelectStr = "Select DocID,DocName, DocData = datalength(docData)*.001, Loaded from Governance_Documents where "
SQLSelectStr = SQLSelectStr + "DocName like '" + Lastyear + "%' or DocName like '" + currentYear + "%' order by DocName desc"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn
Gov_conn.Close()
Gov_conn.Open()
MeetingMinutes_DDL.DataSource = Sqlcommand.ExecuteReader()
MeetingMinutes_DDL.DataTextField = "DocName"
MeetingMinutes_DDL.DataValueField = "DocName"
MeetingMinutes_DDL.Items.Insert(0, New ListItem("---Select Meeting Minute---", String.Empty))
MeetingMinutes_DDL.DataBind()
Gov_conn.Close()
End sub
Could you confirm that you are calling a initializedatasource() function as below,
If not Page.IsPostBack() Then
initializedatasource()
End If
Could you post your DDL_SelectedIndexChanged function code.
Also, where are you setting the filename property value in your function (I can't find it in your code)?
I am calling the Initializedatasource(), If not page.IsPostBack.
Here is the DDL_SelectedIndexChanged function.
Protected Sub DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Dim SIV As String = CType(sender, DropDownList).SelectedValue
Dim fileName As String = ""
Dim fileExtention As String = ".pdf"
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as DocName,DocData FROM Governance_Documents WHERE DocName = '" & SIV + ".pdf'"
Dim cmd As New SqlCommand(SQLSelectStr, Gov_conn)
Gov_conn.Close()
Gov_conn.Open()
Dim dReader As SqlDataReader = cmd.ExecuteReader()
Dim dt As DataTable = New DataTable
dt.Load(dReader)
Dim bytes() As Byte = CType(dt.Rows(0)("DocData"), Byte())
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = ReturnExtension(fileExtention)
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)
Response.BinaryWrite(bytes)
Response.Flush()
Response.Close()
' Response.End()
Catch ex As Exception
End Try
CType(sender, DropDownList).Focus()
End Sub
You have missed out "Handles" part (Please check my previous post),
From:
Protected Sub DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
To:
Protected Sub DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BM_DDL.SelectedIndexChanged
That is basically taking me back to where we started from. With each DropDownlist having it's own SelectedIndexChanged.
I tried your code exactly as you put it and it made no difference.
Anyone else got any ideas as to how I can do what I am asking?