Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Showing posts with label C# String. Show all posts
Showing posts with label C# String. Show all posts

Tuesday, December 23, 2008

Modifying DataBound Hyperlinks Text

Modifying a GridView's Databound Hyperlink text since I couldn't do this easily in the ItemTemplate, I moved the Replace function to the DataBound of the GridView:


protected void GridViewStocksInLists_DataBound(object sender, EventArgs e)
{
int rowId;
string imgUrl;
string navUrl;
for (rowId=0; rowId<GridViewStocksInLists.Rows.Count;rowId++) {
imgUrl = string.Empty;
navUrl = string.Empty;
imgUrl = ((HyperLink)GridViewStocksInLists.Rows[rowId].FindControl("hlChart")).ImageUrl.Replace('$', '^');
((HyperLink)GridViewStocksInLists.Rows[rowId].FindControl("hlChart")).ImageUrl = imgUrl;
navUrl = ((HyperLink)GridViewStocksInLists.Rows[rowId].FindControl("hlChart")).NavigateUrl.Replace('$', '^');
((HyperLink)GridViewStocksInLists.Rows[rowId].FindControl("hlChart")).NavigateUrl = navUrl;
}
}
This replaced $ with ^ in my HyperLink.

Here is the GridView that contains the HyperLink:

<asp:GridView ID="GridViewStocksInLists" runat="server" DataSourceID="SqlDataSourceGetStocksInList">
<RowStyle CssClass="tableAnalysisRow" />
<HeaderStyle CssClass="tableAnalysisHeader" />
<AlternatingRowStyle CssClass="tableAnalysisAlternatingRow" />
<Columns>
<asp:TemplateField HeaderText="Chart">
<ItemTemplate>
<asp:HyperLink ID="hlChart" runat="server"
ImageUrl='<%# Eval("Symbol", "http://chart.finance.yahoo.com/c/0b/d/{0}").ToLower() %>'
NavigateUrl='<%# Eval("Symbol", "http://finance.yahoo.com/q/bc?s={0}&t=1y").ToLower() %>' >
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

References: http://forums.asp.net/p/1263726/2542303.aspx#2542303

Remove HTML Tags

Remove HTML Tags from a string:

public static string RemoveHtml(string txt)
{
return Regex.Replace(txt, @"<[^>]*>", string.Empty);
}


Reference: http://www.webpronews.com/expertarticles/2006/12/01/aspnet-remove-html-tags-from-a-string

Monday, May 5, 2008

Resetting a StringBuilder

Here is how to reset a StringBuilder:


myStringBuilder.Length = 0;
myStringBuilder.Remove(0, myStringBuilder.Length) also works, but why bother with that one?
References: http://forums.asp.net/t/1193708.aspx

Friday, December 14, 2007

C# string is null or empty

Here's another shortcut method to make sure the string is not null and not empty. What I find interesting is that using myString.length>0 is supposedly more efficient than myString != string.empty.

if (!String.IsNullOrEmpty(myString))
{
myString = "myString != null && myString != string.Empty";
}

C# string extract tag method

This extracts a tagged string. For example, string findTagValue = extractTag ("Foo", "", ""); This would fetch Foo.

private string extractTag(string original, string beginTag, string endTag)
{
string retVal = string.Empty;
int begin = original.IndexOf(beginTag);
int end = -1;
if (begin > 0)
{
end = original.IndexOf(endTag, begin);
}
if ((begin > 0))
{
if ((end > begin))
{
retVal = original.Substring((begin+beginTag.Length), (end - begin-endTag.Length));
}
}
return retVal;
}