Thursday, 19 January 2012

Outputing the url value of a SharePoint hyperlink field

So as i was working on a SharePoint project i had, i needed to display an image i get from the a hyperlink field inside a repeater.
The problem was that i kept getting the link twice, and i was burning all my brain cell and Google capabilities trying to find a normal solution(parsing the string is not an optimal solution obviously), after a long long search, i came upon this lovely link.
And from there, it was fairly simple, despite my dislike for doing so many unnecessary casting, the end code is as follows:

<img src="<%# (new SPFieldUrlValue(((SPListItem)Container.DataItem)["ChildPicture"].ToString()).Url) %>" alt="ChildPicture" />

Thursday, 15 December 2011

How to open office files inside current window

I had a bug today when a program i was working on didn't work, after fixing the file associations, it worked for windows xp, but one of my clients need it to work for windows 7, however in windows 7 there is no real file association menu.
This fix will help you mostly if you are trying to open office files in the same browser window, and:
a) it opens in new office document.
b) it prompt is not displayed(it might be displayed after a few alt+tab)

This will fix it, there will be no prompt of "what do you want to do with", or "do you want to save or open this file", nor will it attempt to open it in a word window, instead it will open in the browser / WebBrowser window.

The fix is simple, make a text file, copy the next content, rename to something.reg, and run. you can incorporate it into the installer of you program to make sure the clients run it.
content:


Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Word.Document.8]
"BrowserFlags"=dword:80000024

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Word.RTF.8]
"BrowserFlags"=dword:80000024

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Word.Document.12]
"BrowserFlags"=dword:80000024

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Word.DocumentMacroEnabled.12]
"BrowserFlags"=dword:80000024

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.Sheet.8]
"BrowserFlags"=dword:80000A00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.Sheet.12]
"BrowserFlags"=dword:80000A00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.SheetMacroEnabled.12]
"BrowserFlags"=dword:80000A00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.SheetBinaryMacroEnabled.12]
"BrowserFlags"=dword:80000A00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.Show.8]
"BrowserFlags"=dword:800000A0

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.Show.12]
"BrowserFlags"=dword:800000A0

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.ShowMacroEnabled.12]
"BrowserFlags"=dword:800000A0

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.SlideShow.8]
"BrowserFlags"=dword:800000A0

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.SlideShow.12]
"BrowserFlags"=dword:800000A0

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.SlideShowMacroEnabled.12]
"BrowserFlags"=dword:800000A0


[HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\AttachmentExecute\{0002DF01-0000-0000-C000-000000000046}]
"Word.Document.8" =dword:0
"Word.RTF.8" =dword:0
"Word.DocumentMacroEnabled.12"=dword:0
"Word.Document.12"=dword:0
"Word.DocumentMacroEnabled.12"=dword:0
"Excel.Sheet.8"=dword:0
"Excel.Sheet.12"=dword:0
"Excel.SheetMacroEnabled.12"=dword:0
"Excel.SheetBinaryMacroEnabled.12"=dword:0
"PowerPoint.Show.8"=dword:0
"PowerPoint.Show.12"=dword:0
"PowerPoint.ShowMacroEnabled.12"=dword:0
"PowerPoint.SlideShow.8"=dword:0
"PowerPoint.SlideShow.12"=dword:0
"PowerPoint.SlideShowMacroEnabled.12"=dword:0




Of course you can expand this for other files types, just add the names they are defined under in the registry.
If you are wondering, the first half ( up to [HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\AttachmentExecute\{0002DF01-0000-0000-C000-000000000046}]) is about the new window, the other is fixing the prompt.

The following KB resources were used:
http://support.microsoft.com/kb/927009
http://www.winhelponline.com/blog/reset-the-always-ask-before-opening-this-type-of-file-setting/

Sunday, 22 May 2011

Quick post: png in IE7

