Sub imgdropdown()
Dim strSQL As String = ""
strSQL = "" & _
"SELECT * " & _
"FROM upload_img " & _
"WHERE upload_folder = 'slider'"
Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString)
Using adapter As OleDbDataAdapter = New OleDbDataAdapter(strSQL, connection)
Dim ds As New DataSet()
adapter.Fill(ds)
DropDownList1.DataSource = ds
DropDownList1.DataTextField = "upload_name"
DropDownList1.DataValueField = "upload_name"
DropDownList1.DataBind()
End Using
End Using
End Sub
Then i have some records, when i click one then i get the data from a db to my textbox's so i can edit the text.
Main page
<p>
<label>Status/Visning:</label><br />
<asp:TextBox ID="TextBox6" CssClass="cssclasstextbox" runat="server" /> 0 = Ikke Aktiv, 1 = Visning i Stor Slider, 2 = Visning i lille Slider, 3 = Visning i lille/stor Slider.
</p>
<p>
<label>Billede:</label><br />
<asp:Label ID="noimg" runat="server" BorderWidth="1" BorderColor="Gray" Height="150" Width="150"></asp:Label>
</p>
<p>
<label>Billede:</label><br />
<asp:DropDownList ID="DropDownList1" Height="30px" cssClass="cssclassdropdown cssclassdropdown-select cssclassdropdown-skinned-text" runat="server"></asp:DropDownList>
</p>
<p>
<label>Start og Slut Datoer:</label><br />
<label>Slut Dato:</label><asp:TextBox ID="TextBox4" CssClass="cssclasstextbox cssclassdate_picker" runat="server" /> <label>Slut Dato:</label><asp:TextBox ID="TextBox5" CssClass="cssclasstextbox cssclassdate_picker" runat="server" />
</p>
Code Behind
Session("theeditid") = textid
Dim strSQL As String = ""
strSQL = "" & _
"SELECT * " & _
"FROM slider_list " & _
"WHERE slider_id =" & textid & ""
Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString)
Using command As OleDbCommand = New OleDbCommand(strSQL, connection)
Try
command.Connection.Open()
Using reader As OleDbDataReader = command.ExecuteReader(CommandBehavior.CloseConnection)
If reader.HasRows Then
reader.Read()
TextBox1.Text = reader.Item("slider_name")
TextBox2.Text = reader.Item("slider_title")
TextBox3.Text = reader.Item("slider_price")
TextBox4.Text = reader.Item("slider_startdate")
TextBox5.Text = reader.Item("slider_enddate")
TextBox6.Text = reader.Item("slider_status")
CKEditor1.Text = reader.Item("slider_text")
noimg.Text = "<img src=""../upload/slider/" & reader.Item("slider_img") & """ width=""148"" Height=""148"" /><br />Billede Navn: " & reader("slider_img") & ""
End If
reader.Close()
End Using
Catch exc As Exception
Finally
command.Connection.Close()
End Try
End Using
End Using
When i click that link how can i then get my dropdown not to show the first record from the database that it get its info from, but show the reader.Item("slider_img") as the selected one in the dropdown list.
DropDownList1.DataValueField="upload_name" Is the same value as reader("slider_img")
It seems your dropdown list loses the selected value after post back. you should call imgdropdown() procedure in the Page_Load event using IspostBack check like :
Sub Page_Load()
If not IsPostBack Then
imgdropdown()
End If
End Sub
Please Mark As Answer if it helped.
MCPD ASP.NET 4.0 and 3.5, MCTS WSS, MOSS, SharePoint 2010, MCT
Microsoft Community Contributor Award 2011
Hi, i get the values from the DB to the dropdown from here
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If (Not Page.IsPostBack) Then
Call imgdropdown()
......
But if i click on a edit link i have then it add some data to my textbox's so i can edit it, my question is how can i get my dropdownlist to show that value.
If i have a dropdownlist that show this
1
2
3
4
Then it always show the 1 as the first one in the dropdownlist, if i then press my edit link, then i get the some data to my textbox's and here i have a value reader("slider_img") its maybe 3, how can i get my dropdownlist to show that as the selected value
!?
If i click save then it save the slider_img as 1 bc its the first one in the dropdown, so i need to select no. 3 in the dropdownlist and then save, to keep the slider_img value as 3.
So my question is, how can i get the reader("slider_img") value inside the dropdownlist so it will show that value as the selected one.
siraero
Member
419 Points
604 Posts
dropdownlist from database selected value
Jun 25, 2012 09:20 AM|LINK
Hi.
I have some textbox's and a dropdown list, the dropdownlist get its data from this code
Main page
<p> <label>Billede:</label><br /> <asp:DropDownList ID="DropDownList1" Height="30px" cssClass="cssclassdropdown cssclassdropdown-select cssclassdropdown-skinned-text" runat="server"></asp:DropDownList> </p>Code Behind
Sub imgdropdown() Dim strSQL As String = "" strSQL = "" & _ "SELECT * " & _ "FROM upload_img " & _ "WHERE upload_folder = 'slider'" Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString) Using adapter As OleDbDataAdapter = New OleDbDataAdapter(strSQL, connection) Dim ds As New DataSet() adapter.Fill(ds) DropDownList1.DataSource = ds DropDownList1.DataTextField = "upload_name" DropDownList1.DataValueField = "upload_name" DropDownList1.DataBind() End Using End Using End SubThen i have some records, when i click one then i get the data from a db to my textbox's so i can edit the text.
Main page
<p> <label>Status/Visning:</label><br /> <asp:TextBox ID="TextBox6" CssClass="cssclasstextbox" runat="server" /> 0 = Ikke Aktiv, 1 = Visning i Stor Slider, 2 = Visning i lille Slider, 3 = Visning i lille/stor Slider. </p> <p> <label>Billede:</label><br /> <asp:Label ID="noimg" runat="server" BorderWidth="1" BorderColor="Gray" Height="150" Width="150"></asp:Label> </p> <p> <label>Billede:</label><br /> <asp:DropDownList ID="DropDownList1" Height="30px" cssClass="cssclassdropdown cssclassdropdown-select cssclassdropdown-skinned-text" runat="server"></asp:DropDownList> </p> <p> <label>Start og Slut Datoer:</label><br /> <label>Slut Dato:</label><asp:TextBox ID="TextBox4" CssClass="cssclasstextbox cssclassdate_picker" runat="server" /> <label>Slut Dato:</label><asp:TextBox ID="TextBox5" CssClass="cssclasstextbox cssclassdate_picker" runat="server" /> </p>Code Behind
Session("theeditid") = textid Dim strSQL As String = "" strSQL = "" & _ "SELECT * " & _ "FROM slider_list " & _ "WHERE slider_id =" & textid & "" Using connection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString) Using command As OleDbCommand = New OleDbCommand(strSQL, connection) Try command.Connection.Open() Using reader As OleDbDataReader = command.ExecuteReader(CommandBehavior.CloseConnection) If reader.HasRows Then reader.Read() TextBox1.Text = reader.Item("slider_name") TextBox2.Text = reader.Item("slider_title") TextBox3.Text = reader.Item("slider_price") TextBox4.Text = reader.Item("slider_startdate") TextBox5.Text = reader.Item("slider_enddate") TextBox6.Text = reader.Item("slider_status") CKEditor1.Text = reader.Item("slider_text") noimg.Text = "<img src=""../upload/slider/" & reader.Item("slider_img") & """ width=""148"" Height=""148"" /><br />Billede Navn: " & reader("slider_img") & "" End If reader.Close() End Using Catch exc As Exception Finally command.Connection.Close() End Try End Using End UsingWhen i click that link how can i then get my dropdown not to show the first record from the database that it get its info from, but show the reader.Item("slider_img") as the selected one in the dropdown list.
adeelehsan
All-Star
18287 Points
2740 Posts
Re: dropdownlist from database selected value
Jun 25, 2012 09:50 AM|LINK
It seems your dropdown list loses the selected value after post back. you should call imgdropdown() procedure in the Page_Load event using IspostBack check like :
Sub Page_Load() If not IsPostBack Then imgdropdown() End If End SubMCPD ASP.NET 4.0 and 3.5, MCTS WSS, MOSS, SharePoint 2010, MCT
Microsoft Community Contributor Award 2011
siraero
Member
419 Points
604 Posts
Re: dropdownlist from database selected value
Jun 25, 2012 10:17 AM|LINK
Hi, i get the values from the DB to the dropdown from here
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If (Not Page.IsPostBack) Then Call imgdropdown() ......But if i click on a edit link i have then it add some data to my textbox's so i can edit it, my question is how can i get my dropdownlist to show that value.
If i have a dropdownlist that show this
1
2
3
4
Then it always show the 1 as the first one in the dropdownlist, if i then press my edit link, then i get the some data to my textbox's and here i have a value reader("slider_img") its maybe 3, how can i get my dropdownlist to show that as the selected value !?
If i click save then it save the slider_img as 1 bc its the first one in the dropdown, so i need to select no. 3 in the dropdownlist and then save, to keep the slider_img value as 3.
So my question is, how can i get the reader("slider_img") value inside the dropdownlist so it will show that value as the selected one.
Mudasir.Khan
All-Star
15346 Points
3142 Posts
Re: dropdownlist from database selected value
Jun 25, 2012 10:29 AM|LINK
After populating the textboxes don't set the data source of the dropdown by default value will be selected
siraero
Member
419 Points
604 Posts
Re: dropdownlist from database selected value
Jun 25, 2012 10:42 AM|LINK
Hi
Thx for reply, im new to this what do u mean !?
siraero
Member
419 Points
604 Posts
Re: dropdownlist from database selected value
Jun 27, 2012 10:35 AM|LINK
i used this code then it worked.
In the "last code behind" sektor i added this, where i added the values to the textbox's
TextBox1.Text = reader.Item("slider_name") TextBox2.Text = reader.Item("slider_title") TextBox3.Text = reader.Item("slider_price") TextBox4.Text = reader.Item("slider_startdate") TextBox5.Text = reader.Item("slider_enddate") TextBox6.Text = reader.Item("slider_status") CKEditor1.Text = reader.Item("slider_text") noimg.Text = "<img src=""../upload/slider/" & reader.Item("slider_img") & """ width=""148"" Height=""148"" /><br/>Billede Navn: " & reader("slider_img") & "" DropDownList1.SelectedValue = reader.Item("slider_img")