One thing i didn't mention is that I didn't set the DataSource on the DetailsView in the aspx page. It is set in the Page_Load because it is retrieved from from data objects (NHibernate). I'm not sure if justB was doing the same thing or not.
Hello everyone, I'm back with an example that anyone should be able to try which shows the problem.
If you do the example without the ModeChanging event you get this error when you click the Edit button:
"The DetailsView 'DetailsView1' fired event ModeChanging which wasn't handled."
You don't need much ASP.NET:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateEditButton="True" OnModeChanging="DetailsView1_ModeChanging"></asp:DetailsView>
public class Product
{
public string Name
{
get { return"VS2005"; }
}
public string Publisher
{
get { return"Microsoft"; }
}
}
You will now have a detailsview which shows 2 rows, one for Name and the other for Publisher. If you click Edit, you will have to click it twice to get into edit mode, then twice to cancel out. I just noticed after the first time it only takes 1 click
to get into edit but still 2 clicks to get out of edit mode. I just relised this is because the detailsview is not rebound when you go to edit. So i changed the IsPostBack if statement, I'm not sure why the databinding makes you click edit twice.
If you have any problems getting the demo to work let me know. This demo is basically how my web app works with objects instead of SQL.
protected void DetailsView1_ModeChanging(object sender, System.Web.UI.WebControls.DetailsViewModeEventArgs e)
{
DetailsView1.ChangeMode(e.NewMode);
if (e.NewMode != Insert)
{
DetailsView1.DataSource = <get object>;
DetailsView1.DataBind();
}
}
Cancelling would go from Edit mode to ReadOnly mode, which not rebinding may be the problem you are having, emphasize "may be".
HTH.
Brian
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
Thanks for that, it works very nicely now except for Update.
I can click Update as many times as I like and it won't go back to ReadOnly. However it does update the data. Do I need to do something in the ItemUpdating event to make it go back to ReadOnly. I fixed this by doing the same thing as you suggested
for ModeChanging by databinding but I also had to set the currentmode to ReadOnly.
Thanks heaps for your help I was about to split up the page into three seperate pages for view, edit and add because I couldn't work it out; but I didn't want to because Edit and Insert are basically the same.
Thanks again for you assistance it is greatly appreicated. Jono
I had this issue as well. After banging at it for a few hours, I just deleted the handler in the code-behind, and deleted the call to the handler on the property page. I then double-clicked the handler on the property page to create a new code block, inserted
my code, and everything worked fine. I remember Access (at least Access '97, anyway) used to have the same issue from time to time, and I somehow managed to pull that fact out of long-term memory despite the overload of useless baseball statistics dwelling
there.
I have to say I've had just about enough of this wizard-driven all-your-code-in-one-place orientation you find everywhere in the documentation and samples. Who exactly are they catering for? Dumb web sites with 2 tables in the back-end?
Anyway, I found the answer while experimenting. All the samples I found were using DatasSourceID bound to some damn SQL- or ObjectDataSource object which forces you
to design and maintain (don't get me started) tableadapters using your MOUSE and a wizard or add method parameters to your control structure, blah blah..). If you want to set the DataSource at runtime, you must set the new mode before actually binding, like
in the following sample:
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
/* The mode must be set PRIOR to binding */
DetailsView1.ChangeMode(e.NewMode);
DetailsView1.DataSource = GridView1.DataSource;
DetailsView1.DataBind();
}
it works from very beginning with datagrid and gridview binding events, pageindexchanged and many others. Main idea is that after the state is changed you have to rebind your control to make it actually "see" the changes.
Just wanted to add my two cent for those still having this problem. That happened to me as well whenever I changed the DataSource or DataSourceID in PageLoad. If I needed to switch the DataSource for the same control I had to do it elsewhere or come up with
a scheme as the one mentioned earlier.
I am trying to gain access to a DropDownList that is inside a "InsertItemTemplate", but the above code ends up with null. However if I let it run once and then try it again in another round trip to the server then I get access to the control. Any idea
what is going wrong.
Try rebinding the details view before accessing the control.
Brian
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
jonorossi
Member
180 Points
38 Posts
Re: trouble with modechanging in detailsview
Jun 28, 2006 04:57 AM|LINK
Jono
jonorossi
Member
180 Points
38 Posts
Re: trouble with modechanging in detailsview
Jun 29, 2006 06:41 AM|LINK
If you do the example without the ModeChanging event you get this error when you click the Edit button:
"The DetailsView 'DetailsView1' fired event ModeChanging which wasn't handled."
You don't need much ASP.NET:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateEditButton="True" OnModeChanging="DetailsView1_ModeChanging"></asp:DetailsView>
In your code file put this in the class:
protected void Page_Load(object sender, EventArgs e) { if (true) //(!IsPostBack) { IList products = new List(); products.Add(new Product()); DetailsView1.DataSource = products; DetailsView1.DataBind(); } } protected void DetailsView1_ModeChanging(object sender, System.Web.UI.WebControls.DetailsViewModeEventArgs e) { DetailsView1.ChangeMode(e.NewMode); }And add this class to the same code file:You will now have a detailsview which shows 2 rows, one for Name and the other for Publisher. If you click Edit, you will have to click it twice to get into edit mode, then twice to cancel out. I just noticed after the first time it only takes 1 click to get into edit but still 2 clicks to get out of edit mode. I just relised this is because the detailsview is not rebound when you go to edit. So i changed the IsPostBack if statement, I'm not sure why the databinding makes you click edit twice.
If you have any problems getting the demo to work let me know. This demo is basically how my web app works with objects instead of SQL.
I appreciate any help you can provide, Jono.
bmains
All-Star
29116 Points
5886 Posts
MVP
Re: trouble with modechanging in detailsview
Jun 30, 2006 03:17 AM|LINK
Try this:
protected void DetailsView1_ModeChanging(object sender, System.Web.UI.WebControls.DetailsViewModeEventArgs e) { DetailsView1.ChangeMode(e.NewMode); if (e.NewMode != Insert) { DetailsView1.DataSource = <get object>; DetailsView1.DataBind(); } } Cancelling would go from Edit mode to ReadOnly mode, which not rebinding may be the problem you are having, emphasize "may be".HTH."Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
jonorossi
Member
180 Points
38 Posts
Re: trouble with modechanging in detailsview
Jun 30, 2006 03:46 AM|LINK
I can click Update as many times as I like and it won't go back to ReadOnly. However it does update the data. Do I need to do something in the ItemUpdating event to make it go back to ReadOnly.I fixed this by doing the same thing as you suggested for ModeChanging by databinding but I also had to set the currentmode to ReadOnly.Thanks heaps for your help I was about to split up the page into three seperate pages for view, edit and add because I couldn't work it out; but I didn't want to because Edit and Insert are basically the same.
Thanks again for you assistance it is greatly appreicated. Jono
JesusReagan
Member
64 Points
16 Posts
Re: trouble with modechanging in detailsview
Jul 16, 2006 08:07 PM|LINK
I had this issue as well. After banging at it for a few hours, I just deleted the handler in the code-behind, and deleted the call to the handler on the property page. I then double-clicked the handler on the property page to create a new code block, inserted my code, and everything worked fine. I remember Access (at least Access '97, anyway) used to have the same issue from time to time, and I somehow managed to pull that fact out of long-term memory despite the overload of useless baseball statistics dwelling there.
Scott
hwassermann
Member
5 Points
1 Post
Re: trouble with modechanging in detailsview
Jul 20, 2006 04:16 PM|LINK
Anyway, I found the answer while experimenting. All the samples I found were using DatasSourceID bound to some damn SQL- or ObjectDataSource object which forces you
to design and maintain (don't get me started) tableadapters using your MOUSE and a wizard or add method parameters to your control structure, blah blah..). If you want to set the DataSource at runtime, you must set the new mode before actually binding, like in the following sample:
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
/* The mode must be set PRIOR to binding */
DetailsView1.ChangeMode(e.NewMode);
DetailsView1.DataSource = GridView1.DataSource;
DetailsView1.DataBind();
}
sorvik
Member
45 Points
9 Posts
Re: trouble with modechanging in detailsview
Jul 21, 2006 08:03 PM|LINK
it works from very beginning with datagrid and gridview binding events, pageindexchanged and many others. Main idea is that after the state is changed you have to rebind your control to make it actually "see" the changes.
the above snippet should be changed like this:
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 mybind();
}
11 }
void mybind()
{
IList products = new List();
products.Add(new Product());
DetailsView1.DataSource = products;
DetailsView1.DataBind();
}
12
13 protected void DetailsView1_ModeChanging(object sender, System.Web.UI.WebControls.DetailsViewModeEventArgs e)
14 {
15 DetailsView1.ChangeMode(e.NewMode);
mybind();
16 }
baboso#4
Member
159 Points
154 Posts
Re: trouble with modechanging in detailsview
Apr 14, 2007 07:40 PM|LINK
Arjuna_Maram...
Member
189 Points
87 Posts
Re: trouble with modechanging in detailsview
Jun 13, 2007 10:40 AM|LINK
Hi,
I tried the same thing of
protected void DtlExpense_ModeChanging(object sender, DetailsViewModeEventArgs e) { dtlExpense.ChangeMode(e.NewMode); DropDownList ddlExpCategory = dtlExpense.FindControl(_ExpenseCategory) as DropDownList; }I am trying to gain access to a DropDownList that is inside a "InsertItemTemplate", but the above code ends up with null. However if I let it run once and then try it again in another round trip to the server then I get access to the control. Any idea what is going wrong.
Thanks
Arjuna.
bmains
All-Star
29116 Points
5886 Posts
MVP
Re: trouble with modechanging in detailsview
Jun 13, 2007 11:41 AM|LINK
Hello Arjuna,
Try rebinding the details view before accessing the control.
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).