Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you move inline code to a code-behind page without VS 2

Status
Not open for further replies.

ironhide1975

Programmer
Feb 25, 2003
451
US
I have a very simple ASP.Net page I created, and I just want to move the inline code to a code-behind page. I was told you can't do this without Visual Studio or something to create a Project with DLL's. Can someone let me know if it's possible to move this to a code-behind page and if so, how do you change the page to reference the code behind if you have no visual studio project setup?

Code:
<%@ Page Language="vb" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<script language="vb" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
	
    Dim Search As String
    Dim Conn2 As SqlConnection
    Dim sSQL As SqlCommand
		
    Search = "WhoDidWhatID"
    
		Try
      Conn2 = New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
      sSQL = New SqlCommand("Exec GENERAL_WhoDidWhat_FindSortCHR '" & Search & "',''", Conn2)
      Conn2.Open()
			
      Dim DS As SqlDataReader = sSQL.ExecuteReader()
			
			MyDataGrid.DataSource = DS
			MyDataGrid.DataBind()
		Finally
      Conn2.Close()
		End Try
  End Sub
  
  </script>

 
Visual Studio 2005 will create a line like this:
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default3.aspx.vb" Inherits="Default3" %>
[/quote]
so that it knows where the actual code file is. I'm not sure if the command line compiler will recognise this syntax or not, but presumably you could just use the free express editions to create your projects with code behind files anyway?

