SharePoint Web Part Development

by Seth Bates



Example 1:



Protected Overrides Sub RenderWebPart(ByVal output As 

   System.Web.UI.HtmlTextWriter)

      output.Write("This is my first web part.")

End Sub



Example 2: 



Protected WithEvents btnSearch As Button

Protected Overrides Sub CreateChildControls()

    btnSearch = New Button

    btnSearch.Text = "Search"

    Controls.Add(btnSearch)

End Sub



Example 3: 



Private Sub btnSearch_Click(ByVal sender As System.Object, 

                   ByVal e As System.EventArgs) Handles btnSearch.Click

    Dim strConnectionString As String = 

                 "Data Source=(local);Initial Catalog=pubs;

                                          User ID=pubs;Password=search"

    Dim strQuery As String = "SELECT * FROM titles"

    If txtSearch.Text.Length > 0 Then

        strQuery &= " WHERE title LIKE '%" & txtSearch.Text & "%'"

    End If



    Dim objAdapter As New SqlClient.SqlDataAdapter

    objAdapter.SelectCommand = New SqlClient.SqlCommand(strQuery, 

                     New SqlClient.SqlConnection(strConnectionString))

    Dim objDataSet As New DataSet

    objAdapter.Fill(objDataSet)



    grdResults.DataSource = objDataSet.Tables(0)

    grdResults.DataBind()

End Sub





Example 4: 



Private objControl As Control

Protected Overrides Sub RenderWebPart(ByVal output 

                                     As System.Web.UI.HtmlTextWriter)

    objControl.RenderControl(output)

End Sub

Protected Overrides Sub CreateChildControls()

    objControl = Page.LoadControl("/UserControls/PubsDisplay.ascx")

    Controls.Add(objControl)

End Sub









2



