Jon's Blog

.NET Development & More

ASP.NET: Add New Error Message to Validation Summary Control

You can use this method to add a new error to an existing validation summary control from your code-behind page.  Don't forget to use the proper ValidationGroup that your validation summary is using.

protected void AddErrorToValidationSummary(string errorMessage)
{
CustomValidator custVal = new CustomValidator();
custVal.IsValid = false;
custVal.ErrorMessage = errorMessage;
custVal.EnableClientScript = false;
custVal.Display = ValidatorDisplay.None;
custVal.ValidationGroup = "MyValidationGroup";
this.Page.Form.Controls.Add(custVal);
}

That Pesky Enter Key: JavaScript to the Rescue

OK, this solution was put together Frankenstein style from multiple sources.  Basically I wanted to only allow the user to use the Enter key in specific circumstances.  If they were typing their query in the search box, for example.  This solution uses a combination of JavaScript and the ASP.NET Panel control.  The following JavaScript is placed in the MasterPage:

<script type="text/javascript">
 var allowEnterKey = false;
 window.document.onkeydown = CheckEnter;

 function CheckEnter()
 {
  var keyID = (window.event) ? event.keyCode : e.keyCode;

  if (!allowEnterKey && keyID == 13)
   return false;
  else
   return true;
 }
</script> 

 

Then my search box (or whatever) can be wrapped in a Panel control like so:

<asp:Panel ID="pnlSearch" runat="server" DefaultButton="btnSearch">
    <div id="SearchDiv">
        <asp:TextBox ID="txtSearch" runat="server" Columns="60" 
            onfocus="allowEnterKey=true" onblur="allowEnterKey=false" />
        <asp:Button ID="btnSearch" runat="server" Text="Search" 
            onclick="btnSearch_Click" onfocus="allowEnterKey=true" onblur="allowEnterKey=false" />
    </div>
</asp:Panel>

 

Notice the DefaultButton property set on the Panel as well as the onfocus and oblur properties of the TextBox.

AJAX Control Toolkit: Strange CSS Bug with HTML Editor control and IE 8

Today I was adding the new HTML Editor control (newly added to the AJAX Control Toolkit) to an Intranet page.  Whenever I would mouse click into the HTML Editor it would continue to expand.  This would only occur in IE 8.  When using Firefox or IE 8 in compatibility mode it wouldn't happen.  I tracked it down to the following declaration in my CSS file:

 .white td
{
    padding: 3px;
}

I removed this declaration and instead used the cellpadding attribute for the table.  This solved the issue.  Very strange.