Posted by Sébastien Lachance with Comments (0)
The solution is to set the SQLServerCompactEditionUnderWebHosting property of the current application domain to true.
Response.Redirect cannot be called in a Page callback
Sometime you may need to redirect the user somewhere else when doing a page callback. This is not possible with Response.Redirect but you can use Response.RedirectLocation,
Response.RedirectLocation = "notarealpage.aspx?i=1";
Last week, I created a web page that allowed some users to download a list of name with their email addresses as a CSV file. However, some characters were not exported correctly.
Here is a sample of the code I used :
StringBuilder sb = new StringBuilder(); IList<Subscriber> subscribers = Subscriber.GetAll(); foreach (Subscriber subscriber in subscribers) { sb.AppendLine(string.Format("{0},{1}", subscriber.Email, subscriber.Name.Replace(",", ""))); } Response.Clear(); Response.ContentType = "application/CSV"; Response.AddHeader("content-disposition", "attachment; filename=\"subscribers.csv\""); Response.Write(sb.ToString()); Response.End();
And some name looked like this when opened in Excel : sébastien lachance. Not really the expected results.
Why is this occurring?
The default encoding for the response stream is UTF-8 (8-bit UCS/Unicode Transformation Format) and Excel will not detect it due to the lack of BOM (Byte Order Mark). Instead, it will try to open the file using the ASCII encoding. It appear to be a known bug and the fix for it is easy.
Response.ContentEncoding = Encoding.Unicode;
or
Response.Write("\uFEFF");
Just before writing to the stream.
HTTP Error 404.3 - Not Found
The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map.
I got this error when trying to let user download .dwg files in IIS 7.5 but a MIME map was not created. Any uncommon files will get you this error. Fortunately, it’s very easy to do.
Open IIS and navigate to your site. Now click on MIME Type (shown below).
Right click and select Add (or click Add… in the right-side menu). You are now presented with a dialog requesting you to enter your filename extension and the MIME type (list of possible MIME types). This will enable you to associate a file extension with a type of content.
H2R77MC2M35K
Edit : This is an old post back from november 2008 on my old blog. Since I had this problem today, I though it would be a good idea to share it again with all my readers.
Going back to standard ASP.NET Web Forms was ridiculously hard. In the last months, I have been completely blown away by the limitless (almost) possibilities of the ASP.NET MVC framework and now I find myself struggling very hard to make a single dropdownlist works the way I wanted to. The scenario was simple, in the client page, I used the value supplied by the drowdown list item to make an AJAX query and display the incoming results somewhere else in the page. That should be very easy right? Well, no. You may not know this but the value attribute of the ASP.NET CheckBoxList's items is not rendered at all.
I know that there is multiple solutions to render it, but that's not the point. I can't find a single reason to not let the developer decide if he want to display it or not. I even heard on some forum (I did not test it myself) that you cannot add a custom attribute named "value" (you can add custom attributes to any controls that will be rendered in the final html) without it to be stripped by the ASP.NET WebForms engine.
Anyway, let's see the solution to this problem.
First of all, we need to create a custom control that will inherit from the System.Web.UI.WebControls.CheckBoxList and that will implement the System.Web.UI.WebControls.IRepeatInfoUser. We use the base class CheckBoxList as we want to reuse existing behavior without implementing it ourselves. As for the IRepeatInfoUser interface, we use it because we are implementing a control that has list items.
public class CheckBoxListFixed : CheckBoxList, IRepeatInfoUser { void IRepeatInfoUser.RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer) { } }
Inside the RenderItem method we will use the HtmlTextWriter to output all the wanted html. So we want to create an input of type checkbox:
writer.WriteBeginTag("input"); writer.WriteAttribute("type", "checkbox"); writer.Write(">"); writer.Write(Items[repeatIndex].Text); writer.WriteEndTag("input");
This will render only basic checkboxes.
<table id="checkboxList" border="0"> <tr> <td><input type="checkbox">1</input></td> </tr> <tr> <td><input type="checkbox">2</input></td> </tr> <tr> <td><input type="checkbox">3</input></td> </tr> <tr> <td><input type="checkbox">4</input></td> </tr> <tr> <td><input type="checkbox">5</input></td> </tr> <table>
With no IDs or attributes. Let's add those to each checkbox.
writer.WriteBeginTag("input"); writer.WriteAttribute("type", "checkbox"); writer.WriteAttribute("name", UniqueID + this.IdSeparator + repeatIndex.ToString(NumberFormatInfo.InvariantInfo)); writer.WriteAttribute("id", ClientID + this.ClientIDSeparator + repeatIndex.ToString(NumberFormatInfo.InvariantInfo)); if (Items[repeatIndex].Selected) writer.WriteAttribute("checked", "checked"); System.Web.UI.AttributeCollection attrs = Items[repeatIndex].Attributes; foreach (string key in attrs.Keys) { writer.WriteAttribute(key, attrs[key]); } writer.Write(">"); writer.Write(Items[repeatIndex].Text); writer.WriteEndTag("input");
We have also added the "checked" to each checkbox. Now we are ready to insert the "missing" value attribute. Adding the following line will resolve the problem.
writer.WriteAttribute("value", Items[repeatIndex].Value);
Complete control code :
namespace yourNamespace { public class CheckBoxListFixed : CheckBoxList, IRepeatInfoUser { void IRepeatInfoUser.RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer) { writer.WriteBeginTag("input"); writer.WriteAttribute("type", "checkbox"); writer.WriteAttribute("name", UniqueID + this.IdSeparator + repeatIndex.ToString(NumberFormatInfo.InvariantInfo)); writer.WriteAttribute("id", ClientID + this.ClientIDSeparator + repeatIndex.ToString(NumberFormatInfo.InvariantInfo)); writer.WriteAttribute("value", Items[repeatIndex].Value); if (Items[repeatIndex].Selected) writer.WriteAttribute("checked", "checked"); System.Web.UI.AttributeCollection attrs = Items[repeatIndex].Attributes; foreach (string key in attrs.Keys) { writer.WriteAttribute(key, attrs[key]); } writer.Write(">"); writer.Write(Items[repeatIndex].Text); writer.WriteEndTag("input"); } } }
And here is a bonus : How to use this control in my code?
Supposing the control isn't in another library and that you cannot add it to your toolbox, you can always use the Register directive inside your aspx file.
<%@ Register TagPrefix="yourPrefix" Namespace="yourNamespace" %>
Next, you can declare your control with the format
<tagprefix:CheckboxListFixed runat="server" id="yourID" />
I have a basic scenario which i'm required to display a page where an user can change his user information, including his password. I decided to use an ASP.NET Textbox control which have a TextMode of password. But, when I put the password in the Text property, nothing will be displayed. The value will also be cleared after a postback event.
There is an easy workaround, you can add the "value" attribute with the password as your value.
txtPassword.Attributes.Add("value", "strongPassword");
But why can't we just set the value the normal way? Basically, it's for security reasons. The password will be set as clear text in the HTML and malicious user could have access to it. This is why you need to stay away from this method if you store sensitive information.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: Cannot have multiple items selected in a DropDownList. Source Error:
Line 253: total = parseFloat(lblTotal.innerHTML.replace('$', '').replace(',', '.'));Line 254: fBudget = parseFloat(budget.replace('$', '').replace(',', '.'));Line 255: var lblWarningContract = document.getElementById('<%= lblWarningContract.ClientID %>');Line 256: Line 257: if (total > fBudget)
I spent at least one hour figuring this out. The Source Error does not contain any valid information. After a lot of debugging, I finally found the solution : you should never use the same item twice on two different dropdownlist.
ListItem item = new ListItem(" -- Select one -- ", "-1");ddlLoadingContact.Items.Insert(0, item);ddlUnloadingContact.Items.Insert(0, item);
A ListItem have a property called Selected that tells which one (the item) the dropdownlist should display as selected. If you use the same ListItem in more that one dropdownlist and you select this item in the DropDownA and a different item in DropDownB. The DropDownB will have two ListItem with the Selected property set to true. Since this is the same item in both dropdownlist.
ddlLoadingContact.SelectedValue = "-1"; //item will have the Selected property set to trueddlUnloadingContact.SelectedValue = "John"; //another item will have the Selected property set to true. And both are contained in this dropdownlist.
Lastly I have got to play a lot with AJAX (ATLAS) and have been pleasantly surprised by the quality and the ease of use of the product. Really easy to use and "a lot of support" for it. For every problem there is a solution and those seem to be resolved fast by the community.
The first really big error so far, is the 'Sys' is undefined one. There seems to have a problem with the anonymous user being disable for the web site (in particular the script folder). Well, I'm sure that I wont allow anonymous users to access critical data, so adding a new location in the Web.Config for this specific folder with solve the problem.
Here is the lines to add :
<location path="ScriptResource.axd"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web> </location>
There can be also the fact that the resource handler is not set, so adding the following line in the Web.Config, should also solve the problem :
<add verb="GET" path="ScriptResource.axd" type="Microsoft.Web.Handlers.ScriptResourceHandler" validate="false"/>
Every time I set the SelectedValue of a DropdownList in ASP.NET, I was checking if the value existed in the control and if it doesn't, I usually set a default one (creating a new item with a SelectedIndex of -1). I realized that if you set the FormattingEnabled to true, the SelectedIndex will be set to -1. Thus removing the need for the this check.
This property is found on the ListControl class, with the Dropdownlist inherit.
Happy coding !