Powered By Blogger

Search

Thursday, March 20, 2008

Create custom web control (FilterDropDown)



Create custom web control (FilterDropDown)

In this article we will have a look at implementing a custom web control. This web control will inherit from the Textbox web control and List Box Control, It working as like Dropdown List Box. But Filter feature also added and you Type your Own Text also.

Suppose you have city Dropdown box. It has 1000 Cities. User City is Not there in you Dropdown, then you provide “Other” Option also, and you want add one more Text Box

But Here if City is there in you Drop down you can select that and user need New city they can enter it.





  • Copy Following Code.


  • Paste it on FilterDropDown .vb file


  • Build and Get Dll .


  • Add in your application as a control


Imports System.Web.UI.WebControls
Imports System.Web.UI
Imports System.Web
Imports System.Data
Imports System
Imports System.Xml

Public Class FilterDropDown
Inherits System.Web.UI.WebControls.WebControl
Implements INamingContainer

Private Shared DropDown As New TextBox()
Private Shared DropList As New ListBox()

Public Sub New()
MyBase.New()
Me.Width = (Unit).Pixel(200)
Me.BorderWidth = (Unit).Pixel(1)
End Sub

Private Shared Ds As New DataTable()
Private Shared Dt As String
Private Shared m_width As Integer = 200
Public Property dataSource() As DataTable
Get
Return Ds
End Get
Set(ByVal value As DataTable)
Ds = value
End Set
End Property

Public Property dataTextField() As String
Get
Return Dt
End Get
Set(ByVal value As String)
Dt = value
End Set
End Property
Public Property FWidth() As Integer
Get
Return m_width
End Get
Set(ByVal value As Integer)
m_width = value
End Set
End Property


Protected Overloads Overrides Sub CreateChildCOntrols()
DropList.ID = Me.ID & "txt"
DropDown.ID = Me.ID & "List"

' Data Source Assign
DropList.DataSource = Ds
DropList.DataTextField = Dt
DropList.DataBind()

'Properties
DropList.Style.Add("position", "absolute")
DropList.Style.Add("display", "none")
DropList.Width = (Unit).Pixel(m_width)
DropList.Height = (Unit).Pixel(200)
DropDown.Width = (Unit).Pixel(m_width)

Dim CTxtID As String = Me.ID & "_" & DropDown.ID
Dim CListId As String = Me.ID & "_" & DropList.ID

'Script
Dim LoadScript As String = " <script language='javascript' type='text/javascript'> var myList" & CListId & " = new Array(); for(var i=0; i<document.getElementById('" & CListId & "').options.length;i++) { myList" & CListId & "[i]=document.getElementById('" & CListId & "').options[i].innerText; }</script> "
Me.Page.ClientScript.RegisterStartupScript([GetType](), "Load", LoadScript)

Dim FocusScript As String = "<script language='javascript' type='text/javascript'> function " & CListId & "show(oObject,ListID){var len = oObject.value;oObject.value='';var oTextRange = document.selection.createRange();if (oTextRange != null){var left = (oTextRange.boundingLeft-5);var Top = oTextRange.boundingTop + 17;}document.getElementById(ListID).style.top=Top;document.getElementById(ListID).style.left=left;oObject.value=len ;}</script> "
Me.Page.ClientScript.RegisterStartupScript([GetType](), "Focus", FocusScript)
DropDown.Attributes.Add("onfocus", "document.getElementById('" & CListId & "').style.display='block';" & CListId & "show(this,'" & CListId & "');")
DropDown.Attributes.Add("onblur", "document.getElementById('" & CListId & "').style.display='none'")
DropDown.Attributes.Add("onkeyup", CListId & "MyList(this,'" & CListId & "'); ")

Me.Page.ClientScript.RegisterStartupScript([GetType](), "MyList", "<script language='javascript' type='text/javascript'>function " & CListId & "MyList(txtid,listid) { var txt = txtid.value;var tot=document.getElementById(listid).options.length;for(i=0; i<tot;i++) { document.getElementById(listid).remove(0);}var j =0;for(i=0; i<myList" & CListId & ".length;i++) {if( (myList" & CListId & "[i].toUpperCase()).indexOf(txt.toUpperCase())==0) { var elOptNew = document.createElement('option'); elOptNew.text = myList" & CListId & "[i]; document.getElementById(listid).add(elOptNew,j);j++; } } }</script>")

DropList.Attributes.Add("onChange", "document.getElementById('" & CTxtID & "').value=this.options[this.selectedIndex].innerText")

Controls.Add(DropDown)
Controls.Add(DropList)

