Skip to main content

ASP .NET İpuçları

“Oluşan Hatayı Mail İle Kendinize Gönderin”

Çalışan ASP .NET uygulamanızda hata oluştuğu zaman bu hatadan anında haberdar olmak için aşağıdaki kod parçası işinizi görecektir. Bu kodu Global.asax dosyasında Application_Error event’ına yazmalısınız.

protected void Application_Error(Object sender, EventArgs e)
{
Exception ex = Server.GetLastError().GetBaseException();
string err = "Hata Oluştu !"
+ System.Environment.NewLine
+ "Hata: " + Request.Url.ToString()
+ System.Environment.NewLine
+ "Hata Mesajı: " + ex.Message.ToString()
+ System.Environment.NewLine
+ "Stack Trace: " + ex.StackTrace.ToString();

// Server.ClearError();

System.Web.Mail.MailMessage mm = new System.Web.Mail.MailMessage();
mm.To = "mgedik@verivizyon.com";
mm.From = " mgedik@verivizyon.com ";
mm.Subject = "Sistemde Hata !";
mm.Body = err;

try
{
System.Web.Mail.SmtpMail.SmtpServer = "mail.verivizyon.com";
System.Web.Mail.SmtpMail.Send(mm);
}
catch
{
}
}


“TextBox’a Girilen Değerin Rakam Olduğunu Garantileyin”

Bir TextBox nesneniz var ve siz bu alana sadece rakam girilebilmesini istiyorsunuz. O zaman aşağıdaki vbscipt kodu işinize çok yarayacaktır.

[script language="vbscript"]

function ParseInt(StringM)
dim Reg, MatchInt, IntMatch, ParsedString
Set Reg = CreateObject("VBScript.RegExp")
Reg.Global = True
Reg.IgnoreCase = True
Reg.Multiline = True
Reg.Pattern = "\d"
set MatchInt = Reg.Execute(StringM)

for each IntMatch in MatchInt
ParsedString = ParsedString & IntMatch.value
next

ParseInt = ParsedString
end function

‘Bu fonksiyonu şu şekilde çağırabiliriz.
‘(tbSayi nesnesinin “onkeyup” event’ında Validate()’i çağırmalısınız.)

function Validate()
Form1.tbSayi.value = ParseInt(Form1.tbSayi.value)
end function

[/script]


“Komut Satırı Uygulamalarını Gizlice Çalıştırarak Çıktısını Ekrana Yazdırın”

Komut satırı uygulamalarını gizli bir şekilde çalıştırarak, çıktısını web tarayıcısında göstermek gibi bir ihtiyacınız doğarsa ASP .NET size bu konuda da yardımcı oluyor. Belki çıktıyı web tarayıcıda göstermek çok işe yaramayabilir ama bunu bir string değişkenine atayıp, üzerinde biraz çalışarak işinize yarayabilecek verilere erişebilirsiniz.

public string ExecuteApp(string cmd)
{
string result = "";
string output = "";
string errors = "";

// çıktı ve hata mesajları için tempfilecollection oluşturuluyor
System.CodeDom.Compiler.TempFileCollection tf = new System.CodeDom.Compiler.TempFileCollection();

// komut çalıştırılıyor
System.CodeDom.Compiler.Executor.ExecWaitWithCapture(cmd, tf, ref output, ref errors);

// komutun çalışması sonucu dönen içerik okunuyor
System.IO.StreamReader sr = System.IO.File.OpenText(output);
result = sr.ReadToEnd();
sr.Close();

// geçici dosyalar siliniyor
System.IO.File.Delete(output);
System.IO.File.Delete(errors);

return result;
}


Bu metodu şu şekilde çağırabilirsiniz:

Response.Write(ExecuteApp("netstat -a -n"));
Response.Write(ExecuteApp("ipconfig"));


“Sunucu Taraflı Kodunuzda Nesnelerinize Focus’lanın”

Bildiğiniz gibi ASP .NET Code Behind’da istemci taraflı script kodlarımızı çalıştırabilmemize imkan sağlıyor. Bundan faydalanarak yazabileceğimiz basit bir metot ile sunucu taraflı kodumuzda nesnelerimize kolaylıkla konumlanabiliriz.

private void Page_Load(object sender, System.EventArgs e)
{
if(! IsPostBack) this.SetFocus(TextBox1);
}

private void SetFocus(System.Web.UI.Control ctrl)
{
string s = "[script language = Javascript]";
s += "document.getElementById('" + ctrl.ID + "').focus();";
s += "[/script]";

Page.RegisterStartupScript("Focus", s);
}

private void Button1_Click(object sender, System.EventArgs e)
{
SetFocus(TextBox2);
}

Comments

Popular posts from this blog

DigiTurk Magazine Reader

DigiTurk is the first and only fully digital satellite television provider in Turkey. In addition to satellite TV for millions of customers in Turkey, they publish magazines and run DigiWeb portal. Like many other magazing publishers, DigiTurk saw value in using Silverlight to provide a richer reading experience. But they took the idea a step further, and built a tool which can be used by any magazine publisher to create Silverlight-based magazines. Tim Sneath sat down with the team from DigiTurk to get a demo and hear about how they built this cool solution. http://www.visitmix.com/blogs/news/digiturk/

Ajax with the ASP.NET MVC Framework

Hopefully everyone had a good few days off. Before the holiday break, I did some app-building on top of the ASP.NET MVC framework. Actually rather than building some sort of fancy app, instead I was prototyping some features on top of the framework bits slated for an initial release. I've shared out the sample code, sample app and tests - yes, sorry for another tease :-)... but stay tuned... and you'll soon have actual bits to play with as well. Until then, you can download the sample code and browse it locally, and follow along the rest of the post. In particular there are two projects within the solution: TaskList (the web app) and AjaxMVC (a class library with Ajax extensions). One of the prototypes is around bringing some basic Ajax functionality - basically to get post-back-less partial rendering and some behavior-like extensions to associate with DOM elements - sort of like ASP.NET Ajax but in a manner that fits with the pattern around how controllers and views are writte...

Oracle Forms ile Web Servislerine Erişim

Geçtiğimiz günlerde çalıştığım şirket, başka bir yazılım şirketine teknolojik destek vermek için beni görevlendirdi. Oracle Forms Developer kullanılarak geliştirilmiş bir hastane otomasyonun bazı web servislerini kullanması gerekiyordu. 3-4 günlük bir çalışmanın ardından görevimi başarıyla tamamladım çok şükür. Yalnız gerçekten bu çok kolay olmadı. Çünkü Oracle Forms direkt olarak web servislerine erişmek için bir teknoloji barındırmıyordu bünyesinde. Yaptığım araştırmalar sonucunda Oracle Forms'un Java class'larını import edip, kullanabildiğini gördüm. İşte bu durumda problemi çözmüş olduğumu hissettim. Ama yinede beni bekliyen engeller vardı. Bundan emindim çünkü hiç bilmediğim bir ortamda bilmediğim kodları yazacaktım. Ayrıca java konusunda bilgi sahibi olsamda çok tecrübeli değildim ve uzun zamandır java ile ilgilenmemiştim. Ve düşündüğüm gibi birçok problem çıktı karşıma. Ama yinede bu problemleri tek tek aşıp çözüme gitmeyi başardım. Bir Murphy kanunu: "Eğer çıkması ...