Posts
2
Comments
6
Trackbacks
0
Wednesday, April 01, 2009
FileUpload within UpdatePanel

One of the most well-known (as well as irritating) issues encountered with making use of Asynchronous Postbacks (such as those used with an ASP Update Panel), is that the ASP File Upload control will not work. Most people abstain from using an Update Panel whenever a FileUpload control is to be used. However in some implementations, this is not always a feasible solution.

The reason that a FileUpload will not work is because the FileUpload requires a Full Postback in order to work (as opposed to the Asynchronous Postback). Thus all we need to do is cause a Full Postback on our Submit button. A simple implementation is as follows:

<asp:ScriptManager ID="ScriptManager1" runat="server" />
     <asp:UpdatePanel ID="upd1" runat="server">
           <ContentTemplate>
                   <asp:Button ID="btn1" runat="server" />
                   <asp:FileUpload ID="fupl1" runat="server" />
           </ContentTemplate>
           <Triggers>
                   <asp:PostBackTrigger ControlID="btn1" />                   
           </Triggers>
     </asp:UpdatePanel>

However, this will not work when your Update Panel is within a container control, such as a MasterPage. For example. Let's say that I have an Update Panel on my Master Page, and I wish to create a Postback Trigger for PageX.aspx which happens to have a FileUpload. In order to overcome this method, we may simply write a public method within the Master Page's backend:

public void RegisterPostbackTrigger(Control triggerOn)
{
        ScriptManager1.RegisterPostBackControl(triggerOn);
}

Once we have done this, we need to create a reference to the Master Page, within the Pages that need to make use of this method. We do this by writing:

<%@ Reference VirtualPath="~/MasterPage.master" %>

on the front-end side of our Page, with VirtualPath being the Path of our Master Page. After we do this, we Build, and then go to our Page. Within the Page_Load, we simply need to write:

((ASP.masterpage_master)Page.Master).RegisterPostbackTrigger(btn1);

And there you have it. Now the Submit button will cause a full postback, allowing the File Upload to work as expected. The only caveat is that through Asynchronous Postbacks, the File within the File Upload is lost.


posted @ Wednesday, April 01, 2009 7:48 AM | Feedback (4)
Friday, October 31, 2008
Enum TryParse

 Well this would be my first post on geekswithblogs, and I'm going to start by introducing a couple of tips and tricks in terms of parsing and string manipulation.

 

The following is my first version of an Enum TryParse, which uses Generics to convert a string into an Enum using an out parameter, and returning a boolean verifying whether the string passed can be converted into the Enum or not.

public static bool EnumTryParse<theEnum>(string input, out theEnum returnValue)

{

      if (Enum.IsDefined(typeof(theEnum), input))

      {

            returnValue = (theEnum)Enum.Parse(typeof(theEnum), input, true);

            return true;

      }

 

      returnValue = default(theEnum);

      return false;

}

This is particularly useful for being used in if conditions. The way to use it is as follows:

 

MyEnum m;

 

if (EnumTryParse<MyEnum>("value", out m))
{
     
//do something
}

However, this particular version of my EnumTryParse does not allow for case sensitivity. What if "value" had been written as "Value" within the declaration of the Enum? The solution is to check whether the string equals to a value within the Enum defined, and converting the string input into the case which is required.

 

For this, we need two different string manipulation methods. Case Insensitive String Equals and Case Insensitive String Replacement, which are as follows:

 

public static bool CaseInsensitiveEquals(string input, string pattern)
{
      return input.Equals(pattern, StringComparison.CurrentCultureIgnoreCase);
}

public static string CaseInsensitiveReplace(string input, string pattern, string replacement)
{
      return Regex.Replace(input, pattern, replacement, RegexOptions.IgnoreCase);
}


These two methods allow for some interesting string manipulation which can be used throughout the program, rather than just for the Enum Parser, however that is out of scope for this article. In future articles I shall go into greater depth regarding some string manipulation methods which I have developed.

With these two methods, we can iterate through the values with our enum and when we find a match using the CaseInsensitiveEquals, we can replace the string we inputted with the string which is within the Enum, thus formatting the input into the case which we want.

As such, the final version of the EnumTryParse is as follows:

public static bool EnumTryParse<theEnum>(string input, out theEnum returnValue)
{
     foreach (string s in Enum.GetNames(typeof(theEnum)))
     {
          if (CaseInsensitiveEquals(input, s))
          {
                input = CaseInsensitiveReplace(input, s, s);
                break;
          }
     }

     if (Enum.IsDefined(typeof(theEnum), input))

     {

           returnValue = (theEnum)Enum.Parse(typeof(theEnum), input, true);

           return true;

     }

 

     returnValue = default(theEnum);

     return false;

}

 

posted @ Friday, October 31, 2008 8:42 PM | Feedback (2)
News