Contents tagged with ASP.NET
-
Certificate error with Web Platform Installer
A friend of mine was having an issue getting the Web Platform Installer to work on his Windows Server 2008 R2 box. He said there was some sort of cert error and asked me to try https://go.microsoft.com/fwlink/?LinkId=158722 on my local machine to see if I got the cert error. I tried it and I did get a cert error on Windows 7 64bit. I happened to notice that that url simply redirects to https://www.microsoft.com/web/webpi/2.0/WebProductList.xml . Out of curiosity I dropped to a command line and tried to run .\WebPlatformInstaller.exe /? to see if there were any command line options. It gave an error that said invalid URI. So we tried running it with the product list url like: "WebPlatformInstaller.exe https://www.microsoft.com/web/webpi/2.0/WebProductList.xml" . This seems to get around the expired cert that is on go.microsoft.com.
-
ASP.NET MVC – Multiple buttons in the same form
I keep seeing this question in forums and on twitter so I thought I’d post all the various ways you can handle this and what the pros and cons are.
-
ASP.NET MVC ModelState should work like TempData
I prefer to have the actions that forms post to just process the posted data and then redirect to a different action for viewing the results. So in order to pass validation errors back to the form action I need ModelState to work like TempData does. In fact it seemed that before ModelState was added that one of the most common scenarios for using TempData was passing validation error messages between actions so I’m not sure why it doesn't already work like this. I’m using RC2 so its doubtful this will change before RTM. :(
-
A Quick Fix for the Validator SetFocusOnError Bug
The ASP.NET validators have this nice property called "SetFocusOnError" that is supposed to set the focus to the first control that failed validation. This all works great until your validator control is inside a naming container. I ran into this recently when using validators in a DetailsView. Take this simple example:<%@ Page Language="C#" %> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) DataBind(); } </script> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="_frm" runat="server"> <asp:DetailsView ID="dv1" DefaultMode="Edit" DataSource='<%# new object[1] %>' runat="server" > <Fields> <asp:TemplateField HeaderText="First Name:"> <EditItemTemplate> <asp:TextBox ID="FirstNameTextBox" runat="server" /> <asp:RequiredFieldValidator ID="FirstNameValidator1" ControlToValidate="FirstNameTextBox" ErrorMessage="First name is required." Display="Dynamic" EnableClientScript="false" SetFocusOnError="true" ValidationGroup="bug" Text="*" runat="server" /> </EditItemTemplate> </asp:TemplateField> </Fields> <FooterTemplate> <asp:ValidationSummary ID="vs1" DisplayMode="List" ValidationGroup="bug" runat="server" /> <asp:Button ID="Button1" Text="Post Back" ValidationGroup="bug" runat="server" /> </FooterTemplate> </asp:DetailsView> </form> </body> </html>
If you run this page and do a view source you'll see that the FirstNameTextBox gets rendered like this:<input name="dv1$FirstNameTextBox" type="text" id="dv1_FirstNameTextBox" />
If you just do a post back without entering a value to cause the validator to fail it will output this line of java script in an attempt to set the focus to the invalid element:WebForm_AutoFocus('FirstNameTextBox');
See anything wrong with this? It would seem that the validators just use the string value you typed in for the ControlToValidate property rather than doing a FindControl and using the UniqueID. This is exactly what happens and I verified it with reflector. The Validate method on BaseValidator does this:if ((!this.IsValid && (this.Page != null)) && this.SetFocusOnError) { this.Page.SetValidatorInvalidControlFocus(this.ControlToValidate); }
If you follow the call to SetValidatorInvalidControlFocus you'll see that it never resolves the full UniqueID of the control that its going to set focus to.Ok, so this sucks. How do I work around it. My solution was to simply ditch using the SetFocusOnError property and implement the focus logic myself which is actually pretty easy. I overrode Validate method on my Page like this:public override void Validate(string group) { base.Validate(group); // find the first validator that failed foreach (IValidator validator in GetValidators(group)) { if (validator is BaseValidator && !validator.IsValid) { BaseValidator bv = (BaseValidator)validator; // look up the control that failed validation Control target = bv.NamingContainer.FindControl(bv.ControlToValidate); // set the focus to it if (target != null) target.Focus(); break; } } }
If your using C# 3 this is even easier using LINQ:
public override void Validate(string group) { base.Validate(group); // get the first validator that failed var validator = GetValidators(group) .OfType<BaseValidator>() .FirstOrDefault(v => !v.IsValid); // set the focus to the control // that the validator targets if (validator != null) { Control target = validator .NamingContainer .FindControl(validator.ControlToValidate); if (target != null) target.Focus(); } }
I hope this saves someone the headache of tracking this down.
-
Fix ReturnUrl When Sharing Forms Authentication with Multiple Web Applications
Scenario: You have two web applications www.mydomain.com and login.mydomain.com. The login site provides a centralized login application and www contains any number of web applications that should use the auth ticket issued by the login site.
-
A VS.NET Macro to Generate Machine Keys.
I needed to create a new machine key for an asp.net site. I found a couple of command line utils out on the web that would create a new key but I thought it would be easier to just have it avail in VS.NET. So, I threw together this little macro that will generate the machine key and insert it. Just run the macro while you have you web.config open in VS.NET. If you already have a machinekey it will find it and replace it. If not it will just add it right after the <system.web> node. It should do the proper indents and everything too.
-
LearnExpression.com is live.
We recently launched our video tutorial site for Expression Web Designer. Dustin, our resident graphic artist is pumping out the video tutorials as well as designing the site using the tool. It's good to have some how to vids from a designers point of view. Watch this site throughout 2007 as he continues to expand the content.
-
Scott Guthrie presents at NDDNUG
Scott gave a whirlwind presentation to a standing room only crowd at the North Dallas Dot Net User Group tonight. A wide range of topics were covered from IDE tips and tricks to ASP.NET tips to MS AJAX to LINQ and DLINQ (I still like to call it DLINQ rather than LINQ to SQL). I'm still not sure how all this got packed into a little over 2 hours. :)
-
ASP.NET AJAX 1.0 (excitement builds)
I just downloaded beta 1 of ASP.NET AJAX today. Wow! Lots and lots of changes. The type system has been greatly simplified. I like this. The move to prototypes over closures was a good decision. The core scripts are much easier to read and comprehend as well. The CTP download has been a 404 for me this evening so I haven't had a chance to dive into it.
-
Problems with RewritePath and Search Engines.
We just recently launched the new version of LearnVisualStudio.NET. The implementation uses the RewritePath method to serve up dynamic pages based on templates. All of our pre deployment testing showed that this was all working without a hitch. Once we deployed to production however it became clear which test cases we had left out. We didn't test how the site responded when getting crawled by search engines.