473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

retrive preselected value in second drop down list from the first drop down list

Hi
here is my scenario, I create a drop down list in itemtemplate.(that
drop down is created from db), after user
click edit command, my ideal plan is have another drop down list in
edititemtemplate with preselected value from
the previous drop down list, so far I can only achieved with the
regular drop down list in edititemtemplate with no preselected value
from previous one. anyone can help me.
thanks in advance. below is my current sample code.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script language="VB" runat="server">

Dim objConnection As SqlConnection
Dim myDataReader As SqlDataReader

Sub Page_Load(Sender As Object, E As EventArgs)
' Set up our connection.
objConnection = New SqlConnection("Data Source=sql;Initial
Catalog=pub;User Id=xxx;Password=xxxxx;")

LoadDataFromDB

If Not IsPostBack Then
DataBindGrid
End If

End Sub

Sub LoadDataFromDB()
Dim objCommand As SqlCommand

' Create new command object passing it our SQL query and
telling it which connection to use.
objCommand = New SqlCommand("SELECT * FROM Educationtraining
WHERE Username = 'weifon888';", objConnection)

' Open the connection, execute the command, and close the connection.
objConnection.Open()
myDataReader =
objCommand.ExecuteReader(System.Data.CommandBehavi or.CloseConnection)
End Sub

Sub DataBindGrid()
DBEditDataGrid.DataSource = myDataReader
DBEditDataGrid.DataBind
End Sub
'----------------------------------------------------------------------------------------------
Sub DBEditDataGrid_Edit(Sender As Object, E As
DataGridCommandEventArgs)
DBEditDataGrid.EditItemIndex = E.Item.ItemIndex
DataBindGrid
End Sub

Sub DBEditDataGrid_Cancel(Sender As Object, E As
DataGridCommandEventArgs)
DBEditDataGrid.EditItemIndex = -1
DataBindGrid
End Sub

Sub DBEditDataGrid_Update(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)
' Since the textboxes are autogenerated we don't know their
names, but we do know their positions.
Dim Tempdroplist As DropDownList
Dim Tempdropvalue As String

Dim strUser As String = E.Item.Cells(0).Text
Dim certother As TextBox = E.Item.Cells(1).Controls(0)
Dim strcertother = Replace(certother.Text, "'", "`")

Tempdroplist = E.Item.FindControl("teachercert")
Tempdropvalue = Tempdroplist.SelectedItem.Value

Response.Write(strUser)
Response.Write(strcertother)
Response.Write(Tempdropvalue)
' Update the appropriate record in our database.
Dim objCommand As SqlCommand
Dim strSQLQuery As String

' Build our update command.
strSQLQuery = "UPDATE Educationtraining SET Certother = '" &
strcertother & "' WHERE Username = 'johndoe'"

' Create new command object passing it our SQL query and
telling it which connection to use.
objCommand = New SqlCommand(strSQLQuery, objConnection)

' Close our open DataReader
myDataReader.Close()

Try
' Execute the command
objConnection.Open()
objCommand.ExecuteNonQuery()
objConnection.Close()

Catch Ex As Exception

Finally
objConnection.Close()
End Try
' Refresh our copy of the data
LoadDataFromDB()

' Reset our current edit item and rebind the grid
DBEditDataGrid.EditItemIndex = -1
DataBindGrid()
End Sub

</script>

<html>
<head>
<title>Modify User Profile</title>
<script language="javascript" type="text/javascript">
window.history.forward(1);
</Script>
</head>
<body>

<form id="Form1" runat="server">

<asp:DataGrid id="DBEditDataGrid" runat="server"
BorderWidth = "1px" CellSpacing = "2" CellPadding = "2"
HeaderStyle-Font-Bold = "True"

OnEditCommand = "DBEditDataGrid_Edit"
OnCancelCommand = "DBEditDataGrid_Cancel"
OnUpdateCommand = "DBEditDataGrid_Update"

AutoGenerateColumns = "False"
>
<Columns>
<asp:BoundColumn HeaderText="Username" DataField="Username"
ReadOnly="True" />
<asp:BoundColumn HeaderText="Cert Other" DataField="Certother"
/>

<asp:TemplateColumn FooterText="test" HeaderText="dropdown">
<EditItemTemplate>
&nbsp;

<asp:DropDownList ID="teachercert" runat="server">

<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
&nbsp;&nbsp;
</EditItemTemplate>
<ItemTemplate>
&nbsp;&nbsp;&nbsp;
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1"
DataTextField="Teachercert"
DataValueField="Teachercert" Enabled="False" AutoPostBack="True">
</asp:DropDownList><asp:SqlDataSource
ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [Teachercert] FROM
[EducationTraining]"></asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn
HeaderText = "Edit"
EditText = "Edit &amp; Continue"
CancelText = "Cancel"
UpdateText = "Update &amp; Continue"
/>

</Columns>
<HeaderStyle Font-Bold="True" />
</asp:DataGrid>

</form>
</body>
</html>

Jan 5 '07 #1
0 2129

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dan | last post by:
This is one that has me stumped and I need an expert's input. Any ideas why the values from the second script-generated drop down list isn't recognized by the script to add time values to the...
3
by: pmud | last post by:
Hi, I have a drop down list bound to a database thorugh a data reader. It reads the customer names from data reader. Now, I want the user to be able to type more than one alphabet & the list...
4
by: Martyn Fewtrell | last post by:
I want to update a database record with a number of fields - doesn't sound to difficult at this stage! I have a system to select the correct record from the database and load the data into a...
2
by: Adam Knight | last post by:
Hi all, What is the best way to preselect a drop down list item based on info from a db? Surely i don't have to write a function iterating through all the list items and comparing values to...
7
by: grummanf6f | last post by:
Hello Gurus, this probably is real simple for you but for me it's a bummer. I have one table in which I have basic data collected of schools. I have another data that is detailed info on the...
2
by: sandeepkishanj | last post by:
please tell me which collection variable (or something else) should i use to store and retrieve the following sample data... i have the following in drop down list1.. Asia North America when...
2
by: aartu | last post by:
i have two dropdown list box..if i select one value from the drop down list box the corresponding value matching to the first value shud b got in the second drop down list box
8
by: stekk | last post by:
Question: How do I display/echo the values in page the selected values from drop down lists in page 2? I think this is the line that I have to add some code to make the values SELECTED. Any help is...
2
by: axapta | last post by:
Hi Group, How can I limit a second drop-down list box based on a selected value in the first drop down list box. Using vb.net Regards
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.