How to fire an event

D

Daniela Roman

Hello,

I try to fire an event under a button click event and maybe anybody can give
a clue please.

I have let's say a WEB grid with PageIndexChanged event:

private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)

{

// Set CurrentPageIndex to the page the user clicked.

DataGrid1.CurrentPageIndex = e.NewPageIndex;


// Rebind the data.

DataGrid1.DataBind();

}



Under a button click event:

private void btnUpdtCmd_Click(object sender, System.EventArgs e)

{

}



I want to fire the grid's event but I don't know how to call the event and
provide the parameter list for it:

(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)



Thank you,

Daniela
 
D

Dave Sexton

Hi Daniela,
I want to fire the grid's event but I don't know how to call the event and
provide the parameter list for it:

There are many events on the DataGrid control. You just have to perform an
action that will cause the grid to raise the desired event. It's not common
to raise Control events externally; Controls raise their own events
internally.

Are you trying to get the PageIndexChanged event to be raised from your button
click event handler?

If so, change the CurrentPageIndex property and the event should be raised by
the DataGrid.
 
D

Daniela Roman

Thanks. In fact I use a 3-rd party WEB grid control and for some reason it
does not fire the UpdateRowBatch event when I change the values of the grid
thru code. I need to raise that event thru other means.
The sample I provided is not the best example.

Is there another way of firing an event externally?
 
D

Dave Sexton

Hi Daniela,

I've seen some Controls that provide Raise[Event] methods, but usually not for
every event anyway. And I don't really like the idea either :)

I would contact the manufacturer or find a forum dedicated to that control
where people might have had similar issues. It definitely sounds like a bug.
 
D

Dave Sexton

Hi Daniela,

.... and in the mean time, you can possibly solve your problem by placing the
code from the event handler that isn't being called (which you could call
directly anyway) into another method as such:

void gd_UpdateRowBatch(object sender, EventArgs e)
{
CommonMethod();
}

void btn_Click(object sender, EventArgs e)
{
CommonMethod();
}

void CommonMethod()
{
// TODO: something in common
}

If the grid does things that require the event to actually be raised, you
might be able to emulate the effect yourself in the CommonMethod.

GL
 
D

Daniela Roman

Thank you. I will give it a try tomorrow. I have to see how I can deal with
the parameters.
 
C

Cor Ligthert [MVP]

Daniela,

Are you sure you want to fire the event, and not just what the event does.

TheMethod(null,null)'if you want to use a sender object it is up to you

Cor
 
D

Daniela Roman

It seems that changing values by code, which is done on client side does not
triger an event. I would need to force a postback. How can it be done?

Of course I can use the code under the event for the button click event, but
the other one would loop thru all the rows automatically.
 
D

Dave Sexton

Hi Daniela,
It seems that changing values by code, which is done on client side does not
triger an event. I would need to force a postback. How can it be done?

You should have mentioned that the "code" you spoke of was actually script,
running on the client. I never would have thought that manipulating the grid
in script would cause a server event to be raised.

You can force a post back by dynamically adding the ASP.NET javascript
postback method call to your own client script. This can be done by calling
the GetPostBackEventReference method (Page class in 1.* and Page.ClientScript
in 2.0) on the server to insert the callback into your own script:

<script type="text/javascript">
function doingThings()
{
// do some things in javascript

// post back to raise the button's click event
<%= ClientScript.GetPostBackEventReference(gridControl, string.Empty) %>
}
</script>

The problem here is that I have no idea whether the grid will raise the
UpdateRowBatch event on a post back. What action in particular raises the
UpdateRowBatch event? What argument needs to be supplied, if any? You might
want to read the grid's documentation and figure out what that event is
intended for before trying to raise it yourself.

The alternative to posting back for the grid control is to post back for your
button control (change "gridControl" to "myButton" in the javascript above,
for example) and perform the actions in code, on the server, as I suggested in
my previous response.

If you don't want to use a button then you can postback to the Page (change
"gridControl" to "this" or "Page" in the javascript above) and implement
IPostBackEventHandler.
Of course I can use the code under the event for the button click event, but
the other one would loop thru all the rows automatically.

If you can do so then it will be much easier (and safer) to iterate all of the
rows yourself when the button is pressed than it would be to try to rig the
event to be raised unnaturally.

Please realize that if what you are trying to accomplish has absolutely
nothing to do with the grid's UpdateRowBatch event then you shouldn't be using
the event at all.
 
D

Daniela Roman

Hi Dave,
There is no script code, I have a check box column and I loop thru the rows,
marking them as checked, all done under a button click event in C#.
Is it client side or server side?
In my oppinion it is server side.
 
D

Dave Sexton

Hi Daniela,

If the code is C# then it's running on the server :)

The button click event is being raised upon a post-back to the server. Since
you're apparently iterating the rows in the click event handler already I
don't see why you desire the UpdateRowBatch event to be raised. It seems like
an arbitrary choice.

What exactly do you need from this event?
 
D

Daniela Roman

Hi Dave,

It's my bad I did not provide enough details on the matter.
The ideea is: I have an Infragistic datagrid control, one of the columns is
a check box. One of the buttons is Check All, which will check all the check
boxes in the grid. Then the option is to either cancel the process and
uncheck them or go ahead and save the changes. For this there are 2 other
buttons, Save and Cancel. Under save I need to trigger the UpdateRowBatch
event. A collegue suggested that it does not trigger because the code is
doing the changes on the client side, which sincerely did not make much
sense to me.
 
D

Dave Sexton

