473,888 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DetailsView - Won't switch to Edit mode if databinding programmaticall y

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 implements
ITemplate and repopulate the Template properters of TemplateField in
OnInit. And I am DataBinding by getting a DataTable from my db
provider class in Page_Load event. When I databind programmaticall y in
the Page_Load, the data displays in the ReadOnly mode fine, but when I
click on Edit command button, it does a postback and the data in the
right column (not the headers) disappears and it does not go into Edit
mode or call the EditItemTemplat es. It just goes back into ReadOnly
mode. The ModeChanging event is fired but the ModeChanged is not. I
tried using an ObjectDataSourc e declaritively in the aspx file. Then
it then goes into Edit mode but another issue arises. The fields again
are determined at runtime so I can't hard code the UpdateParameter s. I
tried using and Update method that took a DataTable or Hashtable. But
I can't seem to find a way to have it pass me a dynamic data object
when using the ObjectDataSourc e. If I use a Hashtable, it blows up.
If my update method takes a DataTable, the update method does not get
called nor does the ItemUpdating event get called so that I could
handle it in there. And it gets stuck in Edit mode.

I have a nice welt on my forehead from banging my head against the wall
here. Does anyone have any ideas on how to view/edit data that is
determined runtime? I thought this would have been easy in ASP.NET 2.0
but maybe I am just missing something.


Aug 28 '06 #1
1 7329
I figured it out. If you use a ObjectDataSourc e but don't provide
UpdateMethod, the ItemUpdating event is still fired so you can update
your db using the e.NewValues passed at the event params. The trick is
to cancel the update using e.Cancel and then call
ChangeMode(Deta ilsViewMode.Rea dOnly) to change it back to normal view.
If you don't cancel the update, the DetailsView will attempt to find an
UpdateMethod in the ObjectDataSourc e and will blow up. But I have
tested this method and it works.

Here is the code:

DetailsViewTest .aspx
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

<script runat="server">

protected void Page_Load(objec t sender, EventArgs e)
if (!IsPostBack)
for (int n = 1; n <= 7; n++)
BoundField bf = new BoundField();
bf.DataField = "Column" + n;
bf.HeaderText = "Col " + n;
dvTest.Fields.A dd(bf);

CommandField cf = new CommandField();
cf.ShowEditButt on = true;

dvTest.Fields.A dd(cf);

//dvTest.DataSour ce = DataProvider.Ge tData();
//dvTest.DataBind ();
protected void dvTest_ModeChan ging(object sender,
DetailsViewMode EventArgs e)


protected void dvTest_ModeChan ged(object sender, EventArgs e)


protected void dvTest_ItemUpda ting(object sender,
DetailsViewUpda teEventArgs e)
DataProvider.Up dateData(e.NewV alues);

e.Cancel = true;
dvTest.ChangeMo de(DetailsViewM ode.ReadOnly);


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitl ed Page</title>
<form id="form1" runat="server">
<asp:DetailsVie w ID="dvTest" runat="server" AutoGenerateRow s="False"
DataSourceID="d sData"
OnItemUpdating= "dvTest_ItemUpd ating"
OnModeChanged=" dvTest_ModeChan ged"
OnModeChanging= "dvTest_ModeCha nging">
</asp:DetailsView >
<asp:ObjectData Source ID="dsData" runat="server"
SelectMethod="G etData" TypeName="DataP rovider">
</asp:ObjectDataS ource>
using System;
using System.Data;
using System.Collecti ons;
using System.Collecti ons.Specialized ;
using System.Web;
using System.Web.UI;

public class DataProvider
public static void UpdateData(IOrd eredDictionary newValues)
DataTable dt = GetData();
DataRow dr = dt.Rows[0];

foreach (DictionaryEntr y de in newValues)
dr[(string) de.Key] = de.Value;

HttpContext.Cur rent.Session["TheTable"] = dt;

public static DataTable GetData()
DataTable dt;
object sdt = HttpContext.Cur rent.Session["TheTable"];

if (sdt == null || !(sdt is DataTable))
dt = new DataTable();
dt.Columns.Add( "Column1");
dt.Columns.Add( "Column2");
dt.Columns.Add( "Column3");
dt.Columns.Add( "Column4");
dt.Columns.Add( "Column5");
dt.Columns.Add( "Column6");
dt.Columns.Add( "Column7");

DataRow dr = dt.NewRow();

dr["Column1"] = "Data1";
dr["Column2"] = "Data2";
dr["Column3"] = "Data3";
dr["Column4"] = "Data4";
dr["Column5"] = "Data5";
dr["Column6"] = "Data6";
dr["Column7"] = "Data7";

dt.Rows.Add(dr) ;

HttpContext.Cur rent.Session["TheTable"] = dt;
dt = (DataTable) HttpContext.Cur rent.Session["TheTable"];

return dt;

Aug 28 '06 #2

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

Similar topics

by: Shawn Wildermuth | last post by:
I have a *single* SqlDataSource that loads up a single result set that I show in a GridView. In the GridView, i've added a "Select" button and handling the SelectedItem event. I also have a DetailsView (though FormView has this same issue) where I want to be able to set the current item to bind to from the DataSource. All the examples I've seen use two data sources and on every *select* they do another round-trip to the Database (which...
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 update after making changes, but I don't want that update button. How can I get the updated object when the user presses one of my other action buttons?
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 ("MyBoundField") to set its new value. Any help with this would be appreciated. Sub EmployeeDetailView_ModeChanged(ByVal sender As Object, ByVal e As EventArgs) Select Case CustomerDetailView.CurrentMode
by: Frits van Soldt | last post by:
Hello, I hope somebody can help me with this! I have 2 listboxes in the edititemtemplate of a detailsview. In the databound event of the detailsview I would like to fill the listboxes programmatically (not through databinding). If I use detailsview.findcontrol(controlname) then an empty object is returned. How can I address these controls? Thank you for your time.
by: mike | last post by:
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...
by: rockdale | last post by:
Hi, I am upgrading my asp.net 1.x to asp.net 2.0. In my page I have a datagrid and a group of textbox, dropdownlist, checkbox... etc. the datagrid and the group of controls are hooked up - when user click on datagrid to select a row, then I will populate the record detail to those controls for user to edit. As in asp.net 2.0, I tried to use detailsview to replace the group of controls by using <ItemTemplate>. But I do not like the...
by: Timothy H. Schilbach | last post by:
Hi Everyone, I am having a massive issue trying to get custom data into my DV. What I want to do is sooo simple: 1. I have a single DV on a page 2. I created my own template (see below) 3. I created the DetailsViewDataBinging sub which populates the field using a datatable 4. I created a ModeChanging Event handler to allow me to change the mode
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
When I click the "Edit" hyperlink, my DetailsView doesn't enter Edit mode. It will if I set the DefaultMode="Edit". Any thoughts? Thanks. <asp:DetailsView ID="DetailsView1" DataKeyNames="TopicId" runat="server" Height="50px" Width="547px" AutoGenerateEditButton=true OnModeChanging="DetailsView1_ModeChanging" OnPageIndexChanging="DetailsView1_PageIndexChanging"> </asp:DetailsView> protected void DetailsView1_ModeChanging(Object...
by: =?Utf-8?B?QW5nZWw=?= | last post by:
I seem to understand how the control works as long as I mated to SqlDataSource or ObjDataSource it works fine. But I do not want to flatten my design in this manner. I am interested in how I can programmatically manipulate the control. In other words maybe just use the data source then extract from the control the information do with it as I see fit. The opposite is also true. I want to be able to add or modify the data in the...
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.