473,507 Members | 2,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DetailsView Does NOT Fire ModeChanged if populated programatically

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/lib...dechanged.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

Jul 20 '06 #1
0 2538

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

Similar topics

1
3227
by: sck10 | last post by:
Hello, I have a GridView that list all the employees in our group. My select button (link) when clicked will open a DetailsView control of the employee selected. I have one BoundField in the...
12
8669
by: Jim Hammond | last post by:
I am passing the whole object instead or parameters in my select and update methods. I can get the updated object if I set UpdateMethod, let ASP.NET autogenerate an update button, and then press...
1
4413
by: sck10 | last post by:
Hello, I am trying to change a value when a user goes into edit mode on a DetailsView control. I am trying to use the following, but can not figure out how to get to the bound field...
2
6237
by: nolan | last post by:
I have an asp.net 2.0 page with a gridview and detailsview on the same page set up in a master-details scenario. The gridview and detailsview have separate SQL data sources. The user enters...
1
7275
by: Mark Stafford | last post by:
I am attempting to use a DetailsView control to view some data where the fields returned by the database are determined at runtime. I create the TemplateFields on the fly using a class that...
2
5018
by: David Lozzi | last post by:
Howdy, I have a DetailsView that when I fire the UpdateItem event, it doesn't update the data. The page reloads and the data is set back to the original. I'm using a Button to fire the...
4
8932
by: Mark Olbert | last post by:
I'm trying to understand how to respond to mode changing events in a FormView control. I'm not using datasource controls, so I have to do more of the plumbing myself. Do I have to call the...
1
4131
by: ledneh | last post by:
I've been working on concurrency checking for an application I'm building, and a minor part of it has me slightly stumped. I've got a DetailsView that populates from an ObjectDataSource, using...
5
3440
by: =?Utf-8?B?bXBhaW5l?= | last post by:
Hello, I am completely lost as to why I can't update a DropDownList inside a DetailsView after I perform an insert into an object datasource. I tried to simply it down to the core demostration:...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7111
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
7319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7031
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
7485
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
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.