W
weiwei
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.CommandBehavior.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
ataGrid 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>
<asp
ropDownList ID="teachercert" runat="server">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp
ropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp
ropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1"
DataTextField="Teachercert"
DataValueField="Teachercert" Enabled="False" AutoPostBack="True">
</asp
ropDownList><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 & Continue"
CancelText = "Cancel"
UpdateText = "Update & Continue"
/>
</Columns>
<HeaderStyle Font-Bold="True" />
</asp
ataGrid>
</form>
</body>
</html>
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.CommandBehavior.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
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>
<asp
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp
</EditItemTemplate>
<ItemTemplate>
<asp
DataSourceID="SqlDataSource1"
DataTextField="Teachercert"
DataValueField="Teachercert" Enabled="False" AutoPostBack="True">
</asp
ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [Teachercert] FROM
[EducationTraining]"></asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn
HeaderText = "Edit"
EditText = "Edit & Continue"
CancelText = "Cancel"
UpdateText = "Update & Continue"
/>
</Columns>
<HeaderStyle Font-Bold="True" />
</asp
</form>
</body>
</html>