DetailsView Does NOT Fire ModeChanged if populated programatically

G

Guest

Hi,
When I programatically Bind a DataSource to DetailsView it does not fire
"ModeChanged" event.
This is first time i am trying to use ASP.NET DetailsView control.
I have played with some of the sample provided by Microsoft and other
comminity sites. Most of the samples were using Databinding using a
DesignTime "SqlDataSource" Control and assigning its ID to DataSourceID
property of DetailsView.

However, in all of my application we DO NOT use SqlDataSource control for
binding to DetailsView at designtime. We are using ADO.NET DataTable to
programatically assign to DataSource (NOT DataSourceID) property of
DetailsView and then call DataBind() method.

Using this procedure "ModeChanged" event of DetailsView is NOT fired at all.
It only fires "ModeChanging" event.

Following example if taken from MICROSOFT site..
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.modechanged.aspx

I have modified it a little bit to recreate problem I am facing.
I removed DataSourceID property of DetailsView from designtime declaration
and
in the Page_Load event I am assigning DataSourceID to Detailsview and it
works great.
Now if I COMMENT the line for "DataSourceID " in the page-Load and call
"populateDetailView()" method which populates DetailsView, then "ModeChanged"
event will STOP firing.
I would greatly appreciate any insight on this.
<%@ Page language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
CustomerDetailView.DataSourceID = "DetailsViewSource"

If Not Page.IsPostBack Then
'populateDetailView()
End If
End Sub

Private Function getDataTable(Optional ByVal strCustomerID As String =
"") As DataTable
Dim strConn As String = "Data Source=(local);initial
Catalog=NorthWind;Integrated Security=true;"
Dim conn As SqlConnection = New SqlConnection(strConn)
Dim strSQL As String = "SELECT [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country] From [Customers]"
If strCustomerID.Length > 0 Then
strSQL += " WHERE CustomerID = '" + strCustomerID + "'"
End If
Dim cmd As SqlCommand = New SqlCommand(strSQL, conn)
''conn.Open()
''dim dr as SqlDataReader =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
Dim dtbl As DataTable = New DataTable()
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
da.Fill(dtbl)
''conn.Close()
Return dtbl
End Function
Private Sub populateDetailView(Optional ByVal strCustomerID As String =
"")
Dim dv As DataView
Dim dtbl As DataTable
If strCustomerID.Length > 0 Then
dtbl = getDataTable(strCustomerID)
Else
dtbl = getDataTable()
End If
dv = dtbl.DefaultView
CustomerDetailView.DataSource = dtbl
CustomerDetailView.DataBind()
End Sub

Sub CustomerDetailView_ModeChanged(ByVal sender As Object, ByVal e As
EventArgs)

' Display the current mode in the header row.
Select Case CustomerDetailView.CurrentMode

Case DetailsViewMode.Edit
CustomerDetailView.HeaderText = "Edit Mode"
CustomerDetailView.HeaderStyle.ForeColor =
System.Drawing.Color.Red
CustomerDetailView.HeaderStyle.BackColor =
System.Drawing.Color.LightGray

Case DetailsViewMode.Insert
CustomerDetailView.HeaderText = "Insert Mode"
CustomerDetailView.HeaderStyle.ForeColor =
System.Drawing.Color.Green
CustomerDetailView.HeaderStyle.BackColor =
System.Drawing.Color.Yellow

Case DetailsViewMode.ReadOnly
CustomerDetailView.HeaderText = "Read-Only Mode"
CustomerDetailView.HeaderStyle.ForeColor =
System.Drawing.Color.Blue
CustomerDetailView.HeaderStyle.BackColor =
System.Drawing.Color.White

Case Else
CustomerDetailView.HeaderText = "Error!"

End Select

End Sub

Protected Sub CustomerDetailView_ModeChanging(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.DetailsViewModeEventArgs)
e.Cancel = False
End Sub
</script>

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

<h3>DetailsView ModeChanged Example</h3>

<asp:detailsview id="CustomerDetailView"
datakeynames="CustomerID"
autogeneraterows="true"
autogenerateeditbutton="true"
autogenerateinsertbutton="true"
allowpaging="true"
headertext="Read-Only Mode"
onmodechanged="CustomerDetailView_ModeChanged"
runat="server" OnModeChanging="CustomerDetailView_ModeChanging">

<fieldheaderstyle backcolor="Navy"
forecolor="White"/>

<headerstyle forecolor="Blue"/>

</asp:detailsview>

<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the web.config file. -->
<asp:SqlDataSource ID="DetailsViewSource" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWind%>"
InsertCommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode], [Country]) VALUES
(@CustomerID, @CompanyName, @Address, @City, @PostalCode, @country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country] From
[Customers]">
</asp:SqlDataSource>
</form>
</body>
</html>

--Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top