I have a formview control, and on the ItemCreated event, I am "priming" some of the fields with default values.
However, when I try to use the formview to insert, before the ItemInserting event gets called, for some reason it calls ItemCreated first. That results in the fields being over-written with the default values right before the insert happens.
How do I get it to not call the ItemCreated event before the ItemInserting event?
you need to use formview Databound event instead of formview ItemCreated event to set values, try like
protected void frm_DataBound(object sender, EventArgs e)
{
if (frm.CurrentMode == FormViewMode.Edit)//whatever your mode here is.
{
TextBox txtYourTextBox = (TextBox)frm.FindControl("txtYourTextBox");
txtYourTextBox.Text// you can set here your Default value
}
}
Also check this thread of similare issue
FormView_Load being overwritten C# ASP.NET
You cannot change the order in which the events fire. However, you should probably wrap the code that sets the default values inside !IsPostBack so that it doesn't reset your values for example:
protected void FormView_ItemCreated(Object sender, EventArgs e)
{
if(!IsPostBack)
{
//Set default values ...
}
}
Try checking the CurrentMode property of the form view.
void FormView_ItemCreated(object sender, EventArgs e)
{
if (FormView.CurrentMode != FormViewMode.Insert)
{
//Initialize your default values here
}
}
Related
I have a question about C# repeater. I have default width setting, and it will change base on some conditions in the Page_Load, I want change to be pass to my Image on OnItemDataBound. However, it seems that the OnItemDataBound is firing off before Page_Load because I changed the width to 700 in Page_Load, but when the image is loaded, it is always showing 380 instead. If OnItemDataBound is not the correct function to use, which function should I call so that I can change the image width after the Page_Load (where the custom width is set) is called? I tried OnPreLoad, OnLoad, and none of them worked.
protected int width = 380;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
width = 700;
}
}
protected void Test_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
{
Image Image = (Image)e.Item.FindControl("Image");
Image.ImageUrl = Utilities.generateImage();
Image.Width = width;
}
}
Databinding is done on PrerenderComplete event, which is fired on page lifecycle before PageLoad. For more info check https://msdn.microsoft.com/en-us/library/ms178472.aspx#lifecycle_events to see lifecycle events and their order.
If you declare the datasource in markup, it can render everything much earlier since you are not doing a manual databind. This can occur previous to Page_Load.
Try overloading an earlier event, such as OnLoad or OnPreLoad. Both of these occur prior to Page_Load.
If you are explicitly performing databinding and doing it in another event that occurs prior to Page_Load, then you'll have to ensure that the repeater is rebound if you want to change things. Once you call databind, it binds. If you need to change something either do it before or rebind.
You can use Page_Init for this. It fires before ItemDataBound:
protected void Page_Init(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// load
}
}
See also:
https://learn.microsoft.com/en-us/previous-versions/aspnet/ms178472(v=vs.100)
Is there an event or command I could use so I can invoke the object that is getting added to the ObservableCollection before it gets added?
At the moment, once the user clicks the row in the grid, it adds it to the collection, however I need to specifically assign properties in C# that I don't want to assign in the grid.
public void event
{
// I want to do something before the CanUserAddRow event does this
collection.Add(<T>;
}
You can use DataGrid.InitializingNewItem event:
private void InitializingNewItem(object sender, InitializingNewItemEventArgs e)
{
//use e.NewItem here
}
From MSDN
You can set default values for the new item by handling the InitializingNewItem event and setting the values programmatically
I'm not exactly sure is that work for you...
private void DataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
YourObject obj = e.Row.Item as YourObject;
if (obj != null)
{
//see obj properties
}
}
Explanation :
In here after user enter the data to the grid, and It takes as e.Row.Item Then you can change any of modification to your object.
I'm filling a combobox with the datasource property in a c# winform app. In the other hand, I'm firing up an action with the SelectedIndexChanged of the same combo. The problem is that whenever the combo is filled with datasource the SelectedIndexChanged is called and I just want this event to be called when the user in fact does a selection.
Is there a way to avoid calling this event when filling the combo?
This is some of my code
//Filling the combo with some data
combo_cliente.DataSource = clientes;
combo_cliente.DisplayMember = "NomComp";
combo_cliente.ValueMember = "IDPersona";
private void combo_cliente_SelectedIndexChanged(object sender, EventArgs e)
{
// Here is the action to be triggered when user perfoms a selection
}
Thanks
Maybe unsubscribe and then subscribe again:
combo_cliente.SelectedIndexChanged -= combo_cliente_SelectedIndexChanged;
combo_cliente.DataSource = clientes;
combo_cliente.SelectedIndexChanged += combo_cliente_SelectedIndexChanged;
im assuming you assigned the event handler with the designer so they are bound when the control is instantiated. alternatively you could assign them in code after populating the controls.
Attach your event handler in your code behind instead of doing it on your aspx page and do it affer you have finished loading your control.
you need to add a blank record as the first of your combobox. Then in your code, you can write this;
private void combo_cliente_SelectedIndexChanged(object sender, EventArgs e)
{
if!(comboBox1.SelectedValue.ToString()== string.Empty)
{
//Here is the action to be triggered when user perfoms a selection
}
}
I have a listbox in which a have a series of cases retrieved from a database. When i create a new case i wan't to update the listBox to reflect the actual state of the cases-table in the database. But I get a NullReferenceException from the event handler for this line: populateBoxes((int)lb.SelectedValue) when i try to update it.
This is my event-handler on the listbox:
private void lbCases_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox lb = (ListBox)sender;
populateBoxes((int)lb.SelectedValue);
}
The update event:
private void button1_Click(object sender, EventArgs e)
{
this.casesTableAdapter.Fill(this.caseDB.cases);
}
I've used the built-in feature of VSE2008 to set the datasource, displaymember and valuemember of the listbox.
You have to make sure that when the datasource is set the lbCases_SelectedIndexChanged either does not fire or ignores the event.
Either create a 'Loading' boolean to ignore the event or set the index to -1 and add a check in lbCases_SelectedIndexChanged for the -1 index value to prevent the exception.
Set the selectedindex to -1 and then populate the list box over.
I suggest, you do a check in that event handler:
private void lbCases_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox lb = sender as ListBox;
if(lb == null)
return;
populateBoxes((int)lb.SelectedValue);
}
Check the SelectedValue for null before you run the
if (lb.SelectedValue != null)
{
populateBoxes((int)lb.SelectedValue);
}
Also, if you want to end up with a selected value, you will have to select it after you call:
this.casesTableAdapter.Fill(this.caseDB.cases);
i have created checkbox event maually.
chkCheckBox1.CheckedChanged += new EventHandler(chkCheckBox1_CheckedChanged);
this event is not triggered,in pageload i have put
(!page.ispostback)
{
}
so when i clik the check box it goes to the page load and not going to the evnt
protected void chkCheckBox1_CheckedChanged(object sender, EventArgs e)
{
..........
}
the checkbox event is not triggerd..
Have you enabled AutoPostBack property on your control?
By default this is set to False when you add a checkbox control to your page. Try setting it to true.
Set the Autopostback property to true.
chkCheckBox1.CheckedChanged += new EventHandler(chkCheckBox1_CheckedChanged);
You have to wire up this event on every call to the page so if you have put this inside of the if(!Page.IsPostBack) then put it outside.
Take a look at this article Adding a dynamic control to a placeholder control and wire up the event. It shows an extra step for making things totally dynamic but the principles stay the same for what you're after.
Grz, Kris.
To trigger the following event
protected void chkCheckBox1_CheckedChanged(object sender, EventArgs e)
{
..........
}
Set the checkbox autopostback property to TRUE