473,414 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Which update button is clicked in FormView ItemUpdated event?

Hi

I have 2 update buttons in my FormView ('Apply' and 'OK'). I want both
buttons to update the data source but the 'OK' button should redirect
afterwards.

I can see which button is clicked in the ItemCommand event but I can't
redirect from here because the ItemUpdated event hasn't fired yet.

I can put a variable in the code-behind class which is gets the
CommandArgument in the
ItemCommand event and then read it in the ItemUpdated event but this doesn't
seem to
follow the event model concept very well. Is there a better or more
appropriate technique I could use?

Thanks
Andrew

Sep 22 '06 #1
4 4652
Hello Andrew,

Is the redirect here means redirect the current page to another page/url?
If so, what come to my mind first is use Page.ClientScript to dynamically
regisgter a clientscript to do the redirection at client-side. We can
register the scritp in the certain update button's update server event
handler. Since the client-side code will be executed after the page is
rendering to client, all the server-side code will be executed as normal.
How do you think of this?

And for client-side redirection, you can use

window.location.href = "new url"

to do the work.

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
Sep 22 '06 #2
Hi Steven

Thanks for the prompt reply. I'm not sure this will work for me as I may
wish to use Server.Transfer in the future. I have discovered a more alarming
problem with the Server-side redirect however:

The exception thrown in the ObjectDataSource Inserted event does not stop
the redirect in the FormView ItemInserted event. e.g.

protected void odsAccount_Inserted(object sender,
ObjectDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
throw e.Exception.InnerException;
}
}

// this still runs
protected void fvAccount_ItemInserted(object sender,
FormViewInsertedEventArgs e)
{
if (isRedirect) Response.Redirect(REDIRECT_PAGE);
}

I find this very odd. Can you explain this and let me know how I can get
around it?

Thanks again
Andrew
Sep 22 '06 #3
OK, I have to check for an exception in here too. Is there a could
explaination of handling exceptions in web controls?

protected void fvAccount_ItemInserted(object sender,
FormViewInsertedEventArgs e)
{
if (e.Exception == null)
{
if (isRedirect) Server.Transfer(REDIRECT_PAGE);
}
}

Thanks
Andrew
Sep 22 '06 #4
Hello Andrew,

Thanks for your quick reply.

Yes, if you'll use Server.Transfer (server-side redirection) later,
client-script approach won't work. For server-side redirection case, I
think you may consider set a flag (a page variable ) to indicate that
whether the request should be redirected and do the transfer/redirect in a
later event(such as Page_PreRender...).
And as for the new problem you mentioned:

====================
The exception thrown in the ObjectDataSource Inserted event does not stop
the redirect in the FormView ItemInserted event. e.g.

protected void odsAccount_Inserted(object sender,
ObjectDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
throw e.Exception.InnerException;
}
}

=======================

this is due to the internal exception handling codelogic of the FormView
and the underlying DataSourceView it calls(for your scenario, it's the
ObjectDataSourceView). As you've also found, the FormView.ItemInserted
event can help capture the exceptions occured during the insert operation
and expose it thourgh the event argument:

#FormView.ItemInserted Event
http://msdn2.microsoft.com/en-us/lib...trols.formview.
iteminserted.aspx

Of course, this is the particular implementation of FormView (and some
other template databound control), for some simple webcontrols, there won't
have such additional exception management code logic. Actually the
detailed code logic of the FormView's ItemInserting process is as below:

1) We use postback event to trigger the FormView's Insert or manually call
FormView.InsertItem programmtically

2) FormView call its internal "HandleInsert" method which call the
DataSourceView(obtain from datasource control)'s Insert method.
DatasourceView.Insert is an asynchornous method and FormView will pass an
callbackhandler.

3) and in DataSourceView.Insert method, there is a large try...catch....
block which will capture any exceptions and return it (without rethrow it
to the original control). The code logic is like:

4)The callbackhandler(in FormView) will construct a ItemInsertedArgument
and call the OnItemInserted method which trigger the ItemInseretd event.

#"callback" here is the callbackhandler passed in 2)
=======================
try
{
num1 = this.ExecuteInsert(values);
}
catch (Exception exception1)
{
flag1 = true;
if (!callback(num1, exception1))
{
throw;
}
return;
}
finally
{
if (!flag1)
{
callback(num1, null);
}
}
========================

Thus, you can see why you'll get the exception through the event argumenet
in the FormView.ItemInserted event. If you have interests, you can use the
reflector tool to inspect the diassembly code of the FormView and
DataSourceView for a clearer view.

If you have anything unclear above or anything else we can help, please
feel free to let me know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 25 '06 #5

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

Similar topics

3
by: Michael Glass | last post by:
I'm working on an ASP.Net web app using VS2005 and the .Net 2.0 framework, and I have a serious problem with the page I'm currently working on. The page has, among other things, two FormViews and a...
0
by: Metal2You | last post by:
I'm working on an ASP.NET 2.0 application in Visual Studio 2005 that accesses a Sybase database back end. We're using Sybase SQL Anywhere 9.0.2.3228. I have installed and registered the Sybase...
1
by: yuchang | last post by:
Hi, Using the FormView control is very efficient. But I want to do some action,like showing a success message or redirect to another page, after inserting, updating and deleting. How do I get...
3
by: Phillip Vong | last post by:
Please help a newbie. Newbie using VS2005 and ASPX. I have page1.aspx and page2.aspx. Page2 is using FormView and in the EditItem Template, I want people to be redirected to page1.aspx after...
1
by: Dan Sikorsky | last post by:
How can you tell which FormView controls have different values from their initial values when the page was loaded? If the user clicked the submit button on the EditItemTemplate and no controls...
5
by: Mark Olbert | last post by:
It appears that FormView controls require the >>exact<< same layout of controls and control types in the various templates in order to function properly. Failure to do so results in a "failure to...
2
by: Danielle | last post by:
All - Thanks in advance for any help you can provide. I've been working with a GridView in Visual Basic for a long time trying to get a list of contacts and to be able to edit and delete the...
2
by: somacore | last post by:
I have a webform in C#. This form has both a gridview and a formview. The formview displays the details of the selected gridview index. No problem there. On the formview is a button. When this...
4
by: justice750 | last post by:
Hi All, I am using a FormView control. The allows me to update records in the database. However, when a database field is null I can not update the field on the form. It works fine when the field...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
agi2029
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,...

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.