Generate postback to specific method/event handler - c#

I have a custom gridview, and i'm generating a postback event for onclick manually on RowDataBound. However, this postback ALWAYS goes to/expects a specific method on the row click.
Here's my current code to make that postback happen:
if (!String.IsNullOrEmpty(this.OnRowClick))
e.Row.Attributes.Add("onclick", this.Page.ClientScript.GetPostBackEventReference(this.grid1, "Select$" + e.Row.RowIndex.ToString()));
As you can see here, i've setup a property to my grid to ask which method to call when executing that postback... however, i cannot seem to find anywhere on the web how to specify in the _dopostback call WHICH method to call... it always, without exception, expects this method:
grid1_SelectedIndexChanged(object sender, eventargs e)
But I don't want that. I want it to go to whatever method name is contained in the property OnRowClick.
Here's how things are currently setup for the visual people:
that's just it... i'm not sure. I am a visual person so maybe this will help a bit for everyone as well:
Custom Control Setup (minified version):
[ Some other controls
-= A gridview =-
Some other controls]
[(Control Event Property)
OnRowClick Event (GridViewRow eventarg)]
[(Control Event Wireup)
if(OnRowClick!=null)
this.GridView1.SelectedIndexChanged += InternalMethod(...)
[(InternalMethod(...))
GridViewRow row = this.GridView1.SelectedRow;
if(OnRowClick!=null)
OnRowClick(row);]]
So far so good... no problems whatsoever... BUT... when i set it up like this:
HtmlPage
-> Custom Control(mygrid)
---> mygrid.OnRowClick += SomePageMethod(GridViewRow row)
------> SomePageMethod never, ever, gets called... like... never...
Since i have built my control as a composite control, i can't step through its internal code to debug, so i can't tell why it's not happening... :S please please help me bubble up this event properly!
Custom Control Code:
this.grid1.SelectedIndexChanged += new EventHandler(grid1_SelectedIndexChanged);
void grid1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = this.grid1.SelectedRow;
if(this.OnRowClick!=null)
OnRowClick(this.grid1, row);
}
#region Events
public delegate void RowClickEventHandler(object sender, GridViewRow SelectedRow);
[Category("Events")]
[Description("This event fires when a row is clicked, if it is defined")]
public event RowClickEventHandler OnRowClick;
#endregion
Web page code:
this.pgrSummary.OnRowClick += new SimplePager.SimplePager.RowClickEventHandler(pgrSummary_OnRowClick);
And that methode never gets called...

Related

Method for Focus Change

Is there a method which initiates on focus change and can be overridden?
My goal is for the program to fetch closest data automatically from database to input fields whenever user changes his focus/presses enter or tab when on corresponding field. I'm still looking for a way to do this when user selects an item by mouse.
I'm aware that this could be implemented on mouse click but I refuse to believe that there is not a general method for focus change.
What about something like this:
foreach(Control ctrl in this.Controls)
{
ctrl.Enter += new EventHandler(Focus_Changed); // Your method to fire
}
Iterate through all controls and add a enter-event. Bind this handler to your method.
Edit:
Just in case you are wondering why "Enter" and not "LostFocus" or something like that: From my knowledge not every control got focus-events. As I've seen so far "Enter" is presented for all. Maybe there are exceptions. Should be checked out...
You could use Control.Enter event and Control.Leave event for that purpose.
See on MSDN Control.Enter and
Control.Leave.
textBox1.Enter += textBox1_Enter;
textBox1.Leave += textBox1_Leave;
private void textBox1_Enter(object sender, System.EventArgs e)
{
// the control got focus
}
private void textBox1_Leave(object sender, System.EventArgs e)
{
// the control lost focus
}

C# Basics - TextBox event handler

I have a simple TextBox called MsgBox1 and I have changed the trigger from LostFocus to ProperyChanged.
When I modify the text (i.e. MsgBox1.Text = "Some Text") execution branches to the event handler.
So far, so good.
Now, what do I put in the empty event handler to tell it to actually update MsgBox1.Text?
Hours of searching yields less than helpful results like:
{
// Do Something
}
Edit: Thanks, It was stupidity on my part. The methods don't run in parallel while tracing. If I run rather than trace everything works as it should. Thanks again.
As stated in the comments there is no need to update anything. The event fires when the textChange event occurs.
you can test this by using the following code :
private void textBox1_TextChanged(object sender, EventArgs e)
{
var currentTextValue = this.textBox1.Text;
var currentTextValueFromObject = (sender as TextBox).Text;
}
both vars yield the same result. One grabs the object from the event handler while the other grabs it directly from the form.

Gridview cell value on RowCommand

