This method correctly loads the data and binds it to the infragistics control as long as I remove the object source, EventArgs e and call the method from page_load.
Is that a good idea to remove the object source, eventArgs e?
protected void dgvAppts_NeedDataSource(object source, EventArgs e)
{
if (Session.IsNewSession == false)
{
DataTable ApptTable = new DataTable();
ApptTable = objGatewayFunctions.GetAppointmentReCap(Session["LoginID"].ToString(), RecapDate.ToShortDateString(), "R", ConfigurationManager.AppSettings["Connection"].ToString());
this.dgvAppts.DataSource = ApptTable;
//if (ApptTable.Rows.Count == 0)
//{
// this.uwtTabs.Tabs(0).Style.ForeColor = System.Drawing.Color.Gray;
//}
//else
//{
// this.uwtTabs.Tabs(0).Style.ForeColor = System.Drawing.Color.Black;
//}
}
}
If it's better to have the object source, EventArgs e present in the method. How do I call that function so it can load the WebDataGrid?
I don't think Infragistics WebDataGrid has NeedDataSource event; it sounds like the code was being converted from the use of a Telerik control.
If that's the case then there's no need to call the method
protected void dgvAppts_NeedDataSource(object source, EventArgs e)
You can call it instead something like
protected void BindMyGrid()
and call it from the page load indeed (with possible check to do it only if page is not in a postback mode)
You need to assign your method as event handler to NeedDataSource event in your grid control.
Select your grid in the designer, open Properties window and select your method as event handler for NeedDataSource event.
Looks like the method got detached from the event and that's why you have to call it yourself. It should be the grid control that calls your method when it needs the data source by raising the event.
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)
I want to call the event from within another event.
I want to call this event
private void gv_client_CellContentClick(object sender, DataGridViewCellEventArgs e){}
From this event as
private void update_staff_Click(object sender, EventArgs e){
//some codes
gv_client_CellContentClick(); // i want to call this event here
}
If the event is in same class you can call it like
private void update_staff_Click(object sender, EventArgs e){
//some codes
gv_client_CellContentClick(sender,e); // i want to call this event here
}
As per our comment thread, it seems you want to call the method (as opposed to raising the event).
In your original handler, you can simply call the method:
private void update_staff_Click(object sender, EventArgs e)
{
var rowIndex = ???;
var columnIndex = ???;
var args = new DataGridViewCellEventArgs(columnIndex, rowIndex);
gv_client_CellContentClick(sender, args); // Note: You might need to change sender too if you know this function uses it...
}
The bit you'll need to figure out is the row/column indexes. Presumably this can be retrieved based on the location of the "update_staff" button/control being clicked - tip: cast "sender" to whatever control type you know it is to work out which button/control was clicked.
If I have a button which does something and also a double-click event on a data grid which I want to do the same thing, what is the best way to ensure that only one function has to be maintained?
Apart from doing the following, is there any fancy C# way to indicate that two events are to do the same thing?
void button1_Click(...) { MyFunction(); }
void dataGrid1_DoubleClick(...) { MyFunction(); }
void MyFunction() { // do stuff }
I suppose that you are talking about a DataGridView (WinForms) so the signature of the event DoubleClick in the DataGridView and the signature of Click event on a button control is the same.
(An EventHadler). In this case you can simply set the same method using the form designer or manually bind the event
dataGridView1.DoubleClick += new EventHandler(MyFunction);
button1.Click += new EventHandler(MyFunction);
Of course the MyFunction method should match the expected signature of an EventHandler
private void MyFunction(object sender, EventArgs e)
{
// do your work
}
Reviewing my answer after a few minutes I wish to add:
If you find yourself in a situation in which you need to differentiate between the controls using the sender object (like Control c = sender as Control; if (c.Name == "someName") ) I really suggest you to return to the first idea. Call a common method but keep the EventHandler separated for each control involved.
Using VS, in the form's designer view You can set the procedure You want to call to each control's each event in the control's properties window.
image
Just to add to what Steve said, you will want to bind these events to your function manually in the Load event of your form, instead of using the events under the lightning bolt in the properties window in the designer, like so:
private void Form1_Load(object sender, EventArgs e)
{
button1.Click += MyMethod;
dataGridView1.DoubleClick += MyMethod;
}
void MyMethod(object sender, EventArgs e)
{
//Do Stuff
}
Also, declaring a new instance of the EventHandler class has been redundant since Anonymous methods were introduced to C#, you can just point the event directly at the method as shown above.
I'm creating listviews in a flowpanel at run time which later will accept drag and dropped files. the reason being is i want these to act as folders so a user double clicks and gets a window displaying the contents.
i'm having difficulty setting up the events for my listviews as they are added.
how do i create some events (like MouseDoubleClick and DragDrop) dynamically for each added listview? can i create a single function for both of these events and have listview1, listview2, listviewX use it?
i have a button that is adding the listviews, which works fine. please advise, i apologize if this is too conceptual and not exact enough.
private void addNewWOButton_Click(object sender, EventArgs e)
{
ListView newListView = new ListView();
newListView.AllowDrop = true;
flowPanel.Controls.Add(newListView);
}
You would have to have the routine already created in your code:
private void listView_DragDrop(object sender, DragEventArgs e) {
// do stuff
}
private void listView_DragEnter(object sender, DragEventArgs e) {
// do stuff
}
and then in your routine, your wire it up:
private void addNewWOButton_Click(object sender, EventArgs e)
{
ListView newListView = new ListView();
newListView.AllowDrop = true;
newListView.DragDrop += listView_DragDrop;
newListView.DragEnter += listView_DragEnter;
flowPanel.Controls.Add(newListView);
}
You would have to check who the "sender" is if you need to know which ListView control is firing the event.
You can also just use a lambda function for simple things:
newListView.DragEnter += (s, de) => de.Effect = DragDropEffects.Copy;
Just make sure to unwire the events with -= if you also remove the ListViews dynamically.
To answer the other half of your question, you can use a single handler for any event, from any source, that has the handler's signature. In the body of the handler, you just have to check the sender argument to determine which control raised the event.
You need a way to tell one control from a different one of the same class, however. One way to do this is to make sure to set the Name property on each control when you create it; e.g., newListView.Name = "FilesListView".
Then, before you do anything else in your event handler, check the sender.
private void listView_DragDrop(object sender, DragEventArgs e) {
ListView sendingListView = sender as ListView;
if(sendingListView == null) {
// Sender wasn't a ListView. (But bear in mind it could be any class of
// control that you've wired to this handler, so check those classes if
// need be.)
return;
}
switch(sendingListView.Name) {
case "FilesListView":
// do stuff for a dropped file
break;
case "TextListView":
// do stuff for dropped text
break;
.....
}
}
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
}
}