I have a gridview with a button (Template field/LinkButton) that loads another control, but I was getting a runtime error when clicking the button until I added the following:
protected void gvLoans_RowEditing(object sender, GridViewEditEventArgs e)
{
}
Why? I don't want to allow editing. Am I missing something?
You most likely have the event handler assigned in the markup (aspx/ascx). If you remove the event handler assignment you can remove the event handler in the code behind.
Also, make sure you are not enabling the built in editing functionality which could happen if you provide a CommandButton with a CommandName of "Edit".
Related
I want to run code in a button event handler but the if(Page.IsPostBack) conditional is running first and contains a redirect so the event never runs.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
(do some stuff with save button pressed...)
// then return the same page to prevent another post on refresh
Response.Redirect(Request.Url.AbsoluteUri);
}
}
and on that page there is another button that calls a handler:
protected void Export_Click(object sender, EventArgs e)
{
(do other stuff..)
}
Is there a way to run the handler first or a way to check which button was clicked so I can add a conditional to the redirect?
Thanks.
This behavior is actually by design in WebForms. To keep things stateless, a full postback occurs whenever you wish to fire off a server-side event.
So the short answer is no, you cannot force your event to occur before the Page_Load does.
However, you can restructure your design a bit to get the behavior you desire. At the moment, it looks as though you are depending on a PostBack having a occurred to execute your save logic. Instead of handling the logic in your Page_Load event, it could be handled in an event appropriate to how your user is expecting to save.
If saving is triggered through a button press, then moving logic for handling the save and redirect to a Save_Click event will allow other events related to PostBack to execute their own logic as well.
I have a WinForm which contains a multitude of controls interdependent on each other for their visibility and content.
I have a pair of radio buttons, controlling a combobox's (ComboBoxA) enable/disable flag and content. The selection on this combobox controls the visibility of a checkbox. The checking of this checkbox controls another combobox's (ComboBoxB) visibility and content. Business requirements are quite complicated around these controls. As a result, I require the ability to fire of the events programmatically and through user action, doing different things in each case.
In the checkbox's case, I check it programmatically while loading data (if needed), which fires the CheckedChanged event which in turn does additional action controlling ComboBoxB. The code for this is pretty vanilla, nothing special, but my question is more theoretical than practical. Please keep reading.
Due to this requirement, I need a way to distinguish between programmatic checking and user action. I tried using the Click event and CheckedChanged event, setting a flag in the click event, signifying user action. Unfortunately, the CheckedChanged event fires before the Click event, dead-ending this trick.
Now, I tried using the MouseDown event to capture user action. But funnily enough, once the event fires, checkbox remains unchecked and the CheckedChanged event doesnt fire.
Now, I have managed to use a flag in the code to determine programmatic checking and use that to distinguish between the two, but I was curious as to why the MouseDown event didnt allow the checkbox to be checked. Any ideas? I searched online but either I didnt do a thorough job of it, or google is not returning the right results for me. I apologize if anybody is actually able to find a google result for this problem.
It's something else in your code, not the MouseDown event that's preventing the CheckChanged to be fired.
Here is how I know this:
I've added a checkbox and a button to an empty form, and added event handlers to Click on the button, and on the checkbox CheckedChanged, KeyDown and MouseDown events. I've also added to the form a string variable called LastEventRaised, and in the CheckedChanged I've simply shown a MessageBox:
string LastEventRaised = string.Empty;
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
MessageBox.Show("Checked changed " + LastEventRaised);
LastEventRaised = string.Empty;
}
private void checkBox1_KeyDown(object sender, KeyEventArgs e)
{
LastEventRaised = "KeyDown";
}
private void checkBox1_MouseDown(object sender, MouseEventArgs e)
{
LastEventRaised = "MouseDown";
}
private void button1_Click(object sender, EventArgs e)
{
LastEventRaised = "programmatically";
checkBox1.Checked = !checkBox1.Checked;
}
Each time the message box popped up I've got the correct message.
I wanted to create a "Click" event for a textbox in C# (as there isn't any).
So, this way
protected void Page_Load(object sender, EventArgs e)
{
if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "txt1OnClick")
{
txt1_Click();
}
txt1.Attributes.Add("onclick", this.Page.ClientScript.GetPostBackEventReference(txt1, "txt1OnClick"));
}
private void txt1_Click()
{
ImageMap1.ImageUrl = "guide/1.jpg";
}
Then I wanted to load the image without reloading the page.
So I used the AJAX UpdatePanel Control and this worked fine with
protected void Button1_Click(object sender, EventArgs e)
{
ImageMap1.ImageUrl = "guide/1.jpg";
}
But not with the event I created, because the compiler doesn't identify my new events as
a real event or something I couldn't figure out.
I added the button1_click event according to Step 8 of "Refreshing an UpdatePanel Control with an External Button".
The click event of textbox is not shown in this option:
So my question is is there any way to add this event within System.Web.UI.WebControls.TextBox class or, to make this event visible within the above option?
So that I can include click event of the textbox within the Triggers of the update panel.
If you try to create a Click event for a TextBox, every time a user clicks your textbox you'll trigger a postback to the server (even to evaluate if you need to do something as part of handling the event). This is very inefficient - you should handle clicks in the browser, using JavaScript and then trigger the UpdatePanel using client-side logic.
This lets you trigger a call to the server if you need it but avoid it when you don't. If you have a server-side event handler, your code will post back to the server (reloading the page) every time the user clicks the TextBox.
You can read this link (and others) about using __doPostBack() on the client side to trigger an UpdatePanel to perform a postback.
http://encosia.com/easily-refresh-an-updatepanel-using-javascript/
When I double click on a button (myButton) in Design view of a .aspx web form, an event handler is automatically generated in the code behind: protected void myButton_Click(object sender, EventArgs e)
Now if I understand correctly, in order to associate that method with the Button.Click event, somewhere there has to be something like: myButton.Click += new EventHandler(this.myButton_Click);
However, I can't seem to find that anywhere. I've used Ctrl+F for the entire solution and I've checked the mywebform.aspx.designer.cs.
At first I thought it was because the .aspx page's AutoEventWireup was set to true. However, even after making AutoEventWireup false, the button still responds to being clicked by running the code in protected void myButton_Click(object sender, EventArgs e)
I understand that you shouldn't mess with generated code, and I don't intend to, I just want to know more about how this is working under the hood.
The assignment of the event handler is actually done in the asp markup. Here's a link to a bunch of different properties that can be declaratively assigned to your button.
Here's another MSDN link about using the OnClick attribute.
I'm guessing that your ASP markup for the button has the following property assigned:
OnClick="myButton_Click"
As for how it gets translated into an assignment, the page gets compiled at runtime upon the first time it is requested (ASP.NET Compilation Overview).
I am having trouble getting my application to work correctly. I am trying to select a row in a datagridview with the mouse. I need to save the index of this row to allow me to navigate around the selected row.
I have been looking at DataGridView.CellMouseClick Event (Link) But I am unable to ensure that the event handler is associated with the CellMouseClick event.
My code for this so far is simple, Im just trying to see if its detecting mouse clicks:
public event DataGridViewCellMouseEventHandler CellMouseClick;
private void DataGridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e)
{
MessageBox.Show("Mouse clicked in the datagridview!");
}
Can anyone point out where I may be going wrong. Any help would be great!
You need to "wire up" the event.
If your DataGridView is called DataGridView1 then you need the following line of code in either the constructor for your form, the designer (if you add the event handler via the designer) or in the Load event:
DataGridView1.CellMouseClick += DataGridView1_CellMouseClick;
This attaches the event handler in your code to the event.
Your current sample looks like this:
public event DataGridViewCellMouseEventHandler CellMouseClick;
private void DataGridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e)
{
MessageBox.Show("Mouse clicked in the datagridview!");
}
There is no need to redeclare the event (public event DataGridViewCellMouseEventHandler CellMouseClick;) unless you are building your own user control that will host a DataGridView and you effectively want to "wrap" or "rebroadcast" that event.