[COLOR=#800080]
____________________________________________________________

Need help finding an answer?

Try the [url=http://www.tek-tips.com/search.cfm]Search Facility[/url] or read FAQ222-2244 on how to get better results.
[/color]
 
Do I have to use the free express edition? How would you do this with notepad (I know I know, but I really need to know how this works without visual studio.)

Here is the code I put at the top of the page.

Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="whodidwhatListing.aspx.vb" Inherits="whodidwhatListing.asp" %>

I get a 'context is not a member of' message.

 
To be honest I'm not sure (i'm probably not best placed to answer this question as I always use VS) although a quick search came up with articles such as:


Hopefully someone else with experience of this will be able to help.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
which version of ASP.NET are u using?
if its .NET 1.x then CodeFile wont work. in 1.1 this is how i do it:

<%@ Page Language="VB" AutoEventWireup="false" Src="whodidwhatListing.aspx.vb" Inherits="whodidwhatListing.asp" %>

that wont require a compile, if u want to take the DLL approach then:


<%@ Page Language="VB" AutoEventWireup="false" Inherits="whodidwhatListing.asp" %>

compile the VB file with the vbc command, the generated DLL file has to be moved to the bin folder...


Known is handfull, Unknown is worldfull
 
Dude, you're going to waste a ton of time trying to write that in notepad (better yet get Notepad2.exe ;-). There might be a lot of "windows generated code" that you will need to reproduce, depending on your code.
 
>>Dude, you're going to waste a ton of time trying to write that in notepad (better yet get Notepad2.exe . There might be a lot of "windows generated code" that you will need to reproduce, depending on your code.

i agree from the completion of project, perfection, standards etc etc point of view.

but from my experience i would say that going the notepad way will give u some more insights into how .NET works (lots of things are inbuilt in VS). its pretty good for a beginner...

Known is handfull, Unknown is worldfull
 
Visual Studio is great but there is going to be times I won't have it. Let alone the fact that I'm upgrading an entire ASP Classic intranet to .net bit by bit. One of the first items I'm practicing on is a simple listing page. Here is the complete code.

Code:
<%@ Page Language="vb" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<script runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
	
    Dim Search As String
    Dim Conn2 As SqlConnection
    Dim sSQL As SqlCommand
		
    Search = "WhoDidWhatID"
    
		Try
      Conn2 = New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
      sSQL = New SqlCommand("Exec GENERAL_WhoDidWhat_FindSortCHR '" & Search & "','", Conn2)
      Conn2.Open()
			
      Dim DS As SqlDataReader = sSQL.ExecuteReader()
			
			MyDataGrid.DataSource = DS
			MyDataGrid.DataBind()
		Finally
      Conn2.Close()
		End Try
  End Sub
  
  Public Function TruncateMe(ByVal InputString As String, ByVal TruncateTo As Integer) As String
    If InputString.Length >= TruncateTo Then
      InputString = InputString.Substring(0, TruncateTo)
    End If
    Return InputString
  End Function
</script>
<html>
<body>
<form id="Form1" runat="server">
	<asp:datagrid id="MyDataGrid" runat="server"
		width="100%"
		AutoGenerateColumns="false"
		backcolor="#FFFFFF"
		bordercolor="black"
		showfooter="False"
		cellpadding="3"
		cellspacing="0"
		font-name="Verdana"
		font-size="7pt"
		headerstyle-backcolor="#cfcfcf"
		enableviewstate="False">
		<SelectedItemStyle Wrap="False"></SelectedItemStyle>
    <EditItemStyle Wrap="False"></EditItemStyle>
    <AlternatingItemStyle Wrap="False"></AlternatingItemStyle>
    <ItemStyle Wrap="False"></ItemStyle>
    <HeaderStyle Wrap="False"></HeaderStyle>
    <FooterStyle Wrap="False"></FooterStyle>
    <PagerStyle Wrap="False"></PagerStyle>
		<columns>
			<asp:BoundColumn HeaderText="First Name" itemstyle-wrap="False" Datafield = "FirstName" />
			<asp:BoundColumn HeaderText="Last Name" itemstyle-wrap="False" Datafield ="LastName" />
 			<asp:BoundColumn HeaderText="DateTime" itemstyle-wrap="False" Datafield ="DateTime" />
 			<asp:TemplateColumn HeaderText="Employee_ID">
	 				<ItemTemplate>
 						<a href="USER-IntranetUsers.aspx?EmployeeID=<%# DataBinder.Eval(Container.DataItem,"EmployeeID") %>"><%#TruncateMe(DataBinder.Eval(Container.DataItem, "Page"), 30)%></a>
 					</ItemTemplate>
 			</asp:templatecolumn>
 			<asp:BoundColumn HeaderText="Action" itemstyle-wrap="False" Datafield ="Action" />
 			<asp:BoundColumn HeaderText="TrueDomain" itemstyle-wrap="False" Datafield ="TrueDomain" />
 			<asp:BoundColumn HeaderText="TopFlag" itemstyle-wrap="False" Datafield ="TopFlag" />
 			<asp:BoundColumn HeaderText="BottomFlag" itemstyle-wrap="False" Datafield ="BottomFlag" />
 				<asp:TemplateColumn HeaderText="Employee_ID">
	 				<ItemTemplate>
 						<a href="USER-IntranetUsers.aspx?EmployeeID=<%# DataBinder.Eval(Container.DataItem,"EmployeeID") %>">VIEW</a>
 					</ItemTemplate>
 				</asp:templatecolumn>
 		</columns>
	</asp:datagrid>
</form>
</body>
</html>

Now what I was looking for is simply moving the inline code to a code behind page. But I don't want to start an entire new project just to make this simple change. If I have to start a new project everytime I want to make this change then I might as well keep the inline code in there and not worry about it. However I am trying to understand how this works.

I tried moving the inline code to another page, and changing the top referencing a code behind file, but the problem is it requires something to reference or 'inherit' and I have no clue how to tell it your not inherting anything.

Make sense? Let me know.

 
ironhide1975
[qoute]
I tried moving the inline code to another page, and changing the top referencing a code behind file, but the problem is it requires something to reference or 'inherit' and I have no clue how to tell it your not inherting anything.

Make sense? Let me know.
[/qoute]

thats because the ASPX page file must inherit from the VB Class file that u have just created.
 
It's been a couple years but I once had to work on a project (.NET 1.0) where I was forced to write my code using notepad.

Being so long ago I can't remember the exact syntax but it is basically a .aspx page for your rendering and a .cs (or .vb) file for your code behind.

The .aspx file must include a page directive which is similar but slightly different to the following:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="whodidwhatListing.aspx.vb" Inherits="whodidwhatListing.asp" %>

I seem to remember the CodeFile part was replaced with a Source="sourcefile.vb) but again, i'm not entirely sure on that syntax.

Once that directive is set correctly then you can work until you hearts content :)

On a side note, whilst others are correct in saying it will take a lot longer to write cod ein notepad, I found doing so forced me to better understand the was .NET worked, especially as I was just starting out.

Sorry I couldn'y be more precise but hopefully this will help point you in the right direction.

Smeat
 
Sounds like if I want to use something else besides inline code I'm gonna have to start a new project all together. Thank you all for your help, I'm going to close this out for now.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top