End Sub



Public Property result() As String
Get
Try
Return DropDown.Text
Catch ex As Exception
Return ""
End Try
End Get
Set(ByVal value As String)
DropDown.Text = value
End Set
End Property

End Class

Output:







After Type Any text

Tuesday, March 18, 2008

SQL a Stored Procedure to be Automatically Run

SQL a Stored Procedure to be automatically run

How to setup a stored procedure to be automatically run?

I needed to know how to schedule a stored procedure to run over and over! This is a brief step by step of how to get going. This isn't meant to hold your hand but get you going!
Here is a brief how-to steps!

1. Open SQL Enterprise Manager
2. Expand the database folder, Select your database if there are several databases
3. Left Single click on the place where it says stored procedures
4. Right click after selecting and choose new stored procedure
5. Create and save your stored procedure (Even has a syntax checker!)
6. Inside Enterprise Manager--Click on Tools, Job Scheduling (A wizard will appear)
7. Click Next
8 Leave Transact SQL-command selected, Click Next
9. Verify the database and in the big text box type -- EXEC storedprocedurename
10. Click Next
11. This will bring up a form that allow you to do whatever. Run now is selected. To make it a reoccurring stored proc the 5th option is that.
12. Schedule how you want the stored proc to run
13. Choose if you want to email or net send command. If you don't want anything click next
14. To modify or delete the job open SQL Enterprise Manager
15. Expand the Management Folder, Expand SQL Server Agent, select Jobs.
16. This is where all the info on the jobs that you created in Job Scheduler.
17. Right Click on the job and choose properties. That's it!

Disable Browser Right click event


Javascript : Disable Browser Right click event

Disable Browser Right click event.


< body oncontextmenu="return false;" >
</body >

Friday, March 14, 2008

Upload a file from my ASP.NET page

Upload a file from my ASP.NET page?

To upload a file with your ASP.NET page, you need the use of two classes: System.Web.UI.HtmlControls.HtmlInputFile class and the System.Web.HttpPostedFile.
The HtmlInputFile class represents an HTML input control that the user will use on the client to select a file to upload.

The HttpPostedFile class represents the uploaded file. This is obtained from the .PostedFile property of the HtmlInputFile control.
In order to use the HtmlInputFile control, you need to add the enctype attribute to your form tag as follows:

<form id="upload" method="post" runat="server" enctype="multipart/form-data">

Below is a simple example of how to upload a file via an ASP.NET page using C#.

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<html>
<head>
<title>upload_cs</title>
</head>
<script language="C#" runat="server">
public void UploadFile(object sender, EventArgs e)
{
if (loFile.PostedFile != null)
{
try
{
String strFileName, strFileNamePath, strFileFolder;
strFileFolder = Context.Server.MapPath(@"data\");
strFileName = loFile.PostedFile.FileName;
strFileName = Path.GetFileName(strFileName);
strFileNamePath = strFileFolder + strFileName;
loFile.PostedFile.SaveAs(strFileNamePath);
lblFileName.Text = strFileName;
lblFileLength.Text = loFile.PostedFile.ContentLength.ToString();
lblFileType.Text = loFile.PostedFile.ContentType;
pnStatus.Visible = true;
}
catch (Exception x)
{
Label lblError = new Label();
lblError.ForeColor = Color.Red;
lblError.Text = "Exception occurred: " + x.Message;lblError.Visible = true;this.Controls.Add(lblError);
}
}

}

</script>

<body>

<form id="upload_cs" method="post" runat="server" enctype="multipart/form-data">

<P>

<INPUT type="file" id="loFile" runat="server">

</P>

<P>

<asp:Button id="btnUpload" runat="server" Text=" Upload " OnClick="UploadFile"></asp:Button>

</P>

<P>

<asp:Panel id="pnStatus" runat="server" Visible="False"><asp:Label id="lblFileName" Font-Bold="True" Runat="server"></asp:Label>

uploaded

<BR>

<asp:Label id="lblFileLength" Runat="server"></asp:Label> bytes

<BR>

<asp:Label id="lblFileType" Runat="server"></asp:Label>

</asp:Panel>

</P>

</form>

</body >

</html>

Javascript: Set Browser Home Page

Javascript: Set Browser Home Page

This JavaScript code will enable you to place a button on your site that, when clicked on, will make your web site your visitors home page


<FORM><INPUT TYPE="button" VALUE="Make This Site Your Home Page" onClick="this.style.behavior='url(#default#homepage)'; this.setHomePage('http://www.sathiskvp.blogspot.com');"></FORM>


Example: