c#, listbox, stackOverflow exception - c#

I inherited KryptonToolkit ListBox control to get SelectedItemChanging event.
public class CPListBox : KryptonListBox
{
public event CancelEventHandler SelectedIndexChanging;
protected virtual void OnSelectedIndexChanging(CancelEventArgs e)
{
SelectedIndexChanging(this, e);
}
protected override void OnSelectedIndexChanged(EventArgs e)
{
CancelEventArgs cArgs = new CancelEventArgs();
OnSelectedIndexChanged(cArgs);
if(!cArgs.Cancel)
{
base.OnSelectedIndexChanged(e);
}
}
}
In form I handle event with code:
void UsersListBoxSelectedIndexChanging(object sender, CancelEventArgs e)
{
if(_presenter.CurrentUser.IsModified)
{
MessageBox.Show("Nie zapisales zmian!");
e.Cancel = true;
}
}
And I got stackOverflow ;) Exception. Maybe someone now what is wron with this code?

You're recursively calling the method in itself forever. There's no terminating condition for these recursive calls. It'll result in Stack Overflow.
protected override void OnSelectedIndexChanged(EventArgs e)
{
CancelEventArgs cArgs = new CancelEventArgs();
OnSelectedIndexChanged(cArgs); // Clearly calling yourself indefinitely.
//...
}

protected override void OnSelectedIndexChanged(EventArgs e)
{
CancelEventArgs cArgs = new CancelEventArgs();
//Next line!!
OnSelectedIndexChanged(cArgs);
if(!cArgs.Cancel)
{
base.OnSelectedIndexChanged(e);
}
}
You're calling yourself. Hence the StackOVerflow exception.

You have OnSelectedIndexChanged call within OnSelectedIndexChanged, it's an endless recursive call.

Related

Calling Page_Load on button click

Is it a good practice to call Page_Load function in another function like this. It's working but I don't know if i should do this
public bool myButtonClick(object sender, EventArgs e){
Page_Load(this, null);
}
You can do this, but you shouldn't. And it is very easy to prevent this:
In your Page_Load, call a method that does the actual work in the method now. Then call that method from myButtonClick:
public void Page_Load(object sender, EventArgs e)
{
this.SomeMethod();
}
public void myButtonClick(object sender, EventArgs e)
{
this.SomeMethod();
}
private void SomeMethod()
{
// the actual code now in Page_Load
}
You see, nice and clean and reusable too.

call private void from another private void in C#

I would like to call btnSubmit if certain conditions in axTws1_tickPrice are true. How do i do this?
private void btnSubmit_Click(object sender, EventArgs e)
{
//code here
}
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
Call butSubmit (how do i do this)
}
}
You're better off having a common method that both of your control handlers call, rather than trying to call one handler from another. That way your code is more extensible and testable, and you don't have to worry about the event arguments or senders.
For example:
private void btnSubmit_Click(object sender, EventArgs e)
{
DoStuff();
}
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
DoStuff();
}
}
private void DoStuff()
{
// code to do stuff common to both handlers
}
Multiple options.
Option 1 :
Preferred approach, move common logic to another method.
private void btnSubmit_Click(object sender, EventArgs e)
{
CommonLogic();
}
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
CommonLogic();
}
}
private void CommonLogic()
{
// code for common logic
}
Option 2:
Executing PerformClick() method which generates a Click event for a button.
btnSubmit.PerformClick();
Option 3:
Invoke the event method like any other normal method.
btnSubmit_Click(sender, new EventArgs());
just call it with current parameters.
if (Condition)
{
butSubmit(sender, null)
}
Unbelievable, but
btnSubmit_Click(null,null);
Or other arguments if needed.
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
button1_Click(sender, EventArgs.Empty);
}
}
button1_Click is similar to a normal method which accepts two inputs of type object and EventArgs So you can call them by giving the same parameters. if you are not going to use these arguments inside the method then you can call them by passing null,null don't use null if you want to use e or sender inside the method. in such situation call them as like i suggested above.
Thanks Steve and Hari - this worked well
private void btnSubmit_Click(object sender, EventArgs e)
{
DoStuff();
}
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
DoStuff();
}
}
private void DoStuff()
{
// code to do stuff common to both handlers
}

