Checkbox in GridView not persistent ASP.NET - asp.net

I am having issues with this GridView. I update it in design mode, and the update does not make it to the code behind section. For example, I add field "xyz". Gridview says "xyz" fields exist in design mode. In code-behind, it does not exist. when you view the page in browser, ofcoure "xyz" field is not shown. After refresh, even the gridview looses this field in design mode. So ok I got around this problem and managed to add a template field which is now working.
Now the problem is, the checkbox that I added in one of the column is not persistent. I have a button which works on the selected values of checkboxes but each time I click the button, the page refereshes and all the checked values are lost (checked values becomes unchecked).
Does anyone has any idea?
Want to mention, I am working with a bit messy code. But dont want to change a lot at this time.
<asp:TemplateField HeaderText="All" >
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" name="chkAll" />
</HeaderTemplate>
<EditItemTemplate>
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" />
</ItemTemplate>
</asp:TemplateField>

This took me a couple of days to figure out myself. Since my code was quite messy, it was hard to troubleshoot.
I put in some code in page_load default event that finally fixed. Don't know if I was putting code in the wrong place first. It definitely took some time though.

Related

Refresh asp:UpdatePanel when navigating to other panels in the same container

First off, I'm brand new to ASP.NET so I hope I use the correct terminology. I think that what I have is several asp:UpdatePanels in one ajaxToolKit:TabContainer. One panel has some checkboxes one it, here is the first part of the code for it:
<ajaxToolKit:TabPanel ID="MedicaidDataSubTabReadyToBill" runat="server" HeaderText="Ready To Bill">
<ContentTemplate>
<asp:Label ID="lblReadyToBillMessage" runat="server" Font-Size="X-Small" ForeColor="Red" ></asp:Label>
<asp:UpdatePanel ID="MedicaidDataReadyToBillPanel" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:RegularExpressionValidator ID="regSchoolYear" runat="server"
ControlToValidate="uxMedicaidDataReadyToBill_SchoolYear" ErrorMessage="*invalid school year format." Font-Size="X-Small"
ValidationExpression="^\d{4}$">
</asp:RegularExpressionValidator>
This panel loads some checkboxes, some checked and some not, then I go to another panel in the same container and upload an excel file that should make changes to the database that should affect the check boxes on the first panel. Problem is, when I navigate back to the first panel the changes don't reflect. In order for the changes to show I have to refresh the whole page. Is it possible to have this first panel show the changes without having to refresh the whole page? If so how do I do that? If more code is needed to answer this please let me know. Thanks in advance.
The basic way to update an update panel is simple
MedicaidDataReadyToBillPanel.Update();
When your code that uploads and does db stuff is finished, add that line. Assuming everything else is working, that's all you need.

asp:UpdatePanel on individual RadGrid rows

