I am using a GridView in ASP.net with a pre-added buttons. The GridView's data source is from a list of BookingRowStatus
BookingRowStatus(Row, m_AStatus, m_BStatus, m_CStatus, m_DStatus, m_EStatus, m_FStatus)
The arguments that you can see are the Columns. I added RowCreated event to check m_XStatus to enable/disable the buttons depending on the text inside it ("X" = disable | "Y" = enable).
Next is I added a RowCommand event which should catch the event when a button in GridView is clicked. However, whenever I click the button, it invokes RowCreated instead of RowCommand. I added a break point on both of the said Events and I can see that it doesn't even pass RowCommand; it goes straight to RowCreated.
My question is how do I Invoke RowCommand when I press a button on the DataGrid?
The RowCreated event will fire before the RowCommand event is hit as the child controls (i.e. the rows) will be created on post back before you can access the values from them.
Set a breakpoint in both and see if they are both triggered.
Although your solution should work, an easier solution would be to bind the Button directly to the fields value. E.g.
<asp:Button runat="server" Enabled='<%# Eval("m_XStatus") = "Y" #>' />
Related
Hi I'm having a really strange issue here. I have an ImageButton and Gridview, both of which I create on runtime. The Image button is a seperate control on the webform and is not linked in anyway to the Gridview. What I am trying to achieve is when the user completes the editing of a line in the GridView and clicks on the update row button, I perform a set of calculations on the input entered. If an error is found with the data entered, I then make visible the ImageButton which when clicked displays the error message. The function I am using to make visible or hide the Image button through
picCross.Style.Value = "display: none;"
and
picCross.Style.Value = "display: block;"
gets called on the Page.PreRender function. What I have discovered is that I am able to make changes to the ImageButton (changing the button's tooltip, setting its Style's value ) during a postback not fired by the GridView, such as when the user clicks on a button else where on the web form. If however I try making changes to the Imagebutton during the post back event of the GridView, such as when the user clicks on the edit row or update row button/link. The changes are not saved. What is even more strange is I do not have such an issue with labels. I can change the text of labels regardless on whether the change was made during the postback fired off by the Gridview control.
I have tried setting the ImageButton's EnableViewState to true and false but neither makes a difference. I have tried the same approach with panels and the same thing occurs, I can't change its properties during a postback caused by the gridview. My GridView has EnableViewState = True
P.S I'm binding the GridView on runtime too. Only my GridView's RowUpdating event fires. The RowUpdated event does not fire. I was hoping to try changing the ImageButton on RowUpdated, not that I think it would have made much of a difference anyway.
You should create the dynamic controls (Gridview and ImageButtons) in the PreInit event. Init event if you have a Master Page.
I have a query regarding GridView Control : I added a commandfield inside my Gridview and set ShowEditButton="True" (By default it trigger RowEditing Event). Can i Trigger RowCommand Event Instead of RowEditing Event.
I know i can do it via other way but just got curious whether we can do this way
No you would need to use a control like ButtonField or LinkButton in order to trigger a custom command.
I Have a LinkButton when clicked I want to preform an operation inside the OnPreRender method.
So how would I know if that LinkButton was clicked?
Say my LinkButton's name is lnkbtn1.
Thanks in advance.
Baher.
Your button's Clicked event would already have been called by the time you get to PreRender, so maybe use the Click event to set a private boolean property on your control to indicate whether the button was clicked, which you can then check later in PreRender.
We have a Datalist in which we have some data, with every Item we have a button control. What I want to achieve is that on the button click, the data related to that particular row of Datalist is fetched whose Button control is clicked. How to accomplish this? The problem is how to attach the button control with values related in that particular row? Note that I am using ArrayList as the Datasource since I am enabling padding via pageDataSource class...
Implement the OnDataBinding event for the Button. Then in the Button set the CommandArgument to something that will identify the row you are clicking. Then implement the OnItemCommand event for the DataList. You will be able to read the CommandArgument for the Button that was clicked and then perform whatever action you need.
My understanding of the order of page events is this:
Page : Load
Control : DataBind (for a GridView or whatever)
Control : Load
Control : Clicked (for a Button)
Page: PreRender
Control : PreRender
(There are lots of others - but these are the ones I'm interested in)
The important thing to notice here is that the button's click event comes after the gridview's bind event. If the button causes a change to the data, the GridView displays the old data. I could rebind the control in the PreRender event, but that seems totally ugly.
This must be a very common pattern (a button that updates data). How do I put this together so that the GridView binds to the data after the Button click changes it?
The answer was in the Button Click event, after the data has been changed, call DataBind() on the page to cause the GridView (and anything else that needs it) to rebind. I didn't realise you could do that.
Thank you Ocdecio & Mufasa - I would mark your answers as helpful, but I got no rep yet.
ASP.NET does, by default, lots of binding and rebinding. Rebinding after a click event is normal.
The only reason the button click event comes after GridView bind is because you programmed your page to do that . I don't see any problem in binding the control in the PreRender event, in fact that is the only way to take action after a control event (such as Button onclick).