I am busy having a problem with allocating a new event to an existing button that was created in the designer.
Now here is the button that was created prior to runtime which is inside the modalpopupexteder5 -
<asp:Button runat="server" ID="btnClose" Text="Close" OnClick="btnClose_Click" />
Here is the codebehind -
protected void btnAddAccount_Click(object sender, EventArgs e)
{
btnClose.Click -= new EventHandler(btnClose_Click);
btnClose.Click += new EventHandler(btnCancel_Click);
ModalPopupExtender5.Show();
}
protected void btnCancel_Click(object sender, EventArgs e)
{
ModalPopupExtender11.Hide();
}
protected void btnClose_Click(object sender, EventArgs e)
{
ModalPopupExtender5.Hide();
}
So the button in the beginning has the event btnClose_Click hooked up to it. But I want to change it to the btnCancel_Click
But it wont execute the btnCancel_Click. It executes the original btnClose_Click
Any Ideas what could of caused this?
Does this relate to the page life cycle?
--EDIT--
I should let you know that the btnAddAccount_Click does get executed.
Basically I don't to create the same modalpopupextender, I want to use the existing one but depending on user selection will determine which eventhandler should be called and in this case the btnAccount_Click has selected the btnCancel_Click to be assigned to the button.
ASP.NET is not assigning the Click events anywhere for the server side buttons. Those buttons are rendered as plain submit buttons, and the internal code behind is checking the submitted value then based on that it finds the "clicked" button and calls the proper handler.
This means your current logic is leading to a dead end.
Instead of messing with the events, have btnClose_Click as the handler, and in there check the currently active/visible popup and hide it.
I am not quite sure what exactly you are asking, but you can add a Handles to your event and so control the events on your buttons.
Something like:
protected void btnCancel_Click(object sender, EventArgs e) Handles btnCancel.Click, btnClose.Click
{
//do stuff
}
Related
In a C# VSTO Ribbon, you can design and place buttons and you can program whatever you want each button to do i.e. an action that will occur on each individual button click event can be defined by the programmer.
I have some code I would like to perform every time a button was clicked -
regardless of which button was clicked. A simple example includes measuring the time that the button click event was received: I want this for all my buttons, so, I am looking for a general / abstract event handling all button clicks or something of the sort rather than each event one by one. Does this exist in VSTO or is there a clean implementation to it?
Here is a simple illustration of the problem in code.
I have 3 buttons: btn1, btn2 and btn3. And I have defined what I will do for each click as follows:
private void btn1_Click(object sender, RibbonControlEventArgs e)
{
//same code
//btn1 specific code
}
private void btn2_Click(object sender, RibbonControlEventArgs e)
{
//same code
//btn2 specific code
}
private void btn3_Click(object sender, RibbonControlEventArgs e)
{
//same code
//btn3 specific code
}
The problem is clear: there is part of the code that I want to do that is the same for each click. So is there a general click event where I can define the same code only just once?
I have "inherited" a project in ASP.NET with C# and I know for sure a certain stored procedure is associated with pressing a button called button_Search (I have confirmed this also with SQL Server profiler).
However, the method associated with clicking the button is empty:
protected void button_Search_Click(object sender, EventArgs e)
{
}
Where else could that behaviour (running that stored procedure that returns a select which is then tied to a gridview) be implemented in ASP.NET, if not in the button_Search_Click?
Thank you in advance.
protected void Page_Load(object sender, EventArgs e){
if (IsPostBack){ //confirms it's a PostBack and not initial load
Button myButton = (Button)(sender as Page).FindControl("button_Search"); //find your button
if (myButton.ID == "button_Search"){
// your normal code (the code you intend in button_Search_Click )goes here...
}
}
}
This could be any number of method/functions depending on the project structure.
Does the aspx page contain something like the code below?
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="JavacriptClick()" OnClick="Button1_Click" />
The OnClick function would specify a method in the codebehind file OnClientClick would be a javascript function which would imply an ajax call.
If none of the above are present the search button could be being used to trigger a postback and the logic is somewhere in the page life cycle. It is not unusual to find some logic in the PageLoad method with in an if IsPostback statment.
With regards to the stored procedure being activated/populating the gridview check if the gridview has an SqlDataSource/ObjectDataSourcemore information can be found at https://msdn.microsoft.com/en-us/library/aa479341.aspx for further details.
protected void Page_Load(object sender, EventArgs e){
//Please check for below line code
this.button_Search.clicked += CallStoredProc;
}
protected void CallStoredProc(object sender, EventArgs e){
// Here the SP could be called
}
Please check out for such code
I have a Winforms c# form with some comboBoxes , cancel and save buttons that work fine.
I now need to capture when the user has finished entering text into a comboBox.
I add an empty ( for now) lostFocus (or Leave) event to the combbox , which triggers fine. However if the cause of that event was a cancel or save button press , the corresponding event is no longer triggered. These buttons still work fine if pressed at other times.
Should these two event be firing in sequence or is there some better way to capture completed text entry?
The Leave and/or LoseFocus events do not get triggered because you do not leave the combobox and because it doesn't lose focus when you press Enter or Escape.
Therefore the best way is to add the function you are triggering in the LoseFocus event, also to the Button click events of the Cancel- and the Accept-Buttons.
Adding a call to the leave event itself: comboBox1.Leave(null, null); would be the simplest way.
To make sure that the function is called only once, I check who has focus in the ButtonClick events:
private void acceptButton_Click(object sender, EventArgs e)
{
if (comboBox1.ContainsFocus) comboBox1_Leave(acceptButton, null);
// do accept stuff here..
}
private void cancelButton_Click(object sender, EventArgs e)
{
if (comboBox1.ContainsFocus) comboBox1_Leave(cancelButton, null);
// do cancel stuff here..
}
private void comboBox1_Leave(object sender, EventArgs e)
{
// do leave stuff here..
Console.WriteLine(sender);
}
I also pass in the Button so you could check the sender to see how the Leave was triggered..
I'm answering my own question here as I feel it might be useful to other newbies.
The breakpoint I had set in my empty lostFocus event was stopping button click event from occurring. When I removed the breakpoint the problem went away.
However when I added code to my lostFocus event, a form redraw was sometimes moving the buttons and preventing their events from firing. To solve this problem I adapted TaWs very useful answer and fired the button event from within the lostFocus event.
private void comboBox1_LostFocus(object sender, EventArgs e)
{
bool saving = btnSave.ContainsFocus;
// form redraw stuff here..
if (saving)
btnSave_Click(btnSave, null);
}
There are similar questions around here but none that fit into my my particular case scenario.
I have a Windows Form with a Button. The button is attached to the event handler as follows:
private void mybutton_Click(object sender, EventArgs e)
{
// do some processing here
}
In addition there is a combobox where a change in selection in supposed to trigger the button event handler as defined above.
private void mycombobox_SelectedIndexChanged(object sender, EventArgs e)
{
mybutton_Click(sender, e); // this is the line which pops up the dialog
}
The code works exactly as intended at runtime but i get a dialog prompt at compile time which reads:
object reference not set to an instance of an object
There are no other errors or warning.
A google search tells me that this message is an error caused if the program is trying to access a member of a reference type variable which is set to null.
However when i run this code in debug mode, both the sender and event(e) variables are not null.
So why is this dialog popping up ?
And if this had been an error or warning - it should have shown as an error or warning but nothing of that sort happens.
Here's the screenshot:
Edits: Answering Questions Raised in Comments
There are no errors as you can see in the screenshot.
The program works great - just this pop up
The popup is caused by the line:mybutton_Click(sender, e); in the combobox selectedIndexChanged function.
The mybutton_Click(sender, e) does not use any of the arguments sender or e in the processing.
I have not installed any VS extensions either.
It is not a good design to call the Click-event of the button in the SelectedIndexChanged-event of the ComboBox and this might also be the reason for the error.
Better put your logic in a seperate method and call it in the Click- and the SelectedIndexChanged-event like this:
private void UpdateSomething()
{
// Do whatever you want
}
private void mybutton_Click(object sender, EventArgs e)
{
UpdateSomething();
}
private void mycombobox_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateSomething();
}
I think what is happening is that the events are firing in Design mode (which I seem to recall happening to me a few times when using WinForms back in the day).
To get around it what I did was handled the form load event, then in that I attached a listener to the SelectedIndexChanged. Then in Design mode the event isnt bound and wont fire, but at run time it is bound.
Something like:
public void form_OnLoaded(object sender, EventArgs e)
{
myComboBox.SelectedIndexChanged += mycombobox_SelectedIndexChanged;
}
Actually i am dynamicaly creating two textboxes and two buttons inside a table using for loop. Now i want to write event handlers for these 2 buttons so that upon clicking on the button the text inside the respective texbox should be displayed in a new label. Also tell me why upon clicking the button after postback all the dynamic controls disappear. Kindly explain with some good example.
protected void Page_Load(object sender, EventArgs e)
{
.
.
Button Button1= new Button();
Button1.ID = "button1";
Button1.Text = "Button";
Button1.Click+=new EventHandler(Button1_Click);
this.form1.Controls.Add(Button1);
.
.
}
and handler method goes like this
protected void Button1_Click(object sender, EventArgs e)
{
//
}
The reason why after postback all the buttons disappear is, these controls are not created again in Page_Load event.
These controls were not in page markup initially, and while postback, dynamically created markups will not be retained due to stateless transfer
Go through ASP.NET Page life cycle for more information