Categories
General (9)
General ASP.Net (6)
Javascript (6)
ASP.Net WebForms (6)
GridView Controls (7)
ASP.Net AJAX (2)
ASP.Net MVC (1)
 
 
Archive
Aug 2010 (1)
Jul 2010 (1)
Jun 2010 (1)
May 2010 (1)
Apr 2010 (1)
Mar 2010 (1)
Feb 2010 (2)
Jan 2010 (1)
Dec 2009 (2)
Nov 2009 (5)
Oct 2009 (10)
Sep 2009 (6)
Aug 2009 (2)
 
 
7607 Visits
 
Nov 2009

Restrictions of FileUpload control

The FileUpload control from ASP.Net has several limitations. The most common limitation is regarding the filesize. By default, files of upto 4MB can be uploaded without any problems. Beyond that, ASP.Net will reject the request. To allow files of upto 2GB to be uploaded, you can change the httpRunTime in web.config as follows:

<system.web> <httpRunTime maxRequestLength="2000000" executionTimeOut="999999"/> </system.web>
The maxRequestLength is specified in KB. A size of 2,000,000 will allow almost a 2GB file to be uploaded. Also, the timeout to complete the request should be set high so that upload can happen. If maxRequestLength is exceeded in a request, then the request is abruptly declined and control is returned back to the browser. This error cannot be trapped within the AsP.Net framework because - the page is not loaded, no exception is raised, no HttpModule is called, the request abruptly terminates and browser receives no status code.

The other limitation is that it is not possible for the upload control to determine the size of the file before the upload. This means that it is not possible to restrict the upload of files based on file size. I cannot set a javascript rule that files beyond 100KB cannot be uploaded

Apart from large files upload, the upload control has other problems. It is not possible to set a filter-type of which files to upload. For eg, if you want to upload only image files, it is not possible to set a filter - .jpg | .gif | .bmp to the upload control so that only files of these types are shown.

The FileUpload control does not have a progress bar to indicate the progress of the upload. If your website allows users to upload files regularly, then it is better to go for a custom FileUpload control based on JQuery or Silverlight

0 Comment(s), Your comments?

Sorted GridView order

I am using JQuery to sort my GridView. I want to save the GridView as excel. But excel does not contain the sorted order. How can I send the sorted order to the server?

This can be accomplished by having an additional TemplateField with a HiddenField. The HTML markup for Gridview will look like this:

<asp:GridView ID="gv" runat="server" DataSourceID="sql" AutoGenerateColumns="false"> <Columns> <asp:TemplateField> <HeaderStyle CssClass="hidden" /> <ItemStyle CssClass="hidden" /> <ItemTemplate> <asp:HiddenField ID="hdn" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="au_lname" /> </Columns> </asp:GridView>

The javascript for saving the sorted order into the HiddenField is shown below. It is getting all the hiddencontrols within each table cell and updating its value with the row index.

<script type="text/javascript" > function setOrder() { var gv = document.getElementById('gv'); for (var i = 1; i < gv.rows.length; i++) { var hdns = gv.rows[i].cells[0].getElementsByTagName('input'); hdns[0].value = i; } } </script> <style type="text/css"> .hidden { display: none; } </style>

In the server-side, the excel should be written in the same order as the row-index in the hidden fields. This can be done using code like this:

protected void btnSubmit_Click(object sender, EventArgs e) { for (int i = 1; i <= gv.Rows.Count; i++) { GridViewRow gvr = GetSortedRow(i); Response.Write(gvr.Cells[1].Text); } } private GridViewRow GetSortedRow(int index) { foreach (GridViewRow gvr in gv.Rows) { HiddenField hf = gvr.FindControl("hdn") as HiddenField; if (hf.Value == index.ToString()) return gvr; } return null; }

0 Comment(s), Your comments?

Hiding Repeater Items

I have a list of items displayed in a repeater. By default, I want to show the top 10 items. On click of a linkbutton, I want to show the remaining items. How can i do this? (Forum Link)

I have created a repeater bound to a SqlDataSource. The repeater is having a list of items displayed in an unordered list (ul). In FooterTemplate, I am having two linkbuttons. One is used for showing all items, another is used for hiding all but the top 10 items. The code is:

<asp:Repeater ID="rpt" runat="server" DataSourceID="sql" OnItemDataBound="rpt_ItemDataBound"> <HeaderTemplate> <ul id="list"> </HeaderTemplate> <ItemTemplate> <li id="listItem" runat="server"> <%# Eval("au_lname") %> </li> </ItemTemplate> <FooterTemplate> </ul> <a id="show" href="javascript:show();">Show All</a> <a id="hide" href="javascript:hide();" style="display:none;">Hide</a> </FooterTemplate> </asp:Repeater> <asp:SqlDataSource ID="sql" runat="server" ConnectionString="<%$ ConnectionStrings:Pubs %>" SelectCommand="SELECT au_lname FROM Authors"> </asp:SqlDataSource>

