I think many of you are aware of the FriendlyUrls feature added to ASP.NET Web Forms 4.5. It is available by default with Visual Studio 2012 if you have installed Web Tools update 2012.2 or later and it is available by default in Visual Studio 2013 RC as well. FriendlyUrls makes URLs of the web forms application friendly and it enables the option of creating views for mobile devices as well. In the pages specific to mobile devices, we can use the front-end mobile frameworks like jQuery mobile or Sencha Touch.
The default ASP.NET Web Forms template contains an optimized master page for mobile view, Site.Mobile.Master. This will be the default master page for all mobile specific pages.
Using front-end frameworks with server control is a bit tricky, but it is possible to use. In this post, we will see the way to use jQuery mobile’s table options with ASP.NET GridView.
Let’s make the server control ready. In the mobile page, add a GridView and bind data to it. Following is the mark-up of the GridView and the Select method that binds data from Customers table of Northwind database:
I chose 5 properties to make the output simpler. The page should contain the view port metadata tag and the scripts and styles needed for jQuery mobile.
To make the rendered table look like table in the demo on jQuery mobile site, following client properties have to be applied on the table:
These properties can be set in the DataBound event of the GridView. Add the following statements to the DataBound event of the GridView:
With this, we are done with applying the required settings. Load the page on a mobile emulator; it should look as shown below:
Happy coding!
The default ASP.NET Web Forms template contains an optimized master page for mobile view, Site.Mobile.Master. This will be the default master page for all mobile specific pages.
Using front-end frameworks with server control is a bit tricky, but it is possible to use. In this post, we will see the way to use jQuery mobile’s table options with ASP.NET GridView.
Let’s make the server control ready. In the mobile page, add a GridView and bind data to it. Following is the mark-up of the GridView and the Select method that binds data from Customers table of Northwind database:
<asp:GridView runat="server" ID="gvCustomers" SelectMethod="GetCustomers"></asp:GridView>
public IQueryable GetCustomers() { NorthwindEntities ctx = new NorthwindEntities(); return ctx.Customers.Take(10).Select(c => new { c.ContactName, c.CompanyName, c.City, c.Phone, c.Region }); }
I chose 5 properties to make the output simpler. The page should contain the view port metadata tag and the scripts and styles needed for jQuery mobile.
<meta name="viewport" content="width=device-width" /> <link href="Content/jquery.mobile.theme-1.3.2.min.css" rel="stylesheet" /> <link href="Content/jquery.mobile.structure-1.3.2.min.css" rel="stylesheet" /> <link href="Content/jquery.mobile-1.3.2.min.css" rel="stylesheet" /> <script src="Scripts/jquery-1.8.2.js"></script> <script src="Scripts/jquery.mobile-1.3.2.js"></script>The root div in the Site.Mobile has to be marked with data-role attribute to make jQuery mobile act on the content contained in the page.
<form id="form1" runat="server"> <div data-role="page" id="home"> <!-- Rest of the default mark-up --> </div> </form>Let’s make the GridView appear as the table in the demo on the jQuery Mobile site, so that the user will have an option to see some of the less necessary columns only when they are needed. To enable them, the GridView should be rendered as a table with thead and tbody. This can be done by setting the TableSelection property of the header row after data is bound to the GridView as shown below:
protected void gvCustomers_DataBound(object sender, EventArgs e) { gvCustomers.HeaderRow.TableSection = TableRowSection.TableHeader; }
To make the rendered table look like table in the demo on jQuery mobile site, following client properties have to be applied on the table:
- Some CSS classes have to be applied on the table
- data-role and data-mode attributes on the table
- data-priority attribute on less important columns
These properties can be set in the DataBound event of the GridView. Add the following statements to the DataBound event of the GridView:
gvCustomers.CssClass = "ui-responsive table-stroke"; gvCustomers.Attributes.Add("data-role", "table"); gvCustomers.Attributes.Add("data-mode", "columntoggle"); var headerCells = gvCustomers.HeaderRow.Cells; headerCells[3].Attributes.Add("data-priority", "2"); headerCells[4].Attributes.Add("data-priority", "2");
With this, we are done with applying the required settings. Load the page on a mobile emulator; it should look as shown below:
Happy coding!