i have following direct event Handler in Code-Behind:
public void changeBlock(string blockname)
{
IntraNetEntities ent = new IntraNetEntities();
var query = from x in ent.Mailings_blocked where x.blocked == blockname select x;
if (query.Count() == 0)
{
Mailings_blocked add = new Mailings_blocked();
add.blocked = blockname;
ent.AddToMailings_blocked(add);
}
else
{
Mailings_blocked del = ent.Mailings_blocked.Single(c => c.blocked == blockname);
ent.DeleteObject(del);
}
ent.SaveChanges();
updateStore();
Grid_Business.RefreshView();
Grid_Mailing.RefreshView();
}
But when the Event is Triggered, the database gets updated, just like the store, but the RefreshView() is not correctly executed. when clicking any other Button for the same utility (this is a rowCommand) the view gets updated to desired state of one click earlier.
well i did not find out why, but i found out how to fix:
<DirectEvents>
<Command OnEvent="GridCommand" Success="GridID.reload(); GridID.render();">
</DirectEvents>
Calling the reload and render to after will not necessarily help, if the response time is high, because then after gets fired before success
Related
I'm currently trying to figure out how to get the previous Selected Item in a Combobox, the data is added in a list in the Form1_Load function.
//Flavour Change Button
private void CoboFlav_SelectionChangeCommitted(object sender, EventArgs e)
{
var selectedItemPrice = (coboFlav.SelectedItem as Flavour).price;
var selectedItemName = (coboFlav.SelectedItem as Flavour).name;
var pre_item = pre_selIndex = (coboFlav.SelectedItem as Flavour).price;
//var previousItem = flavourTea_Previous_Var = (coboFlav.SelectedItem as Flavour).price;
//Item List
Flavour listItem1 = ListCopy.MyList2.Find(x => (x.name == "- None -"));
Flavour listItem2 = ListCopy.MyList2.Find(x => (x.name == "Lemon"));
Flavour listItem3 = ListCopy.MyList2.Find(x => (x.name == "Passionfruit"));
Flavour listItem4 = ListCopy.MyList2.Find(x => (x.name == "Yogurt"));
//Checking Base Tea Box for adding price to currentItemTotal
if (coboFlav.Text == listItem1.name || coboFlav.Text == listItem2.name || coboFlav.Text == listItem3.name || coboFlav.Text == listItem4.name)
{
//Increment Item Cost Value & take away previous item cost
currentTotalItemCost += selectedItemPrice - pre_item;
}
//Update CUrrentTotal Text
CurrentTotal.Text = currentTotalItemCost.ToString();
}
If the user selected an option in the combobox the selectedPrice int is increased. I am trying to take away the previousItemCost and im having trouble understanding how to find the previous selected user input.
I am not really sure how to approach this, I have seen a couple of other people declare a new int as -1 and set the SelectedIndex to that. But I don't really understand that solution. If someone could point me in the right direction that would be awesome. Also I am quite new to windows forms as I came from a Unity background.
Thanks
Apparently you want a special kind of ComboBox. In your "object oriented programming" course you learned that if you want a class similar to another class, but with some special behaviour, you should create a derived class:
class PreviousSelectedComboBox : ComboBox // TODO: invent proper name
{
private int previousSelectedIndex = -1;
[System.ComponentModel.Browsable(false)]
public virtual int PreviousSelectedIndex {get; private set;}
{
get => this.previousSelectedIndex;
set => this.previousSelectedIndex = value;
}
[System.ComponentModel.Browsable(false)]
public override int SelectedIndex
{
get => base.SelectedIndex;
set
{
if (this.SelectedIndex != value)
{
this.PreviousSelectedIndex = this.SelectedIndex;
base.SelectedIndex = value;
// TODO: call OnSelectedIndexChanged?
}
}
}
}
Test In a dummy test program or a unit test, check if ComboBox.OnSelectedIndexChanged is called by base.SelectedIndex, If not, call it in the SelectedIndex.Set.
Also check what happens if ComboBox.SelectedItem.Set is called. Does this change the selected index by calling your overridden property SelectedIndex?
Event: I don't think that you need an event PreviousSelectedIndexChanged. It won't add anything, because this event is raised whenever event SelectedIndexChanged is raised, so those who want to get notified when PreviousSelectedIndex changes, could subsbribe to event SelectedIndexChanged.
Still, if you want such an event, follow the pattern that is used in property SelectedIndex and in OnSelectedIndexChanged.
In my activity I am dynamically creating controls based off of sqlite data. Each item will have a button with a click event that needs to send that rows ID to the new activity.
I have looked at the following page and can get this to work on a button that already exists in my layout. But it doesn't seem to work when the button is dynamically created. Is there something additional I need to do with the button for this to work?
https://developer.xamarin.com/recipes/android/fundamentals/activity/pass_data_between_activity/
Here is the code that creates the button dynamically:
foreach (Tasks item in table)
{
TableRow row = new TableRow(this);
TextView txtTask = new TextView(this);
txtTask.Text = item.Name;
row.AddView(txtTask);
tableLayout.AddView(row);
row = new TableRow(this);
Button btnEdit = new Button(this);
btnEdit.Text = "Edit Record";
btnEdit.SetWidth(300);
btnEdit.Click += delegate
{
Intent viewTask = new Intent(this, typeof(UpdateTaskActivity));
viewTask.PutExtra("TaskId", item.Id);
StartActivity(viewTask);
};
row.AddView(btnEdit);
tableLayout.AddView(row);
}
In the OnCreate method of UpdateTaskActivity I have:
string test = Intent.GetStringExtra("TaskId") ?? "error";
if (test != "error")
{
//Do Stuff
}
But when I put a breakpoint down, my string is always null. I did put a breakpoint to make sure the correct ID is being pulled.
Why does this work with a built in button but does not work with my dynamic one?
Just to avoid confusion, in my startup screen I have a test button, and my main activity has the following code for that button. This code works fine, because the button isn't dymaically created:
//Test button
Button btnTest = FindViewById<Button>(Resource.Id.btnTest);
btnTest.Click += delegate
{
var activity2 = new Intent(this, typeof(UpdateTaskActivity));
activity2.PutExtra("TaskId", "1");
StartActivity(activity2);
};
The main issue is that you are adding an int to the extras:
// item.Id is an int type
viewTask.PutExtra("TaskId", item.Id);
And then you are trying to get it as a string:
var test = Intent.GetStringExtra("TaskId");
There are two ways to get the value, without having to add the value as a string. Either get an int value:
var test = Intent.GetIntExtra("TaskId", 0);
if (test != 0)
{
// Do Stuff
}
Or, you can first check for the extra, if you don't want to rely on the default value:
if (Intent.HasExtra("TaskId"))
{
var test = Intent.GetIntExtra("TaskId", 0);
// Do Stuff
}
Using Kendo in an MVC application. I have a grid that shows a few rows. Some are editable, some are not. Some are deletable, some are not.
So, I defined events for this in the MVC Razor layout:
.Events(o => {
o.Edit("onGridEdit");
o.Remove("onGridRemove");
})
Then I defined JavaScript to handle this. My first issue is that the deletion event fires after delete. I need to fire beforehand and prevent it. I also want to prevent the confirmation popup.
The edit event behaves as expected. But it still does not work. Here's what I did:
function onGridEdit(e) {
var grid = $("#grid").data("kendoGrid");
var canEditLine = #(someObj.SomeProperty ? "true" : "false");
if (!canEditLine) {
grid.cancelRow();
alert("You do not have rights to modify this line.");
}
}
The result is that the cell being edited is locked, and the user never sees it opened. The alert fires as expected. The problem is that the entire original row gets rendered inside this column rather than this single column getting updated. Here's a screenshot:
Can anyone help with this? I don't want to cancel all row changes; just the row the user is trying to mess with.
EDIT:
Based on the answer below, here's the updated JavaScript code. The styles and databound events below I used as indicated.
function onUserAssignGridDataBound(e) {
var grid = this;
var canDelete = #(userRights.CanDeleteLockedLines ? "true" : "false");
var canEdit = #(userRights.CanEditLockedLines ? "true" : "false");
grid.tbody.find(">tr").each(function () {
var dataItem = grid.dataItem(this);
if (dataItem) {
if ((dataItem.IsReceived) && (!canDelete)) {
$(this).find(".k-grid-delete").css("visibility", "hidden");
}
if ((dataItem.IsLocked) && (!canEdit)) {
dataItem.fields["Description"].editable = false;
dataItem.fields["Quantity"].editable = false;
dataItem.fields["Price"].editable = false;
}
}
});
}
Hide the remove button bases on grid data on databound event
**********************Grid*******************************
.Events(e => e.DataBound("onUserAssignGridDataBound"))
*************************script****************************
function onUserAssignGridDataBound(e) {
var grid = this;
grid.tbody.find('>tr').each(function () {
var dataItem = grid.dataItem(this);
if (dataItem != null) {
if (dataItem.Role.RoleId != 2) {
$(this).find('.k-plus').addClass('hideCell');
}
}
});
}
****************************Styles CSS**********************
.hideCell {
visibility:hidden;
}
.showCell {
visibility:visible;
}
I have a form that I would like to reuse for both adding a new record and editing an existing record. I am able to successfully load the page with the relevant data when I select a record from a GridView and I am able to update the db record appropriately. However, my issue is trying to use the form for both executions. Here is my logic in code behind: (I assign a session variable when I click on the row in GridView and this does work successfully)
protected void Page_Load(object sender, EventArgs e)
{
resultOutput.Visible = false;//Output results as to whether or not a record was added successfully is automatically hidden at page load
//Checking to see if session variable has been created
if (Session["editID"] != null)
{
//Create objects to get recipe data
dbCRUD db = new dbCRUD();
Recipe editRecipe = new Recipe();
//Grabbing session ID and assigning to a variable in order to remove the session variable
var id = Convert.ToInt32(Session["editID"]);
Session.Remove("editID");
//Call method to retrieve db data
editRecipe = db.SelectRecord(id);
//Populate results to text boxes
recordID.Text = id.ToString();
addName.Text = editRecipe.Name;
addTypeDDL.SelectedValue = editRecipe.Meal;
addDifficultyDDL.SelectedValue = editRecipe.Difficulty;
addCookTime.Text = editRecipe.Cook_Time.ToString();
addDirections.Text = editRecipe.Directions;
//Change Button Text
submitRecord.Visible = false;
changeRecord.Visible = true;
//Change Title Text
addEditTitle.Text = "Edit Recipe";
}
}
protected void submitRecord_Click(object sender, EventArgs e)
{
//Variables for execution results
var modified = "";
int returned = 0;
//Creating the recipe Object to pull the values from the form and
//send the recipe object as a parameter to the method containing insert stored procedure
//depending on Add or Edit
Recipe recipe = new Recipe();
recipe.Name = addName.Text;
recipe.Meal = addTypeDDL.Text;
recipe.Difficulty = addDifficultyDDL.Text;
recipe.Cook_Time = int.Parse(addCookTime.Text);
recipe.Directions = addDirections.Text;
//Creating object to access insert method
dbCRUD newRecord = new dbCRUD();
//Checking to see if the page is loaded for edit or new addition
if (Session["editID"] != null)
{
//If recordID exists, recipe will be passed as to UpdateRecord method
recipe.Recipe_ID = int.Parse(recordID.Text);
returned = newRecord.UpdateRecord(recipe);
modified = "has been edited.";
Session.Remove("editID");
}
else
{
//If recordID does not exist, record will be passed to InsertRecord method (new recipe)
returned = newRecord.InsertRecord(recipe);
modified = "added";
}
//Method returns 0 if successful, 1 if sql error, 2 if other error
if (returned == 1)
{
resultOutput.Text = "There was an sql exception";
resultOutput.Visible = true;
}
else if (returned == 2)
{
resultOutput.Text = "There was a non sql exception";
resultOutput.Visible = true;
}
else
{
resultOutput.Text = "\"" + addName.Text + "\" recipe " + modified;
resultOutput.Visible = true;
}
}
I have issues on the if(Session["editID"] != null) line - I am always moved to the else logic and the if logic never runs.
Here is my click method in the GridView:
protected void Grid_Recipe_SelectedIndexChanged(object sender, EventArgs e)
{
int index = Convert.ToInt32(Grid_Recipe.SelectedDataKey.Value);
Session["editID"] = index;
Response.Redirect("addRecord.aspx");
}
My question is how can I control execution during the submitRecord_Click event so that I call the appropriate method. Thanks!
Have you considered using
if(Page.IsPostBack)
{
code here
}
To detect whether you are posting back to the page? Then you could check your value of the item. I see no reason the code shouldn't be in the Session variable - have you tried putting a breakpoint in there to see if the code actually gets in there?
Also does your addRecord.aspx just add the record? If so, just add the record in this class, but use the PostBack check to see. Could you just make sure you are saving in the right context aswell:
// Outside of Web Forms page class, use HttpContext.Current.
HttpContext context = HttpContext.Current;
context.Session["editID"] = index;
...
int Id = (string)(context.Session["editID"]);
I was able to figure out my issue - which actually turned into two issues. First, I had to put my Page Load logic in a if(!IsPostBack) statement because I could not edit the page. Reason being is I was loading the originally posted data to the form on page load, which executed before my logic. Adding the if(!IsPostBack) control statement fixed this issue. From there, I'm still using a session variable to control code behind logic, only I made sure keep my session variable only between the form and the gridview. Basically, when the gridview loads and it is not a post back, the session variable is cleared. This let's me set a new session variable when I click on a row and then the session variable is cleared once I return to the grid to see the results. Thanks for the help!
When the DataGridView in my application is populated, the following method is fired:
public void OrderSelectionChanged()
{
ConfirmOrCancelChangesDialog();
// Get values from selected order and populate controls
if (view.OrderTable.SelectedRows.Count != 0)
{
OrderViewObject ovm = (OrderViewObject)view.OrderTable.SelectedRows[0].DataBoundItem;
selectedOrder = orderModel.GetOrderById(ovm.OrderId);
// Populate view controls with data from selected order
view.OrderID = selectedOrder.Id.ToString();
---->> view.OrderDateCreated = selectedOrder.DateCreated; <<-----
view.OrderDeliveryDate = selectedOrder.DeliveryDate;
PopulateOrderAddressControls(selectedOrder.Address);
PopulateOrderItemTableControl();
PopulateOrderWeightAndSumControls();
view.OrderNote = selectedOrder.Note;
// Enable buttons
view.DeleteOrderButtonEnabled = true;
view.NewOrderItemButtonEnabled = true;
}
else
{
view.DeleteOrderButtonEnabled = false;
}
}
For some reason, the "isSaved" variable is being changed from true to false at the row I marked with arrows and I can't figure out why. This is not supposed to happen and was never an issue before, but suddenly appeared.
The variable "isSaved" is being checked in the following method:
public void ConfirmOrCancelChangesDialog()
{
if (!isSaved)
{
DialogResult dialog = MessageBox.Show(Properties.Resources.SaveChanges,
Properties.Resources.SaveChangesTitle, MessageBoxButtons.YesNo);
if (dialog == DialogResult.Yes)
{
SaveOrder();
}
else
{
UndoChanges();
}
}
}
This is causing the save or cancel dialog to appear everytime the application is started, which obviously is wrong. Since the selection changed method is fired three times and isSaved is changed during the first run, the dialog pops up during the second time around. Through debugging step by step I could figure out at what point isSaved is changing, but not how or why.
View is the form, OrderDateCreated is a getter/setter for a DateTimePicker, selectedOrder is just an order object and DateCreated a Date. Am I missing something here?
Cheers!
It seem related to your code setting values to view object.
If you didn't create properties by your own, that object may have set properties which detects any change and setting the isSaved properties to false.
Try this workaround:
bool wasSaved = isSaved; //reference properly to your isSaved variable, and store it in the wasSaved local var
view.OrderID = selectedOrder.Id.ToString();
view.OrderDateCreated = selectedOrder.DateCreated;
view.OrderDeliveryDate = selectedOrder.DeliveryDate;
isSaved = wasSaved; //revert to the previous state