Events in Asp.Net Webforms custom UserControl

I've created a new Web User Control. This control has a button. On the click event of this button I raise an event in this way:
public partial class SearchDate : UserControl, IPostBackEventHandler
{
private static readonly object ObjEvent = new object();
// delegates
public delegate void VoidDelegate();
// events
[Category("Action")]
[Description("Azionato quando si preme il cancella nel filtro data")]
public event VoidDelegate CancelSearchDate
{
add
{
Events.AddHandler(ObjEvent, value);
}
remove
{
Events.RemoveHandler(ObjEvent, value);
}
}
protected virtual void OnCancelSearchDate()
{
var cancelEvent = (EventHandler) Events[ObjEvent];
if (cancelEvent != null)
{
cancelEvent(this, new EventArgs());
}
}
protected void BtnSearchDateCancelClick(object sender, EventArgs e)
{
OnCancelSearchDate();
}
protected void Page_Load(object sender, EventArgs e)
{
}
public void RaisePostBackEvent(string eventArgument)
{
OnCancelSearchDate();
}
}
I've created a delegate, an event and raise it on click of the button.
In another web form, where this control is hosted, i've added an handler to this event in this way:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
return;
InitDateFilter(SearchDate);
}
protected void InitDateFilter(SearchDate searchDateControl)
{
searchDateControl.CancelSearchDate += SearchDateControlCancelSearchDate;
}
void SearchDateControlCancelSearchDate()
{
// other stuff
currUtente.FiltroData = null;
SaveSession();
}
When I click to the button, it try to evaluate the event but it's always null.
I'm new in asp.net webforms and it seems to me strange that it not works well.
I've tried to raise the event in the simpler way (without the 'add' and 'remove' on the event definition), just calling the event if it isn't null but the behavior is the same.
Thanks
try to just override the OnInit event in the Page and add the handler to your event there.
like this:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
InitDateFilter(SearchDate);
}
Let me know if it helps

using PreSendRequestHeaders Event in global.asax

I tried to assign the PreSendRequestHeaders Event in the global.asax file in the "Application_Start" method. But this does not work.
private void Application_Start()
{
PreSendRequestHeaders += OnPreSendRequestHeaders;
}
private void OnPreSendRequestHeaders(object sender, EventArgs e)
{
// this is not called
}
The OnPreSendRequestHeaders is not called, why?
Is it possible to assign the PreSendRequestHeaders method in the global.asax?
Just use:
protected void Application_PreSendRequestHeaders(Object source, EventArgs e)
{
}
Or instantiate the handler:
protected void Application_Start()
{
PreSendRequestHeaders += new EventHandler(OnPreSendRequestHeaders);
}
protected void OnPreSendRequestHeaders(object sender, EventArgs e)
{
// should work now
}

page.Isvalid always return false?

I have two buttons and two separate validation groups for each button. I kept EnableClientScript=false on each requiredfield validator of the textboxes. I have c# code like below
void submitButton_Click(object sender, EventArgs e)
{
this.Page.Validate("LoginAccountGroup");
if (this.Page.IsValid)
{
}
}
void saveButton_Click(object sender, EventArgs e)
{
this.Page.Validate("CreateAccountGroup");
if (Page.IsValid)
{
}
}
My question is this Page.Isvalid is always returning false in the c# code. How to make it work
Try This
protected bool IsGroupValid(string sValidationGroup)
{
Page.Validate(sValidationGroup);
foreach (BaseValidator validator in Page.GetValidators(sValidationGroup))
{
if (!validator.IsValid)
{
return false;
}
}
return true;
}

Categories