Hi all - complete newbie here :-)
I'm just getting familiar with codebehind and all that it entails. I'm also a newbie to webstandards-design, so i'm trying to keep all of my code away from traditional html tags like <table> etc. and instead use <div>'s and <span>'s for pretty much everything. Problem i've found is that most of the ASP.Net web controls (e.g. DataGrid) seem to create traditional tags (<table> in the case of a DataGrid). On a website I did previously, I found that I could manually loop through the results of a DataReader just like you have to in classic ASP. Problem here, though, is that my DataReader is in my code-behind, and so I can't 'pull' it into the code-front page to loop through it where I need to. I'm guessing I need some kind of place holder asp.net tag that I can use so that I can loop through my DataReader in the code behind, and then place the entire result into the said placeholder, so that it will appear where i want it to.
Am I heading in the right direction there, or are there certain other controls that I can use to post pure css-ready code, while still DataBinding in the code behind? I get the feeling that the Repeater control is near to what I need, but that still uses the traditional <table> tag.
Thanks for any help!
Screamer
There is nothing wrong with using the <table> element, is it not deprecated in any way and is an accepted standard for the w3c.
It appears you are using ASP.NET 1.x since you brought up the DataGrid (The DataGrid was superseded by the GridView in ASP.NET v2.0). If you want to use a customized repeating control, use the Repeater control. I don't beleive the Repeater puts in the table element and repeats exactly what you want it to.
You can still use the <% %> like traditional ASP and access objects from the code behind (Like a SqlDataReader) provided that it has the protected or public accessor.
Thanks Kevin - actually, I can use .Net 2 or 1.1 - this site is going to be run on my own server. Right now, i've selected v2 (I assume it's better). Can you explain (an example would be great) the use of the public/protected tag thingy (i'm a complete newbie to this stuff)...
well most things have accessors, there are serveral different ones, their names depend on what langauge.
What they do is limit what classes or members can access it. For instance, a public string in a class can be seen by all other classes. If it is private, it can only be seen by other things in the class. Here is a full explaination:
C#:http://msdn2.microsoft.com/en-us/library/ba0a1yw2.aspx
VB.NET:http://msdn2.microsoft.com/en-us/library/76453kax.aspx
Here is a simple example:
class TestClass1{private string PrivateString;}class TestClass2{TestClass1 testclass1 =new TestClass1();testclass1.PrivateString ="Test";// You cannot access PrivateString from another class because it is private}Or if you use VB.NET:
Class TestClass1Dim PrivateStringAs String;End ClassClass TestClass2Dim testclass1As TestClass1 =New TestClass1()testclass1.PrivateString ="Test"'You cannot access PrivateString from another class because it is privateEnd Class
Hi Kevin.
Thanks for that - I guess that with a query that I wanted to loop through in my code-front, i'd make a new class (I'm using VB.net, as it happens) that contains only the query, and datareader, etc. I'm still having trouble understanding how i'd 'manually' pull the class containing my executed query into the code front for me to loop through... Or how i'd pullany class in!
Thanks for your help so far!
anyone?
You could write a Business Object, but the simplest thing to do would be to put the DataReader in the same class as you will be using it in.
here is one of the new ways of doing that
http://www.asp.net/QuickStart/aspnet/doc/pages/code.aspx#code
you put your .cs or .vb file in the app_code folder - then your app can see the classes in it. (after making the web.config changes)
Hi uncleb - thanks for the message. Can you or anyone else explain how i'd use this method:http://www.asp.net/QuickStart/util/srcview.aspx?path=~/aspnet/samples/simple/CodeFolder.src to import a datareader from code behind into my main page so that I can use it for looping and the like?
A very big thanks so far :-)
you have 2 files - 1 id a class file, some_name_you_make_up.vb.
The other file is the .aspx where you want to use stuff in the some_name_you_make_up.vb.
some_name_you_make_up.vb....
Imports Microsoft.VisualBasic- or any other namespaces you might need -
like SqlData,SqlDataClient,etc...PublicClass CustomClass <-your class name that you make up
Public Shared Function BindProduct()As SqlDataReader
Dim myCommand As SqlCommand
Dim SqlStatementProduct As String = "SELECT dbo.Product_list.Product FROM dbo.Product_List ORDER BY Product"
Dim myDataConnection As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
myCommand = New SqlCommand(SqlStatementProduct, myDataConnection)
myDataConnection.Open()
Return myCommand.ExecuteReader(CommandBehavior.CloseConnection)
End FunctionEndClass
then to use it in the .aspx page ...
<script runat="server">Sub Button1_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)
Dim SomeNameToInstatntiateTheClassAsNew CustomClass
Dim MyReader as = SomeNameToInstatntiateTheClass.BindProduct()
While MyReader.read
'loop here
End While
EndSub
</script>
since the class (*.vb) files are in the App_Code folder , you don't have to import it
- Framework looks there automatically
Thanks so much uncleb - that looks exactly like what I'm after. A few days ago, I cottoned on to fact that i'd be somehow needing to import a class in my code behind into my main page - and I did manage it with a string variable. That code shows me how to do it with a reader, which was the missing link in my understanding. So thanks again! Unfortunately, due to time contraints, i'd switched to inline coding just to get things working (don't even have <script runat="server"> tags at the moment, as i'm having trouble using variables from inside of those tags in other places around the page) - looks like i'll have to look at converting back.
Thanks again!
0 comments:
Post a Comment