Insert a row in DataGrid control - asp.net

I've got a standard ASP.NET 4.0 DataGrid control, it's declared like this:
<asp:DataGrid id="gridIssues" runat="server" EnableViewState="false" AutoGenerateColumns="False" OnItemDataBound="gridIssues_ItemDataBound" UseAccessibleHeader="true" ShowFooter="true">
I need to insert another row with the "More tickets" button at the bottom when count of rows exceeds a certain number (see screenshot).
http://dl.dropbox.com/u/347209/Screen%20Shot%202012-03-18%20at%206.45.53%20PM.png http://dl.dropbox.com/u/347209/Screen%20Shot%202012-03-18%20at%206.45.53%20PM.png
The preferred way to do this is to inject html like <tr><td colspan='%columns_count%'></td></tr> on the server side somehow. I can do this on the client side with JavaScript, but this solution is not flexible enough for me.

With databound controls, you should add rows to the datasource and then rebind. You can't easily add html directly.

Related

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.

DevExpress Hidden GridView CSS Issues

I have a repeater control that repeats a DevExpress ASPxGridView for every item bound to the repeater. The repeater is contained within an update panel. Events on the page, outside of the UpdatePanel, trigger the UpdatePanel (and subsequently the repeater) to update. All works fine if records are present to bind to the repeater. The repeater renders a grid for each record and all styles look perfect.
If the page initially loads and there are no items to display in the repeater, no grids are rendered (works as intended up until this point). If a record is eventually added and the repeater rebinds (because of the triggered UpdatePanel), the grid styles don't display. If the entire page is refreshed, the grid's styles display perfectly. Keep in mind that I'm using one of the default styles that comes with the grid, so these are being pulled from an AXD and not included in my MasterPage.
A bit too much code to post, but the nuts of the markup looks similar to this:
<asp:UpdatePanel ID="the UpdatePanelInQuestion" runat="server" UpdateMode="Conditional">
<asp:Repeater ID="theRepeaterInQuestion" runat="server" OnItemDataBound="theMethodThatHandlesGridPopulation">
<ItemTemplate>
<dxwgv:ASPxGridView ID="theGridViewInQuestion" runat="server" EnableViewState="false">
<Columns>
...
</Columns>
</ItemTemplate>
</asp:Repeater>
</asp:UpdatePanel>
Any ideas on how to make the styles of the grid display correctly without:
1) Refreshing the entire page instead of triggering.
2) Placing another empty grid on the page with style="display: none;" to force the styles to download.
This problem is caused by the fact that the required scripts for the DX ASP.NET controls are not registered on the page initially. It is possible to register them explicitely via the DevExpress.Web.ASPxClasses.ASPxWebControl.RegisterBaseScript method.
Please check the http://www.devexpress.com/issue=B191046 Support Center ticket regarding this.

Why is ASP.NET ViewState retained with an asp:dropdownlist but not an asp:table?

Alright, there is probably a super-obvious answer to this question for many of you, but it has me stumped.
I have an asp.net web form, and on it I have two controls (well, more than these two but we'll focus on these) - the first is an asp:dropdownlist and the second is an asp:table.
Both of these controls are declared on the HTML side, and filled (child controls added) in the code-behind page.
My simple question (hopefully with a simple answer) is this:
Why does the viewstate persist for the dropdownlist, and NOT for the table?
I have to fill the table on every page load, but I can fill the dropdownlist once (using Not Page.IsPostBack), and it persists.
NOTE: I have read about the lifecycle of ASP.NET pages, and I have tried placing these same calls in the Init() and PreInit() page events with the same results.
What obvious detail am I missing here?
Thanks for the help.
You're not missing anything, your assessment is correct. ASP.NET tables do not save their contents to view state.
I assumed at least part of the reason is that a table can contain any amount of any type of data and could really start to add to the size of the view state in some cases.
Either way, that's the way they work. If postbacks are possible on your page, you'll need to either repopulate the table on each load event, or store the table data to view state yourself and then repopulate the table from the data.
The asp:table object does not store its contents in ViewState, as it is not a data bound control. It works the same as an asp:panel in this regard; if you add controls to it programmatically, you need to do so on each post-back, or the items in it will not persist.
If you wanted to store the contents of the table in viewstate - which I woudln't recommend, but sometimes you need to do so ... you would just have to use the standard asp.net table control.
<asp:Table ID="Table1" runat="server">
<asp:TableRow ID="TableRow1" runat="server">
<asp:TableCell ID="TableCell1" runat="server">
This is Cell 1
</asp:TableCell>
<asp:TableCell ID="TableCell2" runat="server">
This is Cell 2
</asp:TableCell>
</asp:TableRow>
</asp:Table>
You can read more how to do this at http://weblogs.asp.net/nannettethacker/archive/2008/01/24/html-tables-to-asp-net-table-controls.aspx.
You might also try adding runat="server" to the table tag.
Not really answering your question directly, but thought it might be helpful nonetheless.

ASP.NET Repeat Dynamic Data Main Menu Horizontally?

The default ASP.NET Dynamic Data template by default uses a GridView to display the menu repeating vertically. This doesn't look particularly well. I'm wondering if there is either (a) a way to get the gridview repeating horizontally or (b) use another control that allows horizontal repeating.
A DataList can be used :)
You set an ItemTemplate to specify how the items should be displayed and set the RepeatDirection to Horizontal, bind it to your Dynamic data source and you are done :D
Additionally you can specify the number of columns to be repeated through the RepeatColumns attribute.
This page (scroll down towards the end) has got some examples on how to use a DataList
http://msdn.microsoft.com/en-us/library/7efxhktc.aspx
This is a simple process. In our code-behind file we have to wire up our Dynamic Data connection like so:
Menu1.DataSource = visibleTables
Menu1.DataBind()
Then we create a DataList like so:
<asp:DataList ID="Menu1" runat="server"
CellPadding="3" GridLines="Vertical"
HorizontalAlign="Center" CssClass="DDGridView" RepeatDirection="Horizontal"
ItemStyle-CssClass="td" HeaderStyle-CssClass="th" >
<ItemTemplate>
<asp:DynamicHyperLink ID="HyperLink1" runat="server"><%# Eval("DisplayName")%></asp:DynamicHyperLink>
</ItemTemplate>
</asp:DataList>
You can see that I've utilized (temporarily) the css classes from the default gridview to provide uniform layout/display elements.

ASP.NET: How to assign ID to a field in DetailsView?

I have a master-detail page, in which I use GridView to display multiple rows of data, and DetailsView + jQuery dialog to display the details of a single records. only one DetailsView is open at a time.
I need to be able to pull out a single field of the open DetailsView, for manipulation using JavaScript. Is there a way to give a unique ID to a given field in DetailsView, so I can use getElementByID? Or is there another way to accomplish what I'm trying to do?
Thank you in advance.
If you are using a bound textbox in a template field in your detailsview you can then select it by:
$("[id$='MyTextBox']");
Which will find the textbox bound to MyFieldName as below.
<asp:DetailsView ID="DetailsView1" runat="server">
<Fields>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="MyTextBox" Text='<%# Bind("MyFieldName")%>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
Whatever guff asp.net adds onto the begining of the id won't matter because jQuery $= mean "ends with".
there may be a better way, but when I've needed an id available for js work, I just convert the bound field to a templated field. you can then give the textbox the id of your choice. keep in mind when rendered the id will be expanded with the id of the parent control.

Resources