ASP.net: Code explanation of delegates and events - asp.net-2.0

Given this code:
WriteThisMessageToThePage1.sendMessageToThePage += delegate(string message)
Can anyone explain me what this line means in delagates? What does this code represent?

+= is a short hand notation for subscribing to an event. The delegate(string message) code represents the event handler for the event. In this case the event handler is an anonymous method. When the sendMessageToThePage event fires, the code next += is executed.

Related

Why isn't MessagingCenter.Subscribe working?

In Xamarin forms, I Subscribe in OnAppearing and Unsubscribe in OnDisappearing. But it still calling the callback for every instance.
protected override void OnAppearing(){
if (isGoingBack)
MessagingCenter.Subscribe<PhoneNumberVerificationPajModal, string>(this, "Phone.Verify", codeSendRequest);
}
protected override void OnDisappearing(){
if (isGoingBack)
MessagingCenter.Unsubscribe<PhoneNumberVerificationPajModal>(this, "Phone.Verify");
}
Truth is calling Unsubscribe works. But, in my code I have a condition when the click on a button I open a Page. As that page is the one sending the message, when the user click the button I set isGoingBack = false. When I Press the back button and coming back to the page, I can Unsubscribe and Subscribe get called. But going to the next the callback event get call the same number of time I created a new page. But the truth is I Unsubscribe whenever I leave the page.
in your sample you are not passing the arg type to the unsubscribe method
try unsubscribing with the same type parameter you are subscribing with.
so instead of
MessagingCenter.Unsubscribe<PhoneNumberVerificationPajModal>(this, "Phone.Verify");
try
MessagingCenter.Unsubscribe<PhoneNumberVerificationPajModal, string>(this, "Phone.Verify");

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.

Update text in TextBox from Async button click

I have a process that takes a long time to finish executing, the user should be able to see a simple feedback when the process starts and finishes
something like this:
protected async void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = "Process started..\n";
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
await ProcessDelay();
sw.Stop();
TextBox1.Text += "Process finished.\n";
TextBox1.Text += "Elapsed Time (ms): " + sw.ElapsedMilliseconds.ToString() + "\n";
}
private async Task ProcessDelay()
{
await Task.Delay(5000);
}
Text value in TextBox is not updated until the execution of ProcessDelay() is done. What am I missing here?
Remember that when you're working with ASP.NET, you are working within a strict request+response paradigm. WebForms tries to hide this from you, but it's still one-response-per-request underneath. So, when you click a button in your browser, it sends a request to the web server, which executes the click code, returning a result. It can only return one result. So there's no way it can, say, make a change to part of the page and then later make another change.
To put it another way, async on ASP.NET yields to the ASP.NET runtime (that is, it returns the request thread to the thread pool). It does not yield to the browser (that is, it does not return a response).
To do what you want, you'll need an alternative technology. If the background work doesn't take too long, you could consider SignalR. Otherwise, you'll probably need a proper distributed architecture: a reliable queue connected to an independent background process. I describe a few approaches on my blog.

Is it possible to call one event to another event in 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

Best way to persist session variables on listview itemcommand ASP.NET

As the itemcommand event fires after most if not all page/control init/load events. what is the best way to persist session variable data that is modified on itemcomment (adding items for example) so that the page can react to the itemcommand using the modified session?
You could catch the postback earlier in the page's lifecycle:
// id of the control
string id = Request.Form["__EVENTTARGET"];
if (!string.IsNullOrEmpty(id) && id.Contains("myControlId"))
{
string argument = Request.Form["__EVENTARGUMENT"];
...
}
but it's neither very elegant nor safe. I would follow the suggestion of Skowronek: to put more logic on PreRender.

Resources