ASP.Net Update Panel – Javascript Event – jQuery Click

I needed to add a onclick event to a textbox in asp.net to change it to remove text from a textbox onClick when it is clicked.
The normal code would look something like this:



But, this won’t quite work because my textbox is in a UpdatePanel’s ContentTemplate and the javascript is not read on postback.

So, This is what I ended up doing (a simplified version):


	
		
	


This will add an endRequest handler to the end of the partial page postback so that you can call a nice little function at the end of it.

Hope this helps some people!

Asp.Net – Adding Parameters to CommandType.Text

This is how you add parameters to a standard CommandType.Text SQL call.

This should prevent sql injection attacks.

        Using conSQL = New SqlConnection("CONNECTION STRING TO DATABASE")
            Try
                str = "SELECT * FROM TableName where customerid = @CustomerID and CartType = @CartType"
                cmdSQL = New SqlCommand(str, conSQL)
                cmdSQL.CommandType = CommandType.Text
                cmdSQL.Parameters.AddWithValue("@CustomerID", CustomerID)
                cmdSQL.Parameters.AddWithValue("@CartType", cartType)
                cartcount = cmdSQL.ExecuteScalar()
            Catch ex As Exception
                cartcount = 0
            End Try
        End Using

Asp DropDownList – States – ListItem Example

Asp DropDownList - States - ListItem Example
Compiled List of US State Drop Downs for Asp.Net Forms:

Hope this helps!


    Select a State
    Alabama
    Alaska
    Arizona
    Arkansas
    California
    Colorado
    Connecticut
    D.C.
    Delaware
    Florida
    Georgia
    Hawaii
    Idaho
    Illinois
    Indiana
    Iowa
    Kansas
    Kentucky
    Louisiana
    Maine
    Maryland
    Massachusetts
    Michigan
    Minnesota
    Mississippi
    Missouri
    Montana
    Nebraska
    Nevada
    New Hampshire
    New Jersey
    New Mexico
    New York
    North Carolina
    North Dakota
    Ohio
    Oklahoma
    Oregon
    Pennsylvania
    Rhode Island
    South Carolina
    South Dakota
    Tennessee
    Texas
    Utah
    Vermont
    Virginia
    Washington
    West Virginia
    Wisconsin
    Wyoming

More State Drop Downs After the fold:
Continue reading “Asp DropDownList – States – ListItem Example”

Asp.Net VB – Listview – ItemCommand – DataKeys – CommandName – SQL

Asp.Net VB - Listview - ItemCommand - DataKeys - CommandName - SQL

Here is a basic Listview that has an ItemCommand and ItemDataBound:

Frontend Code:


        
            
        
        
            
<%#DataBinder.Eval(Container.DataItem, "FirstName")%> <%#DataBinder.Eval(Container.DataItem, "LastName")%>
<%#DataBinder.Eval(Container.DataItem, "Address1")%>
<%#DataBinder.Eval(Container.DataItem, "City")%>, <%#DataBinder.Eval(Container.DataItem, "State")%>, <%#DataBinder.Eval(Container.DataItem, "Zip")%>
<%#DataBinder.Eval(Container.DataItem, "Phone")%>
Click to make Default Billing Address
Click to make Default Shipping Address

Backend Code:

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

        FindIt()

    End Sub
    Private Sub FindIt()
        
        lblAddresses.Text = ""

        Dim sqlDataConnection As System.Data.SqlClient.SqlConnection = CDataAccess.OpenDatabase
        Dim conStr As String = sqlDataConnection.ConnectionString
        sqlDataConnection.Close()

        Dim selectCommand As String = "SELECT Address.*, Customer.BillingAddressID, Customer.ShippingAddressID"
        selectCommand &= " FROM Address INNER JOIN Customer ON Address.CustomerID = Customer.CustomerID"
        selectCommand &= " WHERE Address.CustomerID = @CustomerID AND Address.Deleted = 0"

        Dim sds As New SqlDataSource()
        sds.ConnectionString = conStr
        sds.SelectParameters.Clear()

        sds.SelectParameters.Add("CustomerID", 12345)
        sds.SelectCommand = selectCommand

        lvAddresses.DataSource = sds
        lvAddresses.DataBind()
    End Sub

    Protected Sub lvAddresses_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles lvAddresses.ItemCommand
        Dim dataItem As ListViewDataItem = e.Item
        Dim currentDataKey As DataKey = lvAddresses.DataKeys(dataItem.DataItemIndex)
        Dim AddressID As Integer = currentDataKey("AddressID")
        Dim BillingAddressID As Integer = currentDataKey("BillingAddressID")
        Dim ShippingAddressID As Integer = currentDataKey("ShippingAddressID")

        'MakeDefault_Billing
        'MakeDefault_Shipping
        'EDITAddress
        'REMOVEAddress
        If String.Equals(e.CommandName, "MakeDefault_Billing") Then
            MakeDefault(AddressID, True)
        ElseIf String.Equals(e.CommandName, "MakeDefault_Shipping") Then
            MakeDefault(AddressID, False)
        ElseIf String.Equals(e.CommandName, "EDITAddress") Then
            EditAddress(AddressID)
        ElseIf String.Equals(e.CommandName, "REMOVEAddress") Then
            RemoveAddress(AddressID)
        End If
    End Sub

    Protected Sub lvAddresses_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lvAddresses.ItemDataBound
        Dim dataItem As ListViewDataItem = e.Item

        If e.Item.ItemType = ListViewItemType.DataItem Then
            Dim currentDataKey As DataKey = lvAddresses.DataKeys(dataItem.DataItemIndex)
            Dim AddressID As Integer = currentDataKey("AddressID")
            Dim BillingAddressID As Integer = currentDataKey("BillingAddressID")
            Dim ShippingAddressID As Integer = currentDataKey("ShippingAddressID")
            lit = e.Item.FindControl("litMakeDefault_Billing")
            Dim lit2 As Literal = e.Item.FindControl("litMakeDefault_Shipping")
            lnk = e.Item.FindControl("lnkMakeDefault_Billing")
            Dim lnk2 As LinkButton = e.Item.FindControl("lnkMakeDefault_Shipping")

            lit.Visible = Not lnk.Visible
        End If

    End Sub

.Net – Sending Email with In Memory Attachment

This VB.Net code takes the FileUpload control’s PostedFile and without saving it to the filesystem attaches it to an MailMessage and sends an email.

Advantages:

  • No threading, read/write permissions needed.
  • One line of code.

Front End:

<asp:FileUpload ID="fileUpload" runat="server" />

Code:

Dim m As New MailMessage()
m.Attachments.Add(New Attachment(fileUpload.PostedFile.InputStream, fileUpload.FileName))