In OnItemDataBound event handler, from the 11th item onwards, I am using style="display: none;" to hide the items as follows:

protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemIndex >= 10) { HtmlGenericControl li = e.Item.FindControl("listItem") as HtmlGenericControl; li.Attributes.Add("style", "display: none;"); } }

Finally, the javascript for Show and Hide buttons. In the show function, we are getting a list of all items (li) within a ul. If ItemIndex > 10, then we change the display style of the item to 'block'. This shows the list item (li). The code is as follows:

<script type="text/javascript" > function show() { var list = document.getElementById('list').getElementsByTagName("li"); for (var i = 0; i < list.length; i++) { if (i >= 10) { list[i].style.display = 'block'; } } document.getElementById('hide').style.display = 'inline'; document.getElementById('show').style.display = 'none'; } function hide() { var list = document.getElementById('list').getElementsByTagName("li"); for (var i = 0; i < list.length; i++) { if (i >= 10) { list[i].style.display = 'none'; } } document.getElementById('hide').style.display = 'none'; document.getElementById('show').style.display = 'inline'; } </script>

0 Comment(s), Your comments?

ToolTip for DropDownList does not work in IE6

Every control has a ToolTip which is displayed when a user hovers over the control. A DropDownList also has a ToolTip property. This works fine in Firefox. But, in IE6, the tooltip does not work. The Tooltip for Dropdownlist for Internet explorer will work only if the DropDownList is disabled. Here is a Microsoft support article explaining the same: http://support.microsoft.com/kb/221608

0 Comment(s), Your comments?

Nested GridViews Bound to ObjectDataSource

How can I display Country and City information in a nested fashion like:

India Delhi Bangalore Chennai USA New York Chicago UK London

This can be done in three steps:

Create a object that will fetch data from database and structure it as collection of countries and collection of cities for each country

// Class used by ObjectDataSource // Fetches data from database // Puts the data as list of countries with list of cities within public class CityHelper { private Dictionary<Country, List<City>> cities = new Dictionary<Country,List<City>>(); public CityHelper() { SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Pubs"].ConnectionString); conn.Open(); string sql = "SELECT co.CountryID, ci.CityID, co.Country, ci.City FROM Country co INNER JOIN City ci ON co.CountryID = ci.CountryID"; SqlDataAdapter adapter = new SqlDataAdapter(sql,conn); DataTable table = new DataTable(); adapter.Fill(table); foreach (DataRow row in table.Rows) { // Add the country, city information // to the Dictionary as KeyValuePair Country country = new Country(); country.CountryName = (string)row["Country"]; country.CountryID = (int)row["CountryID"]; City city = new City(); city.CityName = (string)row["City"]; city.CityID = (int)row["CityID"]; if (cities.ContainsKey(country)) { List<City> cityList = cities[country]; cityList.Add(city); cities[country] = cityList; } else { List<City> cityList = new List<City>(); cityList.Add(city); cities.Add(country, cityList); } } } public Dictionary<Country, List<City>> GetCities() { return cities; } } // Country class with CountryID, CountryName // This class implements IEquatable interface as // this class is used as Dictionary Key public class Country : IEquatable<Country> { private int countryID; public int CountryID { get { return countryID; } set { countryID = value; } } private string countryName; public string CountryName { get { return countryName; } set { countryName = value; } } // The below three methods are required // if this class is to be used for Dictionary Key public bool Equals(Country c) { if (countryName == c.countryName) return true; else return false; } public override bool Equals(object obj) { Country c = (Country)obj; return Equals(c); } public override int GetHashCode() { return countryName.GetHashCode(); } } // City class with CityID, CityName public class City { private int cityID; public int CityID { get { return cityID; } set { cityID = value; } } private string cityName; public string CityName { get { return cityName; } set { cityName = value; } } }

Create a nested Gridview with outer GridView bound to the ObjectDataSource

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" DataSourceID="odsCities" OnRowDataBound="gv_RowDataBound"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Key.CountryName") %>'></asp:Label> <asp:GridView ID="gvCities" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField> <ItemTemplate> <%# Eval("CityName") %> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:ObjectDataSource ID="odsCities" runat="server" TypeName="ForumSamples6.CityHelper" SelectMethod="GetCities" />

Handle the OnRowDataBound of the Outer GridView. In this, bind the inner GridView to the appropriate cities collection

// Called when each row of GridView is bound to data protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // Get the data as KeyValuePair of Dictionary KeyValuePair<Country, List<City>> kvp = (KeyValuePair<Country, List<City>>)e.Row.DataItem; // Bind to the list of cities GridView gvCities = (GridView)e.Row.FindControl("gvCities"); gvCities.DataSource = kvp.Value; gvCities.DataBind(); } }

0 Comment(s), Your comments?