Friday, February 1, 2008

Registering the user control and custom control in the web.config

In asp.Net 1.X we had to import and use both customs server controls and user control on a page by adding the @Register directives to the top of the page. Once registered developers could then declare these controls anywhere on the page using the tag prefix and tag names configured in the @Register directive.

This is fine but if we have too many user controls across the sites (and that too ascx files) then it can be painful to manage across the site.

The control declaration is much cleaner and easier to manage in Asp.Net 2.0. Instead of duplicating them on all your pages, just declare them once within the new pages->controls section with the web.config file of your application

The controls need to be added in the controls tag inside the pages tag which will be inside the system.web tag.







An important this to note here is to use the ~ path as the user control can be used anywhere in the site. The “~” will resolve the control from the root of the web site.

Once the control is registered in the web.config file the control can be used by any page or user control in the site.

Another Important thing to note is that there is no performance difference in either registering the controls in the web.config or on the top of the page as they get compiled down to the same instruction in both the scenarios.

Wednesday, January 30, 2008

Send a huge range of email (bulk)

if (e.CommandName == "Send")
{
DataSet _dsSubscribersList = new DataSet();
_dsSubscribersList = _objNL.GetSubscribersList();
int count = 0;
if (_dsSubscribersList.Tables.Count > 0 && _dsSubscribersList.Tables[0].Rows.Count > 0)
{
DataSet _dsNewsletter = new DataSet();
_dsNewsletter = _objNL.GetNewsLetterDetailsByNewsletterID(
Convert.ToInt32(e.CommandArgument));
if (_dsNewsletter.Tables.Count > 0 && _dsNewsletter.Tables[0].Rows.Count > 0)
{
try
{
string From = ConfigurationManager.AppSettings["NewslettersSender"].ToString();
string To = null;
string body = HttpUtility.HtmlDecode(_dsNewsletter.Tables[0].Rows[0]["NewsBody"].ToString());
for (int i = 0; i < _dsSubscribersList.Tables[0].Rows.Count; i++)
{
To = (
string)_dsSubscribersList.Tables[0].Rows[i]["Email"];
body = body.Replace("[Subscriber Name]", To);
body = body.Replace(
"[userid]", _dsSubscribersList.Tables[0].Rows[0]["ID"].ToString());
try
{
//System.Threading.Thread.Sleep(4000);
SendMail(To, From, (string)_dsNewsletter.Tables[0].Rows[0]["Subject"], body);
count++;
}
catch
{
count--;
}
}
}
catch //(Exception ex)
{
lblMsg1.Text =
"Sending newsletting failure. Please try again.";//ex.Message +"sri 1"; //
lblMsg1.ForeColor = Color.Red;
return;
}
int x = 0;
x = _objNL.Newsletters_SetHasSent(
Convert.ToInt32(e.CommandArgument));
if (x == 1)
{
BindNewsLetters();
lblMsg1.Text =
"Newsletter send successfully to " + count + " subscribers. ";
dvRepeaters.Style.Value =
"display:block";
dvProgress.Style.Value =
"display:none";
}
}
else
{
lblMsg1.Text = "No newsletters to send!!";
}
}
else
{
lblMsg1.Text =
"No subscriber to send newsletter!!";
}
}

-----------------------------------------------------------------------------------------
//send email

private void SendMail(string To, string From, string Subject, string body)
{
MailMessage _mm = new MailMessage(From, To, Subject, body);
_mm.IsBodyHtml =
true;
SmtpClient _sc = new SmtpClient();
_sc.Send(_mm);
}

Wednesday, January 23, 2008

calculating users online

Membership.GetNumberOfUsersOnline()

Page Log Image & JavaScript Page Processing

We have provided below sample source code for JavaScript Page Processing in Asp.Net. You can copy and paste it in your pages to create the sample application.
Code for Processing Page (PageProcessor.aspx)



scrip
function PageOnLoad()
{
location.href = "<%=PageToLoad%>";
document.images['imgsrc'].src="Images/Loading.gif";
}
script


body bottommargin="0" leftmargin="0" rightmargin="0" topmargin="0" onload="PageOnLoad();"
form id="form1" runat="server"
div
table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%"
tr
td height="50" class="NormalText" align=center valign="bottom"
h3We are processing your request. Please wait.. h3
td
tr
tr
td align="center" height="250" valign="top
img src="" name="imgsrc" td
tr
tab le
di v




Source Code of code-behind file (PageProcessor.aspx.cs)

protected string PageToLoad;

protected void Page_Load(object sender, EventArgs e)
{
PageToLoad = Request.QueryString["PageId"];
}

First paragraph of data from sql server

SELECT ID, Body, CHARINDEX(CHAR(13), Body) AS FirstParagraphLen FROM MyTable


or

SELECT ID,SUBSTRING(Body, 1, CHARINDEX(CHAR(13), Body)) AS 'Text'
FROM TableName

Tuesday, January 22, 2008

How to access Cookies from Other web pages

 Public Sub DisplayCookies()

'Create a WebRequest to the specified URI.
Dim req As HttpWebRequest = CType(WebRequest.Create("http://werweb.com/whoison.php"), HttpWebRequest)

'Get an empty cookie container.
req.CookieContainer = New CookieContainer

'Send the request and return the response.
Dim resp As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse)

'Display the cookies.
Dim alertStr As String
alertStr = "Number of Cookies: " & resp.Cookies.Count.ToString() & Environment.NewLine()
alertStr = alertStr & "------------------------------" & Environment.NewLine()

Dim i As Integer
For i = 0 To resp.Cookies.Count - 1
alertStr = alertStr & resp.Cookies(i).Name & "=" & resp.Cookies(i).Value & Environment.NewLine()
Next
MessageBox.Show(alertStr)

'Close the Response.
resp.Close()
End Sub

Create Thumbnail of an image

System.Drawing.Image image = System.Drawing.Image.FromFile("c:\\mypic.jpg");
System.Drawing.Image.GetThumbnailImageAbort tnabort = new System.Drawing.Image.GetThumbnailImageAbort(tnCallback);
System.Drawing.Image tnimage = image.GetThumbnailImage(32, 32, tnabort, IntPtr.Zero);
tnimage.Save("c:\\tnmypic.jpg");

public bool tnCallback()
{
return true;
}