I am making some changes to an internal application that has a Telerik RadGrid component on one .aspx page.
Each row in the RadGrid represents an order and there is a dropdown which allows setting the status of the order. Changing the status of the order updates a couple of other properties of the order which are displayed on that row.
Up until now, changing the order status has resulted in a complete page post back and re-render. I'm keen to change this to a partial post back using an UpdatePanel. I could wrap the status dropdown (a RadCombo) in an UpdatePanel which would take care of the actual database changes that are required (as per the code sample below), but then without updating the other properties on the RadGrid row, the updates are not presented to the user.
<telerik:RadGrid ID="OrdersGrid" runat="server" ...>
<MasterTableView DataKeyNames="OrderId" AllowMultiColumnSorting="false">
<NoRecordsTemplate ...></NoRecordsTemplate>
<Columns>
<telerik:GridBoundColumn ... />
...
<telerik:GridTemplateColumn HeaderText="Order Status" UniqueName="OrderStatus">
<ItemTemplate>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<telerik:RadComboBox ID="RadOrderStatus" DataSourceID="OrderStatusDataSource" runat="server"
SelectedValue='<%# Bind("OrderStatus") %>' Skin="Metro" Width="180px" DataTextField="OrderStatus"
DataValueField="OrderStatus" AutoPostBack="True" EnableLoadOnDemand="False" OnSelectedIndexChanged="RadOrderStatus_SelectedIndexChanged">
</telerik:RadComboBox>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
<SortExpressions>...</SortExpressions>
</MasterTableView>
</telerik:RadGrid>
I'm wondering how I can use the UpdatePanel to effectively wrap the row of the RadGrid so that the entire row can be updated as opposed to just the cell that the dropdown is in. I've tried experimenting already with tag placement but I'm new to Telerik and therefore not very clued up.
At what level in the mark up can I place the UpdatePanel to get this to work as I'd like?
Or is there a Telerik way of doing this?
I could wrap the whole grid but if possible, I'd rather not have the whole grid update on each partial post back, the permitted operations are limited to the row level so I see a full grid update as wasteful.
You can't place an UpdatePanel around each row. For starters, there is no provision to do that (you can't do it with the standard GridView either). Then, if you manage to do that (e.g., override the Render event), you would get invalid markup because you can't have <div> elements inside the <table> and between the other <tr> nodes.
One note on the performance—the AJAX request will have the page go through its entire lifecycle on the server, so all code will be executed again and any time consuming operation will also be executed. The only difference between the AJAX and the full postback is what gets rendered and returned in the response, so you basically shave off network time only.
What you can do is the following:
wrap the entire grid. I would use RadAjaxPanel and RadAjaxLoadingPanel so you have a pretty loading indicator. Something like:
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Black"></telerik:RadAjaxLoadingPanel>
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" LoadingPanelID="RadAjaxLoadingPanel1">
<telerik:RadGrid ID="RadGrid1" runat="server"></telerik:RadGrid>
</telerik:RadAjaxPanel>
OR, use client-side code and jQuery. The combo can call a WebService or a PageMethod that will return the data and you can use jQuery to traverse the DOM and update the other elements (textboxes, dropdowns, whatever you have). This is going to be more difficult.

User control retains focus incorrectly after TextChanged postback when UC is contained within a detailsview control

I reference a previous post: Focus lost on partial postback with UserControls inside UpdatePanel
where an excellent solution works perfectly for web-page controls within a form.
However, I have placed my UC inside a detailsview template-field (for Edit+Insert).
The UC contains an UpdatePanel needed to adjust the text-formatting and control's style(s) following the TextChanged event of the UC-textbox (AutoPostback=True) during the Edit-mode and Insert-modes of the DetailsView.
As such, when the DetailsView-control is in Edit-mode, and user changes Text in the UC, the textchanged event is fired and the user-entered value is validated and when OK, the thousounds-separator (comma) are added to the UC-textbox-text, BUT, the focus moves to the next field in the DetailsView and QUICKLY returns back to the UC-control.
This incorrect focus-move(s) does NOT occur when the UC is wrapped in updatepanels as noted in the referenced post since the focus and tabbing order works perfectly outside of the DetailsView control.
Here is the aspx markup for the template-field-EDIT (only).
<asp:TemplateField HeaderText="Initial Mileage" SortExpression="IMilage">
<EditItemTemplate>
<asp:UpdatePanel ID="updpnlIMilage" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<TGANumeric:GANumeric ID="ucnumIMileage" runat="server"
Caption="Initial Mileage" HideCaption="True" Width="160"
DisplayMask="999,999" InputMask="999999"
Enabled="True" IsRequired="False"
MinNumber="0" MaxNumber="999999"
Text='<%# Bind("IMilage") %>'
TabIndex="0"
/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ucnumIMileage" />
</Triggers>
</asp:UpdatePanel>
</EditItemTemplate>
Thanks in advance. Your comments are welcome.
Thanks...J.
So are you saying your control hierearchy (partial) is:
UpdatePanel > TGANumeric:GANumeric > UpdatePanel > TextBox
This is an awful lot of overhead to just format a number with a comma, which is the only reason I see for your posting back. As far as I can tell there is nothing you need from the server, so why post?
Or is there?
My thoughts, lose the update panels, disable the AutoPostback on the Textbox, handle the formatting client side if it must be seen immediately, or leave the formatting to the DetailsView field DataStringFormat when it posts after save.
I'm betting this will clear up any focus issues.
Based on all the comments in this thread, I want to explain the actual root cause of the tabbing misbehavior.
1) There were no coding issues or event-issues with the user-control.
2) There were no coding issues or event-issues with the layering of Master-page, Content-page, Ajax update-panels / nested update-panels, details-view and template-fields containing the user-control.
3) The real culprit was a small snippet of code where the page adjusts the web-controls on the form based on the "state" (status) of the page/form. I manage the adjusting of visible and/or enabling of web-controls in a single subroutine in the code-behind so that all of this enabling/disabling visible/not-visible occurs in one place under a set of CASE-statements.
The actual erroneous snippet of code inside the 'sbSetFormState()'-method was messing with the class-variable 'm_eFormState' that actually caused the update panel to re-fire and thus the tabbing got thrown out of sequence.
This was discovered by the great suggestion from 'fnostro' to remove or add functionality features until the mis-behavior exposes itself.
I mark this topic as resolved/closed.
Again, thanks to fnostro !!!

