20 Dec 2007
I’m using this function to filter common words out of a search query.
protected string removeCommonWords(string sourceStr)
{
string[] seperator = { " " };
string[] ignoreWords = { "a", "all", "am", "an", "and", "any", "are", "as",
"at", "be", "but", "can", "did", "do", "does", "for", "from", "had",
"has", "have", "here", "how", "i", "if", "in", "is", "it", "no", "not",
"of", "on", "or", "so", "that", "the", "then", "there", "this", "to",
"too", "up", "use", "what", "when", "where", "who", "why", "you" };
string[] outputStr = { };
outputStr = sourceStr.ToLower().Split(seperator, StringSplitOptions.RemoveEmptyEntries);
foreach (string unwantedWord in ignoreWords)
{
int index = Array.IndexOf(outputStr, unwantedWord);
if (index != -1)
{
string[] copyStrArr = new string[outputStr.Length - 1];
// copy the elements before the found index
for (int i = 0; i < index; i++)
{
copyStrArr[i] = outputStr[i];
}
// copy the elements after the found index
for (int i = index; i < copyStrArr.Length; i++)
{
copyStrArr[i] = outputStr[i + 1];
}
outputStr = copyStrArr;
}
}
sourceStr = string.Join(" ", outputStr);
return sourceStr;
}
Let me know if you guys have a better solution.
13 Nov 2007
This code will strip out all the HTML tags and truncate the text to 4 lines.
public static string TruncateText(string txtIn, int newLength)
{
string txtOut = txtIn;
string pattern = @"<(.|\n)*?>";
//Strip out HTML tags
if (Regex.IsMatch(txtIn, pattern, RegexOptions.None))
txtOut = Regex.Replace(txtIn, pattern, string.Empty, RegexOptions.Multiline).Trim();
if (txtOut.Length > newLength)
{
int endPos = txtOut.LastIndexOf(" ", newLength);
txtOut = txtOut.Substring(0, endPos) + "...";
}
return txtOut;
}
13 Nov 2007
One way is to use a class like this to add an onkeydown client event to a to a textbox.
using System;
using System.Web.UI.WebControls;
namespace System.Web.UI.DefaultButton
{
/// <summary>
/// Methods to set default button
/// </summary>
class DefaultButton
{
/// <summary>
/// Sets default button for the specified control
/// </summary>
Control name where user hits enter key</param>
Button control to be called</param>
void SetDefaultButton ( TextBox control , Button btButton )
{
control.Attributes.Add("if(event.which ||
event.keyCode){if ((event.which == 13) ||
(event.keyCode == 13)) {document.getElementById('"+ btButton.ClientID+"').click();return false;}}
else {return true}; ");
}
/// <summary>
/// Sets default button as the image button
/// </summary>
Control name where user hits enter key</param>
Imagebutton to be called</param>
void SetDefaultButton ( TextBox control , ImageButton btButton )
{
control.Attributes.Add("if(event.which ||
event.keyCode){if ((event.which == 13) ||
(event.keyCode == 13)) {document.getElementById('"+ btButton.ClientID+"').click();return false;}}
else {return true}; ");
}
}
}
Another easier and quicker alternative is to place all the form elements in an ASP.NET Panel and then define the panel’s default button property, like this:
<asp:Panel runat="btnHello">
First name: <asp:TextBox runat="txtFirstName" />
<asp:ImageButton ID="lbHello_Click" />
</asp:Panel><br /><br />
But of course if you are using a LinkButton, then its a whole another ball game. None of the techniques described above might work for you. But you are in luck, this page has what you need to get around that - Using Panel.DefaultButton property with LinkButton control in ASP.NET
15 Oct 2007
Find a control within a control recursively:
protected void Control FindControlRecursive(Control Root, string Id)
{
if (Root.ID == Id)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
15 Oct 2007
Check to see if all the controls within a specific control are valid:
protected bool checkIsValid(Control c)
{
bool result = true;
foreach (Control child in c.Controls)
{
if (child is BaseValidator && !((IValidator)child).IsValid)
{
return false;
}
result = result & checkIsValid(child);
}
return result;
}