Tuesday, December 18, 2012

Prevent a postback from triggering on a button control when the Enter key is pressed

If you would like to prevent a postback from occurring on an ASP button control whenever the user presses the "Enter" key, one possible option for overriding this functionality is to add "UseSubmitButton = false" as an attribute/property of the appropriate button controls as follows:

<asp:Button id="btnSubmit" runat="server" UseSubmitBehavior="false" ... />

or

Button btnSubmit = new Button();
btnSubmit.UseSubmitBehavior = false;

In case you're curious, the reason we were asked to do this was due to a problem that our users were encountering with a postback being triggered on the "wrong" command button in our gridview control whenever a user would hit the "Enter" key.  Essentially, our gridview's rows contained several command button controls that, when clicked, would perform a variety of activities on the given row depending on which button was clicked.  Naturally, the first button on the selected row, the button with the focus, would be triggered whenever the user hit the "Enter" key; therefore, we all came to conclusion that the best route was to simply disable the "Enter" key postback functionality across all of the button controls contained in the gridview.

Monday, December 17, 2012

SharePoint List Filter web parts not displaying on a SharePoint 2010 Page in Edit mode

Our Business Analyst and I recently ran into an interesting problem in which we were attempting to install a couple of SharePoint List Filter and Apply Filters Button web parts on a page that would help users filter items in a connected list.


After adding the required web parts, our BA noticed that these controls were not appearing on the page as he would have expected despite the fact that the page was undoubtedly in Edit mode.  As a result, he wasn't able to configure the newly added filter web parts as well as set their connections to the associated list web part.


Anyway, I just happened to place the list view web part that these parts were supposed to connect to in Edit Web Part mode and, voilĂ , all of the filtered web parts suddenly appeared on the page.



At that point, our BA was able to proceed with his configuration and connection work without issue.


Thought it would be nice to share just in case anyone out there happens to encounter the same situation.

Tuesday, December 11, 2012

Obtaining custom configuration settings from a web.config for a Visual Web Part

If you're looking for a possible route for obtaining special configuration settings/information for a Visual Web Part that will be consistent across all sites contained under a given SharePoint Web Application, one possible route is to reference those configuration settings from the <appSettings> node of the web application's web.config file.  NOTE:  Only pursue this route if you know beyond a shadow of a doubt that the config settings will be consistent across all sites contained within the given web application.  If there is even a remote chance that you'll need to adjust those settings for each given site, you will wish to pursue another route for configuring your web part such as adding a custom property.  With that said, I'll now proceed with the steps you can use to make this possible:

Add the Custom Configuration Settings to the Web.config

Before you deploy you're web part, you will wish to open the web.config files for each of the SharePoint web applications under which your SharePoint web part will be deployed and add the new configuration setting to the <appSettings> node.  On your SharePoint Web Front End (WFE) server(s), the web applications will be located under the C:\inetpub\wwwroot\wss\VirtualDirectories directory and the web.config file will be located immediately under each given web application.  In this particular instance, I'm going to reference a custom setting called "dbConnectionString" which is presented as follows:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<configuration>
  <configSections>
  ...
  <appSettings>
    <add key="dbConnectionString" value="Data Source=dbServer;Initial Catalog=dbName;Integrated Security=True" />
  </appSettings>
</configuration>

After making the change, be sure to save the updated web.config file and, now, we move on to actually putting this info to good use...

Referencing the Custom Configuration Settings

To reference the custom configuration settings in your Visual Web Part, you will need to carry out the following steps:
  1. Open your Visual Web Part in Visual Studio
  2. In Solution Explorer, expand the Project tree, right-click on References and select Add Reference...
  3. Click on the .NET tab
  4. Locate and click on the System.Configuration component
  5. Click OK
  6. Open your Visual Web Part's .ascx.cs file
  7. Add the following code that will reference the custom configuration setting:
using System.Configuration; using System.Web.Configuration; ...
protected void Page_Load(object sender, EventArgs e) {     if (!IsPostBack)     {         string dbConnectionString = string.Empty;         string errorMessage = string.Empty;         try         {             using (SPSite site = SPContext.Current.Site)             {                 Configuration config = WebConfigurationManager.OpenWebConfiguration("/", site.WebApplication.Name);                 if (config.AppSettings.Settings["dbConnectionString"] != null)                 {                     dbConnectionString = config.AppSettings.Settings["dbConnectionString"].Value;                 }                 else                 {                     throw new Exception(string.Format("The 'dbConnectionString' <appSettings> node could not be located in the web application's, {0}, web.config file.", site.WebApplication.Name));                 }             }             ... Do something with the configuration info         }         catch (Exception ex)         {             ...Handle the exception properly         }     } }
At this point, you can now deploy your web part to sites contained under the web applications that you added the custom setting to (via updating the web.config file, of course) and they will reference the value that you've specified.