Custom validation not working with delete buttons?

In a ListView, I have a CustomValidator set up to validate a field whenever a button with CommandName="Delete" is clicked.
<ItemTemplate>
<asp:TextBox ID="NameTextBox" Text=<%# Eval("Name") %> runat="server" />
<asp:Button ID="DeleteButton" Text="Delete" CommandName="Delete" ValidationGroup="Delete" runat="server" />
<asp:CustomValidator ValidationGroup="Delete" SetFocusOnError="true" Display="Dynamic" OnServerValidate="CustomValidator_ServerValidate" runat="server">You can't delete this.</asp:CustomValidator>
</ItemTemplate>
However, the error message is never displayed and the processing continues. What's strange is that the custom validation method is called, finds the field, and properly sets up e.IsValid to false. It does not matter whether I check Page.IsValid or not, because the error message is not displayed anyway.
It works if I remove the CommandName="Delete" from the button.
With Google I found the following solution, which seems to indicate someone has had a similar issue:
http://devio.wordpress.com/2007/12/11/formview-delete-button-and-customvalidators/
But I want to make sure that this solution is the way to go. I mean, is custom validation really not supposed to work with a delete button in a databound control, seriously?
I've alrealy heard about a problem like that, he solved it by doing it entirely differently. Like, instead of the customValidator, he puts a Label set EnableViewState="False" and Visible="False" and he check on the delete event the conditions and put the response back into the label. Maybe it can't works for you too?
But, if you realy ask "Why?????", I know he didn't find the exact reason ...

Delete from grid asp.net

i have this template field inside a gridview.
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="ImageButton2" ImageUrl="~/images/DeleteRecord.gif" runat="server"
OnClientClick="return ConfirmacionBorrarClausula();" CommandName="BorrarClausula" CommandArgument='<%#Eval("ClausulaID")%>' OnCommand="gvClausulas_OnRowDeleting" CausesValidation="false"
</ItemTemplate>
</asp:TemplateField>
I have another one in the same page but in a different gridview, almost exactly like this one but the second one isn´t working.
So i have two gridviews each one with a template field like the one here, one onRowDeleting working perfectly, the other one not working at all, when i click it, it asks for confirmation (javascript function) but when i click ok to delete, the grid loses it data and the page fires all the validators.
Thank you for your time.
Make sure the control IDs are set right. And Ispostback the control level set to true. And also Try deleting the control and add it again some time that might help. Try add it from design view.
i manage to solve it, the problem was the second gridview was losing its data on the pageload, i managed that but only with the first gridview.

Resources