I'm attempting to get the cell value from a gridview but running into a few issues. I have setup my code similar to this example except that I'm adding my button fields on the server side. For some reason RowCommand is firing twice when clicking the cell values? TIA for any help
Page Load is empty:
code
protected void Page_Load(object sender, EventArgs e)
{
}
Adding Button Field:
code
foreach (DataColumn col in transposedTable.Columns)
{
ButtonField bfield = new ButtonField();
bfield.DataTextField = col.ColumnName;
bfield.HeaderText = col.ColumnName;
bfield.CommandName = "ColumnClick";
gvTest.Columns.Add(bfield);
}
The RowDataBound and RowCommand events are the same as the example link above
ok...There seems to be an open bug in MSFT connect...
GridView RowCommad Event Firing Twice
seems like there are some workarounds posted in the Workarounds tab...
Hope this helps...
This is a known bug. I had the same problem.
I removed the Handles GridView.RowCommand from the code behind event declaration, and
added this line to the properties declaration for the grid in the .aspx source:
OnRowCommand="GridView_RowCommand"
Worked perfectly.
One way to access row values in a grid is to use the Cells collection along with the grid's current index (which you can access via the eventargs) as shown below
void YOUR_GRID_EVENT(Object sender, GridViewDeleteEventArgs e)
{
Grid.Rows[e.RowIndex].Cells[0];
}
you can also make use of the findcontrol as below:
var txtName = e.Row.FindControl("txtName") as TextBox;
Hope this helps...
Update
Also check your code to make sure that you are calling the GridView's DataBind() method appropriately...because every time you call GridView.DataBind()...the rowcommand event of the grid view gets called...I think (guessing) you currently have a gridView.DataBind() in your onload as well as in the button click event..so that might cause the rowcommand event to be called twice...if this is not the case then post some code so that we can explore more...

Why would my child controls be uninitialized at the time of event attachment?

I have a page and a user control — we'll call them Detail.aspx and Selector.ascx.
Let's say the page shows the details of individual records in a database. The user control basically consists of a DropDownList control and some associated HTML. The DropDownList displays a list of other records to switch to at any time.
When the DropDownList fires its SelectedIndexChanged event, I'd like the parent page, Detail.aspx in this case, to handle it. After all, he'll need to know what was selected so that he can appropriately change the URL and the details shown, etc.
To do that, I've done what I usually do, which is also what the top answer says to do in this StackOverflow question:
public event EventHandler DropDownSelectedIndexChanged
{
add
{
MyDropDownList.SelectedIndexChanged += value;
}
remove
{
MyDropDownList.SelectedIndexChanged -= value;
}
}
The above code appears in the Selector.ascx.cs codebehind file.
As a result, on Detail.aspx, I can use it like so:
<cc1:RecordSelector ID="RecordSelector1" runat="server"
OnDropDownSelectedIndexChanged="RecordSelector1_DropDownSelectedIndexChanged" />
So far nothing fancy or surprising.
Here is my problem:
This causes a NullReferenceException when the browser hits Detail.aspx.
Debugging the problem shows that when the page is first hit, the public event I've shown above tries to add the event, but MyDropDownList is null, thus throwing the exception. From what I can tell, the events are added (or attempted to be added) before the Selector user control's Load event fires and thus also before the DropDownList's Load event fires.
Curiously, if I omit the OnDropDownSelectedIndexChanged attribute from Detail.aspx and instead put the following in the Page_Load event in Detail.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
RecordSelector1.DropDownSelectedIndexChanged += new EventHandler(RecordSelector1_DropDownSelectedIndexChanged);
}
It works exactly as expected. The events are attached and handled just fine. No problems.
But this means several bad things:
I have to remember not to use the designer to add said event onto my user control
I have to remember not to add the event via attributes when working in source view
Worst of all, as the control's author I need to make sure everybody else using my control knows 1 and 2
So what am I doing wrong? Every example I've seen thus far shows similar usage of exposing child controls' events through a user control.
The reason this works:
protected void Page_Load(object sender, EventArgs e)
{
RecordSelector1.DropDownSelectedIndexChanged
+= new EventHandler(RecordSelector1_DropDownSelectedIndexChanged);
}
and this does not:
<cc1:RecordSelector ID="RecordSelector1" runat="server"
OnDropDownSelectedIndexChanged="RecordSelector1_DropDownSelectedIndexChanged" />
is because the first one adds the handler after the control has been initialized (via the page's Init). The second example gets parsed much earlier and as such the page is attempting to add the handler before the control has initialized.
Due to the nature of the page's life cycle I think you may have to live with adding the event handler in the code-behind. There will be no way to add the handler before the control is initialized because that control will always be null prior to initialization.

DataGridView and the CellEndEdit Event

I have a DataGridView, and would like to hook into the CellEndEdit event. I've been able to successfully hook into the CellContentClick event, but am having issues with CellEndEdit.
I added the following code to my Form1.cs file:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellCancelEventArgs e)
{
dataGridView1[0, 0].Value = "Changed";
}
With that code, nothing happens when I am done editing a cell. Is there anything else that I need to do to successfully hook into this event? I see that CellContentClick has a
this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
line of code in the Form1.Designer.cs file, but I tried to mimic this for CellEndEdit, and received a compile error
(No overload for 'dataGridView1_CellEndEdit' matches delegate
'System.Windows.Forms.DataGridViewCellEventHandler')
You could implement this yourself.
In your constructor you could have a HookEvents() method which wires up such events.
Or, within the form designer, click the gridview to select it, go to the properties window and click the yellow thunderbolt to find a list of events. Then, scroll down and find the CellEndEdit event and double click it - this will wire up the event for you.
To wire it up yourself, it may look like:
class A : Form
{
public A()
{
Initialize();
HookEvents();
}
private void HookEvents()
{
dataGridView1.CellEndEdit += dataGridView1_CellEndEdit;
}
}
I doubt very much that your solution would work.
It's not a matter of where you place the subscription, is how you do it.
Brandon, you are declaring an EventHandler, that is the function responsible of doing what you want to do in case of that event "dataGridView1_CellEndEdit" but you are not subscribing to the event. Also in your function you are passing the wrong parameters.
The easy solution is either subscribe from the designer window or by code doing this:
write "dataGridView1.CellEndEdit +=" and then press the TAB buton twice. That shoud create the code for subscription to the event and the correct delegate to handle it.

Categories