Oct 17 2007

Concat String with itself in .NET

This is prolly a really n00b thing of me, but I couldn’t figure out a better way to concat a string with itself, so I reverted to doing this:

Dim strMyString As String = “”

strMyString = “Hello”
strMyString = strMyString & ” world”

myLabel.Text = strMyString

Well, here’s the easier way:

Dim strMyString As New StringBuilder

strMyString.Append(“Hello”)
strMyString.Append(” world”)

myLabel.Text = strMyString.ToString


Oct 17 2007

Paging in .NET without a DataGrid

Again with the paging…

Well, I need it. I’ve spent waaaay to long trying to redesign the mod listing pages and this is what I’ve come up with that seems to work:

ASP Design Code:
<div id=”Content”>
<asp:DataList id=”theDataList” RepeatLayout=”Flow” runat=”server”>
<ItemTemplate>
<div class=”Item”>
<%#BuildItem(DataBinder.Eval(Container.DataItem, “fld1″), DataBinder.Eval(Container.DataItem, “fld2″), Container.ItemIndex)%>
</div>
<%#LineWrap(Container.ItemIndex)%>
</ItemTemplate>
</asp:DataList>
</div>
&gtscript language=”javascript” type=”text/javascript”>stripOutBR(‘Content’);</script>
VB:
Dim strConn As String = ConfigurationManager.ConnectionStrings(“Database”).ConnectionString
Dim objConn As New System.Data.SqlClient.SqlConnection(strConn)
Dim objCommand As New System.Data.SqlClient.SqlCommand
Dim dsTemp As System.Data.DataSet = New System.Data.DataSet
Dim strSQL As String = “”

objCommand.Connection = objConn

strSQL = “SELECT * FROM table”

Dim objAdapter As New System.Data.SqlClient.SqlDataAdapter(strSQL, objConn)

objAdapter.Fill(dsTemp, “table”)

objConn.Close()
objAdapter.Dispose()
objConn.Dispose()

Dim objDps As PagedDataSource = New PagedDataSource
objDps.DataSource = dsTemp.Tables(0).DefaultView
objDps.AllowPaging = True
objDps.PageSize = 15 ‘ Amount of records to show on each page

theDataList.DataSource = objDps
theDataList.DataBind()

Public Function BuildItem(ByVal fld1 As String, ByVal fld2 As String, ByVal intCount As Integer) As String

Format the data to be displayed the way I want

End Function

‘ This function is used to throw in a div with style=”clear:both”
‘ to make a new line every 5th object
Public Function LineWrap(ByVal intLineNumber As Integer) As String
Dim strSpacer As String = “”
If intLineNumber = 4 Or intLineNumber = 9 Or intLineNumber = 14 Then
strSpacer = “<div class=”"spacer”"> </div>”
End If
Return strSpacer
End Function

Javascript:
/* this script is to clear out the tags the DataList throws in.
IE 6 doesn’t like them. Just make sure you call it AFTER
the datalist is written out. */

function stripOutBR(theID){document.getElementById(theID).innerHTML = document.getElementById(theID).innerHTML.replace(/<\/span><br><span>/gi,”);}

Then using the various methods of the objDps object (the PagedDataSource) you can set the current page, list out how many items there are, how many pages, etc. Just mess around with it. I’m not saying this is perfect or correct, as I’m still learning. But this is how I do it and it seems to work.


Oct 17 2007

Getting Rid of Tables in a DataList (.NET)

I’ve been working on Paging in .net and damn… I never thought it’d be this difficult. I mean, in Classic ASP, paging is pretty simple with ADO. But This whole DataGrid, DataList thing is sending me for a loop.

Anywho, I finally got a DataList to spit out the data that I needed, but it used <table> structure to do so. This is probably fine for most people, but I’m trying to design BodyMod.org to be a CSS driven site (Div’s not Tables). So… this was bad. If you’re like me, here’s what you do. Just add the tag RepeatLayout=”Flow” to your DataList object like so:

<asp:DataList id=”theDataList” RepeatLayout=”Flow” runat=”server”>

And whammo… no more table, tr, td outputs =)