Thursday, May 27, 2010
#
Today's little problem is that I am trying to load user controls from my codebehind like so:
Dim myControl As UserControl = LoadControl("~\Modules\Content.ascx")
Controls.Add(myControl)
On running the page myControl is no where to be seen. I wonder why that is? Well after a bit of thought the following come to mind...
- Am I using the correct code to insert the usercontrol?
- Is there an alternative available?
- Does the fact that the usercontrol has a page_load event make a difference?
- Does the fact that the usercontrol is being called from page_init make a difference?
- Do I need to register the control in my aspx page at design time?
I'll be looking to answer these questions as the day goes on!
Here are my answers so far:
- Yes the code is working. I found this out by adding a static <p> tag to the ascx and found it is in fact being displayed on the page
- The fact that the user control has a page-load is making a difference. I have added Response.write("Test from within page_load") and this is being rendered ... twice
-
-
- No I do not have to register the control at design time when loading from codebehind
Thursday, April 22, 2010
#
I was recently introduced to Microsoft's tool that analyzes managed code assemblies called FxCop. It points out possible design, localization, performance and security improvements against a pre-defined set of rules (and also accepts custom rules). At first I was unsure how to go about using it, as it seems to be aimed at Windows developers (.exe and .dll). It’s easy to check an asp.net web site
1)Create a new folder (i.e C:\Code Analysis)
2)Publish your web application into the new folder
3)Open FxCop and add all the dll files from the newly created bin folder to be scrutinized.
Lots more info / docs available here on msdn and you can also download fxcop free
I know you will all point out how this is old news... but why didn't anyone tell me? It is not mentioned in any of the study materials I've been using for the last 18 months!
Here is the markup for the repeater control and its templates:
<asp:Repeater ID="Repeater" runat="server">
<HeaderTemplate>
<table>
<tr>
<td colspan="3"><h2>Header information:</h2></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Container.DataItem%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Here is the code to populate it with data:
SQLString = "select something from foo where something"
SQLCommand = New SqlCommand(SQLString, SQLConnection)
SQLConnection.Open()
SQLDReader = SQLCommand.ExecuteReader
If SQLDReader.HasRows Then
Contactinforepeater.DataSource = SQLDReader
Contactinforepeater.DataBind()
End If
End If
SQLConnection.Close()
SQLDReader.Close()
Saturday, February 27, 2010
#
This is the code I use to send an email from an asp.net page:
Dim message As New MailMessage()
message.IsBodyHtml = False
message.From = New MailAddress("foo@foo.com")
message.To.Add(New MailAddress("foo@foo.com"))
message.Subject = ("Response from form")
message.Body = ("Email message here")
Dim client As New SmtpClient()
client.Host = "email.foo.foo"
client.Send(message)
I am starting work on some asp.net vb pages to consume and work with web services.
Part 1 was to create a web form that posts xml that a dba can paste into a textbox on an aspx page.
Here is the code;
Imports System.Net
Imports System.IO
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit.Click
Dim Data As String
Dim WebRequest As WebRequest
Dim RequestStream As Stream
Dim StreamWriter As StreamWriter
Dim WebResponse As WebResponse
Dim ResponseStream As Stream
Dim StreamReader As StreamReader
' Create a new WebRequest which targets the web service method
WebRequest = WebRequest.Create("http://foo")
' Data to send
Data = DataToSend.Text
' Set the method and content type
With WebRequest
.Method = "POST"
.ContentType = "text/xml"
.Timeout = -1
.ContentLength = Data.Length()
End With
' write our data to the Stream using the StreamWriter.
RequestStream = WebRequest.GetRequestStream()
StreamWriter = New StreamWriter(RequestStream)
StreamWriter.Write(Data)
StreamWriter.Flush()
StreamWriter.Close()
RequestStream.Close()
' Get the response from the remote server.
WebResponse = WebRequest.GetResponse()
' Get the server's response status
ResponseStream = WebResponse.GetResponseStream()
StreamReader = New StreamReader(ResponseStream)
ResponseLabel.Text = StreamReader.ReadToEnd()
StreamReader.Close()
ResponseStream.Close()
' Close the WebResponse
WebResponse.Close()
End Sub
End Class
Here is my web.config used to login protect a folder called administration;
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<customErrors mode="Off">
</customErrors>
<authentication mode="Forms">
<forms name=".ASPXAUTH" loginUrl="login.aspx" protection="All" timeout="999999">
<credentials passwordFormat="MD5">
<user name="admin" password="21232F297A57A5A743894A0E4A801FC3" />
</credentials>
</forms>
</authentication>
<authorization>
<allow users="?" />
<allow users="*" />
</authorization>
<trace enabled="true" localOnly="false" />
</system.web>
<location path="administration">
<system.web>
<authorization>
<allow users="admin" />
<deny users="*" />
</authorization>
</system.web>
</location>
</configuration>
I'm using it in this asp.net vb web site which I built from some tutorials and added a few extra bits of functionality to.
Wednesday, February 10, 2010
#
This handy bit of sql code returns the last identity value inserted into an identity column in the current scope and sets its value to the parameter @ID
SET @ID = SCOPE_IDENTITY()
I found this a very useful thing to get to grips with and start using in my code. For further information on scope identity hit this msdn link
Exposing the property:
Public Property stringproperty() As String
Get
Return _stringproperty
End Get
Set(ByVal value As String)
_stringproperty = String.empty
End Set
End Property
Accessing the property from a content page:
You will need to add this line to your .aspx page in order to be able to access the property you just exposed
@ MasterType VirtualPath ="~/yourmasterpagename.master" %>
You will then be able to access the property programatically from your content page like this;
Master.stringproperty = foo
Friday, January 29, 2010
#
make sure all queries are parameterised like this:
sql = ("select * from contacts where contactid = @id")
Dim cmd As SqlCommand = New SqlCommand(sql, conn)
cmd.Parameters.Add("@id", SqlDbType.VarChar)
cmd.Parameters("@id").Value = id
I also include a usercontrol which checks the querystring for bad terms:
The list is long but this snippet should give you the gist of it:
Dim querystringvar As String = Request.QueryString.ToString
If InStr(querystringvar, "drop") Then
Response.Redirect("/errors/504.aspx)
ElseIf InStr(querystringvar, "select") Then
Response.Redirect("/errors/504.aspx)
Wednesday, January 27, 2010
#
Here is the code you need to start adding parameters to your sql commands:
Add parameter then add value:
Dim cmd As SqlCommand = New SqlCommand(s,c)
cmd.Parameters.Add("@id", SqlDbType.Int)
cmd.Parameters("@id").Value = foo
or
Add parameter with value:
command.Parameters.AddWithValue("@id", foo)