Cancelling TextChanged events (ASP.NET) - c#

I have a bit of an issue with something I'm working on in ASP.NET/C#. I am using a GridView to display all 'items' linked to an account, with 3 fields relating to the link, but also all files not linked to it. Next to all records is a checkbox so they can link/unlink the two, and modify the 3 fields relating to that link.
I am currently using the 'CheckedChanged' and 'TextChanged' events to update the entries, as opposed to looping through each row in the Grid, checking its current state and updating accordingly etc which would provide a lot of overhead.
Problem is,the events fire if the user clicks a 'Cancel' button. Is there a way to detect if this certain button has been Clicked, and stop the events from firing? Or do you guys have better ideas?

First of all, it sounds like your Cancel button's Clicked handler is assigned to your custom function somewhere. By default it shouldn't do anything.
However, you can use the Sender object to check the type of object that fired the event:
if (sender is Checkbox) {
// Do something useful
}
I have not tested this code.

Related

React to a checkbox has changed its state in ObjectListView

I have an ObjectListView (OLV) with 3 columns of checkboxes that enables and disables different calculations. Some react to the object in the OLV object but one checkbox column should also activate some recalculations outside the OLV.
I have looked in the cookbook, but I cant find any solution to my problem. The OLV does have CheckStateGetter and CheckStatePutter methods, but these are used during the change. I need to react after it has been changed. I also looked for a general purpose event like cellEdit, but a checkbox click is not an edit event.
Anyone know how to listen for checkbox changes after it has been done in ObjectListView?
It is not entirely clear to me what you are asking but maybe this helps.
objectListView1.SubItemChecking += delegate(object sender, SubItemCheckingEventArgs args) {
// The event arguments contain information about current check state, new check state, the source column and so on...
};
"one checkbox column should also activate some recalculations outside the OLV."
So check if the SubItemChecking source in the event arguments is the column of interest and you should get what you need.

Enforcing the order of simulanteous event calls in C#

I have a C# program that has two elements, a TreeView and a DataGridView and two EventHandelrs. When an item is selected in the TreeView, the DataGridView with a table that displays some information and can be edited by the user. Each item in the TreeView has its own unique table. When the user unfocuses on the DataGridView, the software underlying software model is updated (saving the changes the user made):
aDataGridView_Leave(object sender, EventArgs e)
{
//Update the software model (save the current DataGridView)
}
aTreeView_AfterSelect(object sender, EventArgs e)
{
//Update the table that is shown in aDataGridView
}
My problem is that if the user de-focuses on the DataGridView by selecting a different item in the TreeView I essentially have a race condition between which of these events happens first (even though this is all on one thread). Obviously, in this case I want the DataGridView_Leave Event to fire first or the changes the user made to the table get blown away by a new table being loaded before the changes are saved.
Is there anyway graceful way to enforce what event happens first?
It sounds like you can resolve this by keeping an isDirty bool that is true when there are changes that must be saved. You need to track this yourself, for example in property setters. Check isDirty in aTreeView_AfterSelect and save if necessary, and in aDataGridView_Leave so that you don't save if there are no changes.
This should work, but it sounds like your set up is a little hacky, and it may be counter-intuitive to users that changes are saved when an element loses focus.
What comes immediately to mind is some sort of counter. Assign each event call a number and store the last one processed. That way you can check if it is an old event and ignore it. I haven't seen it used in C# exactly, but plenty of times with AJAX when network delay often causes this situation.
I created a simple windows form application with a treeview, gridview and text box to log events that occur. When you leave the gridview its leave event always fires before the treeviews after select.
Are you saving the data in the gridview asynchronously? I could see if you are saving asynchronously then the treeviews after select could fire before your async method grabbed the data to save from the grid.
If this is the case, push the gridviews data to a stack and pop it off in the method that does the save.

C# - which event to populate a combobox before it's visible?

I'm currently updating it on click, but this results in the user being able to see the repopulation occur. Which other event can I use which will allow me to handle it myself, then show the combobox when i'm ready? ( after population)
I don't know what you're developing, but that combobox is probably on a window or so that will have an event that fires on show. Use that event to populate the combobox in.
[edit] Ah Winforms. Use the Load event.
[edit2] On each click eh.
Alright. I found a dirty solutions that advises you to override the WndProc and capture messages, but I think it's better to inherit the combobox and override OnDropDown to perform you populating before calling the ancestor's OnDropDown method.
You should populate the box when entered too, because a value may be selected using the keyboard (arrows) without even dropping down the box. You'll need both if you want it on each selection, because a click only causes the Enter event when the box didn't have focus before.
Have you tried the ComboBox.DropDown Event?
You could try to call SuspendLayout() before updating and calling ResumeLayout() after the changes.
combobox.add_HandleCreated triggers after the control is created as the form is loading

"press" edit button in gridview through c#

i'm doing a webshop in asp.net (c#).
Is there a way to push the edit button in the gridview through the code of c#?
I have a "new" button, that just adds the row, it would be great if that same row would "open" itself for editing without user having to press "new" then "edit"...
I know there are other ways to do this, i just want to know if this is possible... it would save tons of time!!
thanks in advance for the anwsers!!
Andrej
Just call the event handler you wrote to handle the edit button push.
Basically speaking, all that happens in code when you click a button is that the button's Clicked event is raised. In a GridView, the event is actually something like GridViewButtonClick. However many handlers you have plugged in will then execute (and you can't, and shouldn't have to, control the order of execution). For a built-in button, because you cannot raise the event from outside that button, you can simulate the button click by just calling the handlers you have attached to the event. If this were your own custom control, you could define a method you could call from outside that would cause the control to raise a certain event.

C#/ASP.NET/JS - onclick event shows row, postback hides row

rbnResubmission.Items.FindByValue("Yes").Attributes.Add("onclick", "getCheckedRadioFieldResubmission(this)");
rbnResubmission.Items.FindByValue("No").Attributes.Add("onclick", "getCheckedRadioFieldResubmission(this)");
So I have these click events for showing rows in a table - that part works fine.
Here's an example of what the code does (I'm not showing the "No" option)
function getCheckedRadioFieldResubmission(radio){
if(radio.value == "Yes"){
document.getElementById('<%=ApprovingInstituteRow.ClientID%>').style.display ="block";
document.getElementById('<%=ApprovalNumberRow.ClientID%>').style.display ="block";
document.getElementById('<%=IRBApprovalRow.ClientID%>').style.display ="block";
document.getElementById('<%=ExpectedDateRow.ClientID%>').style.display ="none";
}
}
The form needs to go through validation, and if there's any problems, because the event to show these rows only happens from "onclick" - they will disappear upon postback. what can I change to make them appear permanently?
I'm guessing you're databinding the radio button list during onLoad. You have to rebind it on postback.
You'll probably want to add the same functionality of your getCheckedRadioFieldResubmission function into the onload event of the html <body> tag. However, since the this object will refer to the body element and not the radio, you'll need to put something of this sort in there to find the radio:
var radio = document.getElementById('radio_id');
This will work if your radio list has a maintained state. As in, it keeps it's state after Post Back.

Categories