Is it possible to call one event to another event in asp.net? - asp.net

Suppose i have event: protected void dpMyNoteBook_PreRender(object sender, EventArgs e),
Inside this event i want to call another event name as : protected void ibtnPinMarkedRemovePin_Click(object sender, System.Web.UI.ImageClickEventArgs e).
Is it possible to do this?

The easiest solution is to move the code that currently is in ibtnPinMarkedRemovePin_Click into a helper method and call that from dpMyNoteBook_PreRender as well.

Event handlers are normal functions that happen to be executed when an event is fired.
Yes. (You can pass nulls as the two parameters, unless your code actually uses the parameters)
However, it would probably be better to move the code to a separate method and call that method from both handlers.

Events are just methods. You will need to create a new ImageClickEventArgs object and make sure it's populated properly (and you'll want to make sure sender is set to the image in question receiving the "click"), but it's just a method call.
ImageClickEventArgs e = new ImageClickEventArgs();
// set properties for e that may be relevant
// commandargument
// commandname
// whatever
ibtnPinMarkedRemovePin_Click(myImagesId, e);

how to map the Selection_change event to DayRender event of the Calender control

Related

Calling click event (CommandEventArgs) of a button serverside

I refer to this article.
I also use ASP.NET and I got the following button click event:
protected void Button1_Click(object sender, CommandEventArgs e)
{
//Do some stuff
}
When I try to call the click event serverside I get an error. It says that it can't convert System.EventArgs to System.Web.UI.WebControls.CommandEventArgs.
Is it possible to call the event the same way as in the article I did refer to?
Thanks!
You can absolutely call it still, it is just a normal C# method. But the caveat is that you need to provide correct values as command name and command argument, as this handler most likely uses one or both of these.
string commandName = "command name here";
object commandArgument = <argument here>;
Button1_Click(sender, new CommandEventArgs(commandName, commandArgument));
That is assuming you call it inside Page_Load, as the post you refer to. If not, make sure to provide reference to correct control as sender.

ASP.NET Session and Page Life Cycle

Let's say that in an ASP.NET .aspx page I have the Page Load method and another method for a button click event.
In the Page Load method I'm checking if the user is logged in by checking the Session. Whether he is or not, I'm storing the result in a Global Variable.
Boolean isGuest = false;
protected void Page_Load(object sender, EventArgs e) {
if(Session["id"]==null)
isGuest = true;
else
isGuest = false;
}
Let's say 20 minutes have passed, and I don't know if the Session has terminated or not, and then I click a Button, the event goes like this:
protected void Button_Click(object sender, EventArgs e) {
if(isGuest==false)
//do something
else
// do another thing
}
My Question is : When I'm clicking the button, does ASP.NET go through the Page_Load method again (check isGuest again) or it simply executes what's inside the Button_Click method, means, it uses a Boolean isGuest that could be false, but in reality the session is terminated, means it should be true.
Page_Load is triggered always before the control events.
Have a look: ASP.NET Page Life Cycle Overview
Side-note: If you want to do things only on the first load and not on every postback you have to check the IsPostBack property.
You can:
Define your own class with the UserID, and other profile properties;
Add that class to session in the global.asax session started event Session["NameReferingToYourClass"] = new YourClass();
Set a member variable to your session object early in the page life cycle mYourClass = Session["NameReferingToYourClass"] casted if you need to;
Then you can make any changes to your class anywhere in the code your member variable is available;
Save back your class with all the changes into session on the Page.Unload(..){ Session["NameReferingToYourClass"] = mYourClass.
This way you are using your class properties in your code, including UserId, pretty much like a windows application, there will be no mentioning of session anywhere else in your code.

how to get a asp.net page event on user control

I have asp.net page which loads the user controls at run time I want to capture the save/cancel event of page on the user control.
User control will be load at run time so, for further development I don't want to change my code on page level.
Any help.
I solve it, with the help of observer pattern but with some more modification because my challenge is Page don't know which user control will be load on run-time in its place holder.
I create change-manager which holds the Observers references with their intended types(submit,cancel,viewonly)
Page holds the reference of change-manager and call the notify of change manager with (eventtype(enum),response(this.response object)) then change manager calls the update method In this method call Observers, kept in the dictionary of change-manager.
Iterate the dictionary and call their updates according to their interested event types.
Now I am free to remember the user-control reference of any type of casting.
see below example of get user control from page you need to use public modifier
///user control code see event is public
public void btn1_Click(object sender, EventArgs e)
{
///do you stuff
}
/// page code
protected void Page_Load(object sender, EventArgs e)
{
uc1.btn1_Click(sender,e);
}

How to get Calling Gridview name in ObjectDataSource Selecting event

I have 2 gridviews, gv1 and gv2 and an ObjectDataSource with the id ods1. Both the gridviews are pointing to DataSourceID="ods1".
My question is, how do I know in selecting event of an ObjectDataSource that which gridview has called ods1. I want to set input parameters based on which gridview has made a call to the ods1.
I think this is not easily possible and it feels like it would be against the idea behind ODS.
You can delegate two ObjectDataSources to get the data from THE SAME repository class but still, you need two different data sources if you want to have two different sets of parameters. Thus, you do not duplicate code as the repository code is shared between object data source instances.
Warning: Hack ahead
I tend to agree with Wiktor Zychla's answer, but if you really need to do this...
The only thing I can think of to accomplish this would be to handle the "DataBinding" event of each of your GridViews, and set a session variable to indicate which one is about to call the ObjectDataSource "Selecting" event.
So you would have your GridView methods:
protected void gv1_DataBinding(object sender, EventArgs e)
{
Session["currentGridID"] = "gv1";
}
and
protected void gv2_DataBinding(object sender, EventArgs e)
{
Session["currentGridID"] = "gv2";
}
And then, your ObjectDataSource could check that Session variable, to see which ID is in it while the ObjectDataSource is firing this time:
protected void ods1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
if(Session["currentGridID"] == "gv1")
{
}
else
{
}
}
To get the name of the gridview which call the objectdatasource
You can do something like:
string CallingGridName = ((ObjectDataSourceID)sender).ID;

Event handlers can only be bound to HttpApplication events during IHttpModule initialization

I am getting the following error
'Event handlers can only be bound to HttpApplication events during IHttpModule initialization.' at the following code (line in bold or double **)
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
**app.EndRequest += new EventHandler(Application_EndRequest);**
}
protected void Application_EndRequest(object sender, EventArgs e)
{
UnitOfWork.Commit();
}
which is mentioned in Global.asax file. Can anybody figure out, where I am lacking? Thanks.
The event handler lives the entire life of your application, so, you only need to add it once not add it every request. The event itself will fire every request, and the only-one handler will be called every time the event is raised.
Add it to Application_Start in global.asax not Application_BeginRequest, or better, create an HTTP Module instead.
Also, I think you may not even need an event handler. The method with current name will be called by convention similar to Page/Control AutoEventWireup (like Page_Load). Just note that this might have issues in ASP.NET MVC applications as some people report. So, my suggestion is to rename the function, add the event handler in Application_Start, or better in a new HTTP Module you create.
Try to comment out line marked with "**". Asp.Net will call appropriate methods by itself if followed by naming conventions: "{Scope}"_"{Event}", where "{Scope}" is Application if you want to handle application level events or "Session" if you want to handle session level events, and "{Event}" is name of the event, like Start, End, etc.
More info: http://msdn.microsoft.com/en-us/library/bb470252.aspx#Stages

Resources