Hi Daniela,
It's my bad I did not provide enough details on the matter.
The ideea is: I have an Infragistic datagrid control, one of the columns is
a check box. One of the buttons is Check All, which will check all the check
boxes in the grid. Then the option is to either cancel the process and
uncheck them or go ahead and save the changes. For this there are 2 other
buttons, Save and Cancel. Under save I need to trigger the UpdateRowBatch
event.

Thank you for the background information, but it's still not clear why you
want to raise the UpdateRowBatch event. What do you expect it to do?

You might want to check out some Infragistics forums and documentation
pertaining to the UpdateRowBatch event if you're not sure why it's not being
raised when you expect it to be.

Realize though that forcing the event to be raised might not cause the grid to
perform the expected actions that are associated with the event. In other
words, events notify subscribers of an action - raising the event yourself (if
you could) would not cause the action to occur. Instead, if you perform the
appropriate action on the grid then the event will be raised. This is
normally how controls behave, although I have to admit that I'm not familiar
with the Infragistics grid and it may behave differently.

So you should try to figure out:

1. how to perform the appropriate action on the grid to raise the event
2. whether the action required to raise the event is even necessary in the
first place
A collegue suggested that it does not trigger because the code is doing the
changes on the client side, which sincerely did not make much sense to me.

What changes?

When the Save button is clicked, if your C# code in the Save button click
event handler is executed, then there is either a post-back or AJAX at work
here. In either case, the Save button's click event is certainly being raised
on the server.

It's possible that there are changes occurring on the client-side before,
after and even during (if AJAX is used) the server click event. Although, I
suspect that there is a full post-back occurring when the Save button is
clicked (meaning AJAX isn't being used).
 
D

Daniela Roman

Unfortunately the Infragistics documentation is very poor, that means it
doesn's say anything about what triggers the event and when.
Why I used the Update RowBatch? I wanted to keep the same structure as the
other pages have (I came into project half way) and this event handles the
looping automaically, the programmer only provides the action for one row
(updating the record), the event will do then the process for each row in
the grid. I did not understand why when I click the check boxes with the
mouse and then click Save button it triggers the UpdateRowBatch, but when I
change the value of the check boxes thru C# code it doesn't.
 
D

Dave Sexton

Hi Daniela,

Well it could be a bug in the grid, but it's probably just misuse.

I assume that the "other pages' structure" performs different actions than
your page and that's why you are observing different behavior. If you are not
doing the same thing on your page then you might need to handle the save
operation differently as well - without use of the event.

This is an Infragistics-related problem. It has to do with performing the
appropriate action to cause a particular event to be raised. Again, you need
to figure out what actions cause the event to be raised and perform them.

As an alternative, you can iterate the rows in your own code.

If the documentation is poor then I suggest looking for Infragistics-related
forums.

GL

--
Dave Sexton

Daniela Roman said:
Unfortunately the Infragistics documentation is very poor, that means it
doesn's say anything about what triggers the event and when.
Why I used the Update RowBatch? I wanted to keep the same structure as the
other pages have (I came into project half way) and this event handles the
looping automaically, the programmer only provides the action for one row
(updating the record), the event will do then the process for each row in
the grid. I did not understand why when I click the check boxes with the
mouse and then click Save button it triggers the UpdateRowBatch, but when I
change the value of the check boxes thru C# code it doesn't.
 
D

Daniela Roman

There are 2 scenarios:
- checking the boxes with the mouse
- checking them by code.
The same code for Save calls the event only when I click them with the
mouse. Looping through the rows with a for() doesn't do the expected
behaviour. What can I possibly do in the for() so that I misuse it?
The other page does the checking of the boxes in a javascript, that is
client side.

Dave Sexton said:
Hi Daniela,

Well it could be a bug in the grid, but it's probably just misuse.

I assume that the "other pages' structure" performs different actions than
your page and that's why you are observing different behavior. If you are
not doing the same thing on your page then you might need to handle the
save operation differently as well - without use of the event.

This is an Infragistics-related problem. It has to do with performing the
appropriate action to cause a particular event to be raised. Again, you
need to figure out what actions cause the event to be raised and perform
them.

As an alternative, you can iterate the rows in your own code.

If the documentation is poor then I suggest looking for
Infragistics-related forums.

GL
 
D

Dave Sexton

Hi Daniela,

Well you might not be misusing the grid. It could be a bug.

I'm just assuming that the grid wasn't designed to do what you expect it to be
doing. To use the grid "correctly" you have to figure out what you "should"
be doing instead.

Because it's an Infragistics grid you'll probably find better support in a
forum dedicated to Infragistics, whether it's a bug or not.
 
K

Kevin Frey

If you want to try "call" the event directly, you're gonna to need the same
arguments that an event would provide. The tricky part of that will be the
"sender" object.

Using your example, you might be better off factoring out the event code
into a separate method that does not take "event-like" arguments (I note in
your case the method does not actually use the "sender" parameter) , and
have your event call that method. That way you can also call the same method
also.

Example

void ChangePage( int newPageIndex )
{
// Set CurrentPageIndex to the page the user clicked.
DataGrid1.CurrentPageIndex = newPageIndex;

// Rebind the data.
DataGrid1.DataBind( );
}

private void DataGrid1_PageIndexChanged(object source,
system.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
ChangePage( e.NewPageIndex );
}

// some other piece of code ...

ChangePage( the_new_page_number );

Kevin.
 
C

Cor Ligthert [MVP]

Kevin,

Using a seperate method has the same evect as

TheMethod(null,null)

Cor
 
D

Dave Sexton

Hi,

And just to add to Cor's response, neither approach will actually raise the event, which I believe
is what the OP was really after.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top