How to program edit button for gridview through codebehind? - c#

I am trying to generate a gridview dynamically through codebehind. So I am making all the columns through code using BoundField and other controls.
Now I am trying to to put a edit button in the gridview sand program that(I made a RowEditing handler). Right now all my code is in the page_load but when I hit the edit button in the gridview I get 2 gridviews back on post back.
So I tried to put a isPostback if statement to stop this but then I just get a error back saying it can't find the handler.
So I am not sure what to do.
Thanks

Depends how you created the control, what version of Visual Studio, and how nested it is.
For example I currently just went backwards a bit in technology.
I'm using 17 gridviews on a page that are all sql server 2000 driven from code behind and i have a dropdown in the grid for even row as well.
vs 2005 2.0 is what i am using.
I'm using a Master Page (which typically will not be a problem from gridviews, but will need ResolveUrl for jquery references)
Getting to your question, I seem to have that problem with .net asking for a handler when i would tend to copy and paste from .aspx html source. I have MultiView/View/FormView etc... so I tried CTYPE etc..., but upon re-doing the edit with being on the aspx page design view, then use properties and the events. Or if it is a link or button on the page then click on it in design view and it should take you there and NOT have a problem with the Handles.
Some of my code for Edit Button
etc.....
THEN in your properties -> Events, look for "RowEditing" and write in a name then put breakpoints in where it is called and you are there really for edit mode...
I only use 2 other events "RowCancelingEdit" and then "RowUpdating"
Thus I click Edit (which everything is readonly), and a postback occurs (eventually i was to be doing grids again with ajax and not have postbacks) then my fields are in edit mode and dropdown shows list from database as well .... I can update or cancel.... either way it ends in back to readonly state with Edit button visible.
Please post some code if you are still having issues with it.

Related

RequiredFieldValidators stop working after UdpatePanel postback inside UserControl

I have a webforms page with 2 UpdatePanels: udpGrid for a gridview and udpFormViews with a ajaxToolkit:TabContainer surrounding 3 tabs for Insert and Editing records from the grid, and a repeater with it's own item template for editing related data to the main record. The edit tab is invisible on Page_Load. Inside the insert and edit tab resides the same Usercontrol which contains a Formview. the formview is set to insert mode by default but has an insert and edit item template with a footer template for save/update/cancel buttons, and when editing The second instance of the usercontrol gets set to edit mode and loaded and shown. It also contains a footer template containing the insert udpate and cancel commandbuttons to trigger the FormView's native inserting/updating/cancel events. RequiredField valdators on the insert FormView UserControl works fine from start and after inserting, but as soon as i do a postback to show the edit FormView usercontrol or the Repeater on hte 3rd tab (which has no relation to the Usercontrol containing the UserControl) the RequiredFieldValidators on the insert formview stop working at all, and even in code validating it's insert-validationgroup returns valid while the requiredField shows empty text in code.
For some reason the insert FormView UserControl loses it's validation logic completely after any postback but it's own Insert, with or without the second instance of the UserControl present. I don't load or change anything to the insert Formview UserControl and i can keep inserting without breaking the Validation but whenever a postback occurs outside of it it breaks. Even updates on the other UpdatePanel (udpGrid) break it and i have no way to test validation on the insert UserControl clientside or serverside using the RequiredFieldValidators. The Edit UserControl does still fire its validators but besides showing the error text on screen it ignores it and still updates the record, also ignoring server side the edit-validationgroup validation.
Does anyone know what is happening? it seems putting these Formvies in UserControls kind of breaks things while using it plain on other pages causes no issues?
Update: i found out that when the RequiredFieldValidation is broken i can still trigger the errortext by filling and emptying the textbox on the clientside. the problem that the page validation on client side and on server side doesnt work correctly persists
Found out the problem after some extensive digging. I was using ASP:Linkbuttons inside the footer template which didn't do the validation anymore after the UpdatePanel surrouning everything did an update. Even though i used ChildrenAsTriggers="False" on my UpdatePanels it somehow messed up the clientside validation triggers.
I went and moved the buttons into the templates themselves and changed them into ASP:Buttons. I believe i needed the buttons to be Linkbuttons since the normal Buttons wouldn't trigger postbacks with the FormView_ItemUpdating and FormView_ItemInserting events of the Formview in the Footer template, but now that i have dedicated buttons inside the Insert and Edit templates they can be normal Buttons again.
Hope this helps someone out there facing a simular problems.

Returning the gridview to its original vertical location after the rowediting postback