After i lost the last hour and a half trying to figure out why my site was crawling to a halt and using 100% of a CPU(actually 25% as i'm using a quad core), i figured out the problem.

NEVER. EVER. use a background repeat with a .png image, it seems that using the .png repeat made the .png engine in IE7 render each repeated item individually, making a large transparent background a living hell for the CPU.

the best method to avoid it is not using a smaller image with repeat, and resizing the image to full size, or just use .gif, as it provide transparency, and also works on the old(and bad) IE6.

p.s.
i hate you Microsoft.

Tuesday, 22 February 2011

Why is my span tag acting all freakishly?

When you try to design component, you will sometimes need to use the design tag as inline, and sometimes as block, for that purpose there are two tags, the span and the div.

The <span> is used for inline elements, elements you want to use while inline, meaning they do not break new line after.
The <div> is used for block elements, usually used for dividing the page into seperate parts, such as header, main zone, footer etc. as such, it will start a new line after it is completed.
There are other differences, such as inline not receiving height properties.

Of course both can be turned into the other type of display, simply by using the css property "display" to inline or block, or even better, the display type of inline-block, letting your inline items have a height and enjoy the best of both world(and enjoying the havoc of trying to fix the design for IE, as it tends to display it differently for each version).
For example: <div style="display:inline;">.

However, you should note, span is defaulted for inline, as such, it is supposed to contain some content, so if you use it for design(such as using it for a background, with no text inside) it will act weird, the best way to fix it is by adding some content, and the best content? a space that will not be ignored, achieved by the the " &nbsp; " key-word(without the quotation).
Oh, and do take note, if your span is across several lines(not starting and ending in the same line), the line break may cause some chaos, so it is best to keep it in one line, or commenting out the new line(simply start a comment at the end of the line, and end it at the beginning of the first element in the next line).

Sunday, 20 February 2011

fixing asp:menu for google chrome asp.net

As some of you using asp.net might notice, Google Chrome has a quirk, it likes simplifying the html code. it isn't very noticeable, most of you will never notice, but when you use the control, the css just doesn't seem to work right for Chrome, the fix for this is to add the following code to your base.master code, or if you dont have a master, to the page's code.

protected override void AddedControl(Control control, int index)
{
if (Request.ServerVariables["http_user_agent"]
.IndexOf("Safari", StringComparison.CurrentCultureIgnoreCase) != -1)
{
this.Page.ClientTarget = "uplevel";
}
base.AddedControl(control, index);
}

In case you are wondering why i use the safari as the user agent, that is because Safari and Chrome use the same rendering engine, the WebKit, thus making this fix good for both browsers.
In case you are using Safari and annoyed about only talking about Chrome, don't be insulted that i only talk about Google's Chrome, after all, chrome is 5 times more poplar (Chrome's market share is at around 13% of all browsers usage, while Safari is only at around 2%)

Friday, 8 January 2010

Stocks Displayer c#

I decided i wanted to be able to know what the stocks are for the yen to my local currency(shekel), but didn't like the bank`s graph(they put days that do not have trade on the graph), so i took the raw data, and made a graph.
Had to create a list for easy checking of the parsed data.

It might be interesting to someone that wants to know how to make a graph on c# or how to parse data without regex.
public partial class Form1 : Form
{
List<data> lst=new List<data>();

public Form1()
{

InitializeComponent();
string url="http://www.bankisrael.gov.il/eng.shearim/select_cur/cur4sdh.php?curname%5B%5D=_31%2B1977-10-31%2B%2BYen+-+Japan%A0&numselect=1&curfile=htm&fyear=2008&fmonth=12&fday=31&lyear=2009&lmonth=12&lday=31&cboday=01&cboyear_from=2008&cboyear_to=2009&lastwom=93&whosend=last";
string shitload;
System.Net.WebClient wc=new System.Net.WebClient();
shitload=wc.DownloadString(url);

//parsing
shitload=shitload.Remove(0,shitload.IndexOf("Exchange rates - Last 3 months"));
shitload=shitload.Remove(0,shitload.IndexOf("<TABLE width"));
shitload=shitload.Remove(shitload.IndexOf("</table>"));
string date;
try{
while (shitload.Length>100)
{
shitload=shitload.Remove(0,shitload.IndexOf("nowrap>")+7);
date=shitload.Substring(0,10);

shitload=shitload.Remove(0,shitload.IndexOf("nowrap>")+7);

lst.Add(new data(date,shitload.Substring(0,8)));

}
}
catch{}

label1.Text=shitload;

foreach (data dta in lst)
listBox1.Items.Add(dta.date+" "+dta.value);


this.Paint += new PaintEventHandler(paintgraph);


}
private void paintgraph(object sender,PaintEventArgs e)
{
Graphics g=e.Graphics;
g.Clear(Color.Wheat);
Pen pn=new Pen(new SolidBrush(Color.Red),2);
float min,max;
int mini=0,maxi=0;
min=100;max=0;
for (int i=0;i<lst.Count;i++)
{
if (lst[i].value<min) {min=lst[i].value;mini=i;}
if (lst[i].value>max) {max=lst[i].value;maxi=i;}
}
int inc=this.Width/lst.Count;
float dif=max-min;
float incy=(this.Height-40)/dif;
for(int i=0;i<lst.Count-1;i++)
{
g.DrawLine(pn,i*inc,(-lst[i].value+max)*incy,(i+1)*inc,(-lst[i+1].value+max)*incy);
Application.DoEvents();
}
if (lst.Count-1!=maxi)
g.DrawString(lst[maxi].date+" "+lst[maxi].value,new Font("Verdana",10),new SolidBrush(Color.Tomato),maxi*inc,(-lst[maxi].value+max)*incy);
else
g.DrawString(lst[lst.Count-1].date+" "+lst[lst.Count-1].value,new Font("Verdana",10),new SolidBrush(Color.Tomato),(lst.Count-1)*inc-130,(-lst[lst.Count-1].value+max)*incy);
if (lst.Count-1!=mini)
g.DrawString(lst[mini].date+" "+lst[mini].value,new Font("Verdana",10),new SolidBrush(Color.Tomato),mini*inc,(-lst[mini].value+max)*incy-20);
else
g.DrawString(lst[lst.Count-1].date+" "+lst[lst.Count-1].value,new Font("Verdana",10),new SolidBrush(Color.Tomato),(lst.Count-1)*inc-130,(-lst[lst.Count-1].value+max)*incy-20);
//if ((lst.Count-1!=maxi)&&(lst.Count-1!=mini))


}


void Form1ResizeEnd(object sender, EventArgs e)
{
this.Refresh();
}
}

public class data
{
public string date;
public float value;
public data(string d,string v)
{
date=d;
value=(float)System.Convert.ToDouble(v);
}
}

Wednesday, 6 January 2010

PerformanceCounter c#

OK, after the last try to find some intresting data with the PerformanceCounter, i wanted to see what other counter it has, so i made a program to show me all the option, or at least the ones that are easy to find out, so i came up with this program:

Unlike the previous programs, this one is not a simple "throw everthing at the poor label", you will need:
-3 listboxs, each with a ListBox1SelectedIndexChanged(object sender, EventArgs e) event.
-and of course, one label to "throw everthing at"
Should look like this if you want:














Now, for the much more intresting code(not all that intersting, but the data you can get from the code is) :


void MainFormLoad(object sender, EventArgs e)
{
foreach (PerformanceCounterCategory pcc in PerformanceCounterCategory.GetCategories())
listBox1.Items.Add(pcc.CategoryName);
}

void ListBox1SelectedIndexChanged(object sender, EventArgs e)
{
int flag=1;
listBox2.Items.Clear();
listBox3.Items.Clear();
PerformanceCounterCategory pcc=new PerformanceCounterCategory(listBox1.SelectedItem.ToString());
foreach (string insname in pcc.GetInstanceNames())
{
flag=0;
listBox2.Items.Add(insname);
}
if (flag==1)
{
foreach (PerformanceCounter pc in pcc.GetCounters())
{
listBox3.Items.Add(pc.CounterName);
}
}
}

void ListBox2SelectedIndexChanged(object sender, EventArgs e)
{
listBox3.Items.Clear();
if (listBox2.SelectedItem==null) return;
PerformanceCounterCategory pcc=new PerformanceCounterCategory(listBox1.SelectedItem.ToString());
try{
foreach (PerformanceCounter pc in pcc.GetCounters(listBox2.SelectedItem.ToString()))
{
listBox3.Items.Add(pc.CounterName);
}
}
catch {label1.Text="instance invalid";}
}

void ListBox3SelectedIndexChanged(object sender, EventArgs e)
{
try{
PerformanceCounter pc;
if (listBox2.SelectedItem==null)
pc=new PerformanceCounter(listBox1.SelectedItem.ToString(),listBox3.SelectedItem.ToString());
else
pc=new PerformanceCounter(listBox1.SelectedItem.ToString(),
listBox3.SelectedItem.ToString(),listBox2.SelectedItem.ToString());
pc.NextValue();//for some, the first is always 0
label1.Text=pc.NextValue().ToString();
if ((label1.Text=="0")||(label1.Text=="100"))
{
float activity = 0;
int NormalDegree = 20;
for (int i = 0; i < NormalDegree; i++)
{
activity += pc.NextValue();
Application.DoEvents();
System.Threading.Thread.Sleep(20);
}
label1.Text=(activity/NormalDegree).ToString("g3");
if (listBox3.SelectedItem.ToString().StartsWith("%")) label1.Text+="%";
}
}
catch
{
label1.Text="counter invalid";
}
}