Using a combination of asp.net, jQuery, and c# and stored procedures.
I have successfully created a gridview, which I then apply jQuery datatables plugin to provide text column filtering (I disabled most of the other datatables function/settings off)).
The Gridview works as expected and when I make use of the RowEditing and RowUpdating events also, IE the editable fields update nicely.
If the user clicks and edits, then clicks the update link, the database is updated and the page reloads. now if that person has scrolled down say record 50 (half way down for instance) in order to locate and then edit that record, then after the page refresh, the user then has to scroll back down to find that same record.
I'm trying to find a way of returning the user to the same location (record) that they had just edited before the page refresh after the page refresh.
I found a very simple solution to my problem with out a load of coding and it turned out to be MaintainScrollPositionOnPostback="True" page directive.
Unless I've misunderstood; your dilemma is nothing to do with gridviews etc, and is all to do with merely maintaining page scroll position on post-back... have a read of this JavaScript solution from the 4 guys
P.S. Google is your friend

How to get the information from a dynamically created TableRow C#/Asp.Net?

Alright so in the project I am working on I am making a hard coded asp:panel on the page and then filling it with:
A Drop Down list
An asp:Table, with an asp:TableHeaderRow
Add and Cancel Buttons
When the selectedIndexChanged event happens with the DropDownList the tablerow gets created then filled with 6 or so tablecells which then get asp:Textboxes placed inside of them.
Eventually they all get added to the tablerow which then gets added to the tablerowcollection.
That all works just fine,but when I go to add the information from these dynamically created TableRows with a foreach loop they are no where to be found but the TableHeaderRow is.
Also when I click add and let it go through the process I get no null exceptions and the only thing left after it is the header, all my rows disappear.
Anyways I feel that I am just missing something really dumb. I've looked through a lot of forums, posts, MSDN and never found an answer to my problem. Any help would greatly be appreciated!
You need to add your dynamic rows to your Table in Page_Init not Page_Load. If you are creating it in Page_Load your new rows won't be added to ViewState and you will see this problem.
Unfortunately you are adding them in response to a server side event which can get tricky. You still need to add the rows in Page_Init but when the page posts back and you at in Page_Init then the selectedIndexChanged event hasn't yet fired. It's too early in the page lifecycle.
If you want to know if it has fired at Page_Init the only way I have found is by examining Request.Form("__EVENTTARGET") collection at that point. This contains the control ID of the control that has triggered the postback - in your case this will be the dropdown list. The control that fired the event will be there but the ID will be qualified i.e not MyControID but ctl_MasterPageContentHolderID_NamingContainer1_MyControlID` or the like - so you will have to take it into account when looking for it. Once you have identified that that event has fired then you can add the rows. Once they are added there then they won't disappear.
As I say it is tricky to get working but I have done this successfully in the past. Generally dynamic controls can be very hard to work with for just this reason. You may want to consider alternatives. Best of luck with it anyway though.

how do i get my dynamic controls for updating database rows to go away when i delete the corresponding row from the DB

Ok so basically my situation is this: i have a button to search a database. when you click the button I call a function (createRows()) that gets the data from the DB and for each row creates a user control that i made and populates its data with the data from the DB. i also call createRows() in the Page_Load function so that the controls will persist.
inside my usercontrol there is a delete button which in the user control's code behind C# file deletes that row of the DB. this all works fine but i have to hit the search button again for the user control to go away and im wondering why it wont actually go away on its own since i am calling createRows() in the Page_Load anyways?
My guess would be viewstate is caching the results. Set EnableViewState="false" on the control with the rows.
ok i think i might have figured it out i found this tutorial:
http://www.codeproject.com/KB/user-controls/Page_UserControl.aspx
and then i did the usercontrol to page communication and called createRows() from the usercontrol in addition to having it called in Page_Load. seems to be working, any other comments or suggestions are welcome though
thanks,
Leo

change listview on button click

I would like to change the listview template on a button click event. for example if your in edittemplate i would like to switch to ItemTemplate.
i am trying to do this because im writing my own custom update function for the list view. so after i successfully update the row, it doesn't switch back to the default view.
Rgds
Adrian
Adrian,
As you have tagged this as an asp.net question I would direct your attention to jquery (jquery.com). If you use a vanilla template (wrapping your elements in simple "div" tags) and use the jquery tools to do addClass/removeClass and toggleClass and apply various css styles to achieve the visual effect you desire you should be able to land just were you wish.
Using page methods you can leverage your custom update on a partial postback from jquery and reduce the server impact.
If you would rather do this server side you are probable looking to leverage the item databound event to set your template.
A more complete answer would require more information about what you are trying to accomplish.
Cheers,
CEC

Categories

Resources