How to bring aspx code (in HTML view) to the aspx.vb code-behind?

P

Paolo Pignatelli

I have an aspx code behind page that goes something like this in the HTML
view:

<asp:HyperLink id=HyperLink1 runat="server"

NavigateUrl='<%#"mailto:" &amp;
DataBinder.Eval(Container.DataItem,"StoreEmail") &amp; "&amp;Subject=" &amp;
DataBinder.Eval(Container.DataItem,"ProductName")

....

It works. For each item in the datagrid, it displays the correct
information.

.... here is additional info, if needed ...

Public Function GetItems(ByVal cartID As String) As SqlDataReader

Dim myConnection As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

Dim myCommand As SqlCommand = New SqlCommand("ShoppingCartList",
myConnection)

myCommand.CommandType = CommandType.StoredProcedure

Dim parameterCartID As SqlParameter = New SqlParameter("@CartID",
SqlDbType.NVarChar, 50)

parameterCartID.Value = cartID

myCommand.Parameters.Add(parameterCartID)

myConnection.Open()

Dim result As SqlDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)

' Return the datareader result

Return result

End Function

///////////////

The sproc is here:

CREATE PROCEDURE [dbo].[ShoppingCartList]

@CartID nvarchar (50)

AS

SELECT dbo.Products.ProductID,
dbo.Products.ProductName,
dbo.ShoppingCart.Quantity,
dbo.Products.Price,
....

FROM dbo.Products INNER JOIN
dbo.ShoppingCart ON dbo.Products.ProductID =
dbo.ShoppingCart.ProductID INNER JOIN
...

WHERE
Products.ProductID = ShoppingCart.ProductID
...

GO
_________________________________



Now I would like to bring this HTML code to code-behind page (.vb).

So, I have (the name is different because I had to use a FindControl
construct)

MailHyperLink.NavigateUrl = .... (Do I use the same quote, '<%# .... %>' ..
what do I put here??)

How do I continue?

If you could also explain the "why" for your answer ,that would be even
better.

TIA,

Paolo
 
P

Paolo Pignatelli

Yes, exactly, I am trying to bring the DataBinding statements into code
behind. Is there also a VB version of the article?

TIA,

Paolo


Scott Allen said:
Hi Paolo:

Are you trying to bring the DataBinding statements into code behind?

This article may provide some insight:
http://odetocode.com/Articles/278.aspx

Let me know if I've misunderstood the question...

--
Scott
http://www.OdeToCode.com/blogs/scott/

I have an aspx code behind page that goes something like this in the HTML
view:

<asp:HyperLink id=HyperLink1 runat="server"

NavigateUrl='<%#"mailto:" &amp;
DataBinder.Eval(Container.DataItem,"StoreEmail") &amp; "&amp;Subject=" &amp;
DataBinder.Eval(Container.DataItem,"ProductName")

...

It works. For each item in the datagrid, it displays the correct
information.

... here is additional info, if needed ...

Public Function GetItems(ByVal cartID As String) As SqlDataReader

Dim myConnection As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

Dim myCommand As SqlCommand = New SqlCommand("ShoppingCartList",
myConnection)

myCommand.CommandType = CommandType.StoredProcedure

Dim parameterCartID As SqlParameter = New SqlParameter("@CartID",
SqlDbType.NVarChar, 50)

parameterCartID.Value = cartID

myCommand.Parameters.Add(parameterCartID)

myConnection.Open()

Dim result As SqlDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)

' Return the datareader result

Return result

End Function

///////////////

The sproc is here:

CREATE PROCEDURE [dbo].[ShoppingCartList]

@CartID nvarchar (50)

AS

SELECT dbo.Products.ProductID,
dbo.Products.ProductName,
dbo.ShoppingCart.Quantity,
dbo.Products.Price,
...

FROM dbo.Products INNER JOIN
dbo.ShoppingCart ON dbo.Products.ProductID =
dbo.ShoppingCart.ProductID INNER JOIN
...

WHERE
Products.ProductID = ShoppingCart.ProductID
...

GO
_________________________________



Now I would like to bring this HTML code to code-behind page (.vb).

So, I have (the name is different because I had to use a FindControl
construct)

MailHyperLink.NavigateUrl = .... (Do I use the same quote, '<%# .... %>' ...
what do I put here??)

How do I continue?

If you could also explain the "why" for your answer ,that would be even
better.

TIA,

Paolo
 
I

IPGrunt

Yes, exactly, I am trying to bring the DataBinding statements into code
behind. Is there also a VB version of the article?

TIA,

Paolo


Scott Allen said:
Hi Paolo:

Are you trying to bring the DataBinding statements into code behind?

This article may provide some insight:
http://odetocode.com/Articles/278.aspx

Let me know if I've misunderstood the question...
"&amp;Subject="
&amp;
DataBinder.Eval(Container.DataItem,"ProductName")

...

It works. For each item in the datagrid, it displays the correct
information.

... here is additional info, if needed ...

Public Function GetItems(ByVal cartID As String) As SqlDataReader

Dim myConnection As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings ("ConnectionString"))

Dim myCommand As SqlCommand = New SqlCommand("ShoppingCartList",
myConnection)

myCommand.CommandType = CommandType.StoredProcedure

Dim parameterCartID As SqlParameter = New SqlParameter("@CartID",
SqlDbType.NVarChar, 50)

parameterCartID.Value = cartID

myCommand.Parameters.Add(parameterCartID)

myConnection.Open()

Dim result As SqlDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)

' Return the datareader result

Return result

End Function

///////////////

The sproc is here:

CREATE PROCEDURE [dbo].[ShoppingCartList]

@CartID nvarchar (50)

AS

SELECT dbo.Products.ProductID,
dbo.Products.ProductName,
dbo.ShoppingCart.Quantity,
dbo.Products.Price,
...

FROM dbo.Products INNER JOIN
dbo.ShoppingCart ON dbo.Products.ProductID =
dbo.ShoppingCart.ProductID INNER JOIN
...

WHERE
Products.ProductID = ShoppingCart.ProductID
...

GO
_________________________________



Now I would like to bring this HTML code to code-behind page (.vb).

So, I have (the name is different because I had to use a FindControl
construct)

MailHyperLink.NavigateUrl = .... (Do I use the same quote, '<%#
..... %>'
..

You don't use the script delimiters in codebehind.

Your codebehind file is pure code. Essentially, the task is to create
a page object that implements your particular task. In this case,
looks like you wish to fill a cart, which is a datagrid?

Looks like you already have the code for creating the sqldatareader.

The datagrid accepts the SqlDataReader as it's DataSource. Given:

Dim grid As DataGrid

You'd assign the reader as:

grid.DataSource = GetItems (someCartID)

then you need to bind the two

grid.DataBind()


Is this what you're talking about? If so, take a look at the .NET
examples for ADO, of which you can see 2 or 3 examples of this kind
of thing, using codebehind with asp.net datalist type controls.

http://samples.gotdotnet.com/quickstart/aspplus/

-- ipgrunt
 
P

Paolo Pignatelli

Hello, and Thank you!

Yes you are correct. What I am trying to do is to be able to access one of the fields of the DataGrid, (the StoreEmail one, which is a hyperlink) and then add the other information about how the hyperlink works, other parameters, so that I can pass it info such as StoreName, ProductName etc... . The hyperlink then opens the email program (mailto:) with the address, the subject, the itemnumber, etc... So, let's say that the datagriod has colums Productname and email. The Email link then opens an email, and the Subject address and other stuff filled in. If I just have a DatgaGrid
' Databind Gridcontrol with Shopping Cart Items

dgShoppingCart.DataSource = cart.GetItems(cartId)

dgShoppingCart.DataBind()

'TEST

Session("CartFull") = 1

'TEST

all the colums appear fine. I then add to the HTML page the inline code

<asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%#"mailto:" &amp; DataBinder.Eval(Container.DataItem,"StoreEmail") &amp; "&amp;Subject=" &amp; DataBinder.Eval(Container.DataItem,"ProductName") &amp; " ID=" &amp; DataBinder.Eval(Container.DataItem,"ProductID") &amp; "&amp;Body=Test" &amp; DataBinder.Eval(Container.DataItem,"ProductName") &amp; " ID=" &amp; DataBinder.Eval(Container.DataItem,"ProductID") %>'>HyperLink</asp:HyperLink>

I get the hyperlinks the way I want them, everything works fine

BUT.... Just out of an obtuse wish to separate code better, I would like to put all the info in the HTML page in the .vb page.

All and any further help is much appreciated.



Paolo




IPGrunt said:
Yes, exactly, I am trying to bring the DataBinding statements into code
behind. Is there also a VB version of the article?

TIA,

Paolo


Scott Allen said:
Hi Paolo:

Are you trying to bring the DataBinding statements into code behind?

This article may provide some insight:
http://odetocode.com/Articles/278.aspx

Let me know if I've misunderstood the question...

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Sat, 19 Feb 2005 08:23:07 -0500, "Paolo Pignatelli"

I have an aspx code behind page that goes something like this in the HTML
view:

<asp:HyperLink id=HyperLink1 runat="server"

NavigateUrl='<%#"mailto:" &amp;
DataBinder.Eval(Container.DataItem,"StoreEmail") &amp;
"&amp;Subject="
&amp;
DataBinder.Eval(Container.DataItem,"ProductName")

...

It works. For each item in the datagrid, it displays the correct
information.

... here is additional info, if needed ...

Public Function GetItems(ByVal cartID As String) As SqlDataReader

Dim myConnection As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings ("ConnectionString"))

Dim myCommand As SqlCommand = New SqlCommand("ShoppingCartList",
myConnection)

myCommand.CommandType = CommandType.StoredProcedure

Dim parameterCartID As SqlParameter = New SqlParameter("@CartID",
SqlDbType.NVarChar, 50)

parameterCartID.Value = cartID

myCommand.Parameters.Add(parameterCartID)

myConnection.Open()

Dim result As SqlDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)

' Return the datareader result

Return result

End Function

///////////////

The sproc is here:

CREATE PROCEDURE [dbo].[ShoppingCartList]

@CartID nvarchar (50)

AS

SELECT dbo.Products.ProductID,
dbo.Products.ProductName,
dbo.ShoppingCart.Quantity,
dbo.Products.Price,
...

FROM dbo.Products INNER JOIN
dbo.ShoppingCart ON dbo.Products.ProductID =
dbo.ShoppingCart.ProductID INNER JOIN
...

WHERE
Products.ProductID = ShoppingCart.ProductID
...

GO
_________________________________



Now I would like to bring this HTML code to code-behind page (.vb).

So, I have (the name is different because I had to use a FindControl
construct)

MailHyperLink.NavigateUrl = .... (Do I use the same quote, '<%#
.... %>'
..
what do I put here??)

How do I continue?

If you could also explain the "why" for your answer ,that would be even
better.

TIA,

Paolo

You don't use the script delimiters in codebehind.

Your codebehind file is pure code. Essentially, the task is to create
a page object that implements your particular task. In this case,
looks like you wish to fill a cart, which is a datagrid?

Looks like you already have the code for creating the sqldatareader.

The datagrid accepts the SqlDataReader as it's DataSource. Given:

Dim grid As DataGrid

You'd assign the reader as:

grid.DataSource = GetItems (someCartID)

then you need to bind the two

grid.DataBind()


Is this what you're talking about? If so, take a look at the .NET
examples for ADO, of which you can see 2 or 3 examples of this kind
of thing, using codebehind with asp.net datalist type controls.

http://samples.gotdotnet.com/quickstart/aspplus/

-- ipgrunt
 
I

IPGrunt

Hello, and Thank you!

Yes you are correct. What I am trying to do is to be able to
access one of the fields of the DataGrid, (the StoreEmail one, which
is a hyperlink) and then add the other information about how the
hyperlink works, other parameters, so that I can pass it info such as
StoreName, ProductName etc... . The hyperlink then opens the email
program (mailto:) with the address, the subject, the itemnumber,
etc... So, let's say that the datagriod has colums Productname and
email. The Email link then opens an email, and the Subject address
and other stuff filled in. If I just have a DatgaGrid
' Databind Gridcontrol with Shopping Cart Items

dgShoppingCart.DataSource = cart.GetItems(cartId)

dgShoppingCart.DataBind()

'TEST

Session("CartFull") = 1

'TEST

all the colums appear fine. I then add to the HTML page the inline code

<asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%
#"mailto:" &amp; DataBinder.Eval(Container.DataItem,"StoreEmail")
&amp; "&amp;Subject=" &amp; DataBinder.Eval
(Container.DataItem,"ProductName") &amp; " ID=" &amp; DataBinder.Eval
(Container.DataItem,"ProductID") &amp; "&amp;Body=Test" &amp;
DataBinder.Eval(Container.DataItem,"ProductName") &amp; " ID=" &amp;
DataBinder.Eval(Container.DataItem,"ProductID") %>'>HyperLink
I get the hyperlinks the way I want them, everything works fine

BUT.... Just out of an obtuse wish to separate code better, I would
like to put all the info in the HTML page in the .vb page.
All and any further help is much appreciated.



Paolo




Yes, exactly, I am trying to bring the DataBinding statements
into
code
behind. Is there also a VB version of the article?

TIA,

Paolo


Hi Paolo:

Are you trying to bring the DataBinding statements into code behind?

This article may provide some insight:
http://odetocode.com/Articles/278.aspx

Let me know if I've misunderstood the question...

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Sat, 19 Feb 2005 08:23:07 -0500, "Paolo Pignatelli"

I have an aspx code behind page that goes something like this
in
the HTML
view:

<asp:HyperLink id=HyperLink1 runat="server"

NavigateUrl='<%#"mailto:" &amp;
DataBinder.Eval(Container.DataItem,"StoreEmail") &amp; "&amp;Subject="
&amp;
DataBinder.Eval(Container.DataItem,"ProductName")

...

It works. For each item in the datagrid, it displays the correct
information.

... here is additional info, if needed ...

Public Function GetItems(ByVal cartID As String) As SqlDataReader

Dim myConnection As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings ("ConnectionString"))

Dim myCommand As SqlCommand = New SqlCommand ("ShoppingCartList",
myConnection)

myCommand.CommandType = CommandType.StoredProcedure

Dim parameterCartID As SqlParameter = New SqlParameter("@CartID",
SqlDbType.NVarChar, 50)

parameterCartID.Value = cartID

myCommand.Parameters.Add(parameterCartID)

myConnection.Open()

Dim result As SqlDataReader
myCommand.ExecuteReader(CommandBehavior.CloseConnection)

' Return the datareader result

Return result

End Function

///////////////

The sproc is here:

CREATE PROCEDURE [dbo].[ShoppingCartList]

@CartID nvarchar (50)

AS

SELECT dbo.Products.ProductID,
dbo.Products.ProductName,
dbo.ShoppingCart.Quantity,
dbo.Products.Price,
...

FROM dbo.Products INNER JOIN
dbo.ShoppingCart ON
dbo.Products.ProductID
dbo.ShoppingCart.ProductID INNER JOIN
...

WHERE
Products.ProductID = ShoppingCart.ProductID
...

GO
_________________________________



Now I would like to bring this HTML code to code-behind page (.vb).

So, I have (the name is different because I had to use a FindControl
construct)

MailHyperLink.NavigateUrl = .... (Do I use the same quote, '<%
#
.... %>'
..
what do I put here??)

How do I continue?

If you could also explain the "why" for your answer ,that
would
be even
better.

TIA,

Paolo

You don't use the script delimiters in codebehind.

Your codebehind file is pure code. Essentially, the task is to create
a page object that implements your particular task. In this case,
looks like you wish to fill a cart, which is a datagrid?

Looks like you already have the code for creating the sqldatareader.

The datagrid accepts the SqlDataReader as it's DataSource. Given:

Dim grid As DataGrid

You'd assign the reader as:

grid.DataSource = GetItems (someCartID)

then you need to bind the two

grid.DataBind()


Is this what you're talking about? If so, take a look at the .NET
examples for ADO, of which you can see 2 or 3 examples of this kind
of thing, using codebehind with asp.net datalist type controls.

http://samples.gotdotnet.com/quickstart/aspplus/

-- ipgrunt
------=_NextPart_000_0008_01C51727.C29BF010
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859- 1">
<META content="MSHTML 6.00.3790.259" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=Arial size=2>Hello, and Thank you!</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Yes you are correct.&nbsp; What I am trying to do
is to be able to access one of the fields of the DataGrid, (the StoreEmail one,
which is a hyperlink) and then add the other information about how the hyperlink
works, other parameters, so that I can pass it info such as StoreName,
ProductName etc... . The hyperlink then opens the email program (mailto:) with
the address, the subject, the itemnumber, etc... So, let's say that the
datagriod has colums Productname and email.&nbsp; The Email link then opens an
email, and the Subject address and other stuff filled in.&nbsp; If I just have a
DatgaGrid </FONT></DIV>
<DIV>
<P><FONT face=Arial size=2>' Databind Gridcontrol with Shopping Cart
Items</FONT></P>
<P><FONT face=Arial size=2>dgShoppingCart.DataSource =
cart.GetItems(cartId)</FONT></P>
<P><FONT face=Arial size=2>dgShoppingCart.DataBind()</FONT></P>
<P><FONT face=Arial size=2>'TEST</FONT></P>
<P><FONT face=Arial size=2>Session("CartFull") = 1</FONT></P>
<P><FONT face=Arial size=2>'TEST</FONT></P>
<P><FONT face=Arial size=2>all the colums appear fine. I then add to the HTML
page the inline code</FONT></P>
<P><FONT face=Arial size=2>&lt;asp:HyperLink id=HyperLink1 runat="server"
NavigateUrl='&lt;%#"mailto:" &amp;amp;
DataBinder.Eval(Container.DataItem,"StoreEmail") &amp;amp; "&amp;amp;Subject="
&amp;amp; DataBinder.Eval(Container.DataItem,"ProductName") &amp;amp; " ID="
&amp;amp; DataBinder.Eval(Container.DataItem,"ProductID") &amp;amp;
"&amp;amp;Body=Test" &amp;amp; DataBinder.Eval (Container.DataItem,"ProductName")
&amp;amp; " ID=" &amp;amp; DataBinder.Eval (Container.DataItem,"ProductID")
%&gt;'&gt;HyperLink&lt;/asp:HyperLink&gt;</FONT></P>
<P><FONT face=Arial size=2>I get the hyperlinks the way I want them, everything
works fine</FONT></P>
<P><FONT face=Arial size=2><STRONG>BUT</STRONG>.... Just out of an obtuse wish
to separate code better, I would like to put all the info in the HTML page in
the&nbsp; .vb page.&nbsp; </FONT></P>
<P><FONT face=Arial size=2>All and any further help is much
appreciated.</FONT></P>
<P><FONT face=Arial size=2></FONT>&nbsp;</P>
<P><FONT face=Arial size=2>Paolo</FONT></P></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>"IPGrunt" &lt;</FONT><A
href="mailto:[email protected]"><FONT face=Arial
size=2>[email protected]</FONT></A><FONT face=Arial size=2>&gt; wrote in message
</FONT><A href="130.133.1.4"><FONT
face=Arial
size=2>news:[email protected]</FONT>
face=Arial size=2>...</FONT></DIV><FONT face=Arial size=2>&gt; On 19 Feb 2005,
"Paolo Pignatelli" &lt;</FONT><A href="mailto:p[email protected]"><FONT
face=Arial size=2>[email protected]</FONT></A><FONT face=Arial size=2>&gt;
postulated <BR>&gt; in </FONT><A
href="face=Arial
size=2>face=Arial
size=2>:<BR>&gt; <BR>&gt; &gt; Yes, exactly, I am trying to bring the
DataBinding statements into <BR>&gt; code<BR>&gt; &gt; behind. Is there also a
VB version of the article?<BR>&gt; &gt; <BR>&gt; &gt; TIA,<BR>&gt;
&gt; said:
&gt; Paolo<BR>&gt; &gt; <BR>&gt; &gt; <BR>&gt; &gt; "Scott Allen"
&lt; said:
href="mailto:[email protected]"><FONT face=Arial
size=2>[email protected]</FONT></A><FONT face=Arial size= 2>&gt; wrote
in message<BR>&gt; &gt; </FONT><A
href="news:[email protected]"><FONT face=Arial
<FONT
face=Arial size=2>...<BR>&gt; &gt;&gt; Hi Paolo:<BR>&gt; &gt;&gt;
&gt;&gt; Are you trying to bring the DataBinding statements into
code said:
behind?<BR>&gt; &gt;&gt;<BR>&gt; &gt;&gt; This article may provide some
insight:<BR>&gt; &gt;&gt; </FONT><A
href="http://odetocode.com/Articles/278.aspx"><FONT face=Arial
size=2>http://odetocode.com/Articles/278.aspx</FONT></A><BR><FONT face=Arial
size=2>&gt; &gt;&gt;<BR>&gt; &gt;&gt; Let me know if I've misunderstood the
question...<BR>&gt; &gt;&gt;<BR>&gt; &gt;&gt; --<BR>&gt; &gt;&gt;
Scott said:
&gt;&gt; </FONT><A href="http://www.OdeToCode.com/blogs/scott/"> <FONT face=Arial
size=2>http://www.OdeToCode.com/blogs/scott/</FONT></A><BR><FONT face=Arial
size=2>&gt; &gt;&gt;<BR>&gt; &gt;&gt; On Sat, 19 Feb 2005 08:23:07 -0500, "Paolo
Pignatelli"<BR>&gt; &gt;&gt; &lt;</FONT><A
href="mailto:p[email protected]"><FONT face=Arial
size=2>[email protected]</FONT></A><FONT face=Arial size=2>&gt;
wrote:<BR>&gt; &gt;&gt;<BR>&gt; &gt;&gt; &gt;I have an aspx code behind page
that goes something like this in <BR>&gt; the HTML<BR>&gt; &gt;&gt;
&gt;view:<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt;&gt; &gt; &lt;asp:HyperLink
id=HyperLink1 runat="server"<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt;&gt;
&gt;NavigateUrl='&lt;%#"mailto:" &amp;amp;<BR>&gt; &gt;&gt;
&gt;DataBinder.Eval(Container.DataItem,"StoreEmail") &amp;amp; <BR> &gt;
"&amp;amp;Subject="<BR>&gt; &gt; &amp;amp;<BR>&gt; &gt;&gt;
&gt;DataBinder.Eval(Container.DataItem,"ProductName")<BR>&gt; &gt; &gt;
&gt;<BR>&gt; &gt;&gt; &gt;...<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt; &gt; &gt;It
works. For each item in the datagrid, it displays the correct<BR> &gt; &gt;&gt;
&gt;information.<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt;&gt; &gt;... here is
additional info, if needed ...<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt; &gt; &gt;Public
Function GetItems(ByVal cartID As String) As SqlDataReader<BR>&gt; &gt;&gt;
&gt;<BR>&gt; &gt;&gt; &gt;Dim myConnection As SqlConnection = New
&gt;&gt; &gt;SqlConnection(ConfigurationSettings.AppSettings<BR> &gt;
("ConnectionString"))<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt;&gt; &gt;Dim myCommand
As SqlCommand = New SqlCommand("ShoppingCartList",<BR>&gt; &gt;&gt;
&gt;myConnection)<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt;&gt;
&gt;myCommand.CommandType = CommandType.StoredProcedure<BR>&gt; &gt;&gt;
&gt;<BR>&gt; &gt;&gt; &gt;Dim parameterCartID As SqlParameter = New
SqlParameter("@CartID",<BR>&gt; &gt;&gt; &gt;SqlDbType.NVarChar,
50) said:
&gt;&gt; &gt;<BR>&gt; &gt;&gt; &gt;parameterCartID.Value = cartID
&gt;&gt; &gt;<BR>&gt; &gt;&gt;
&gt;myCommand.Parameters.Add(parameterCartID)<BR>&gt; &gt;&gt; &gt;
&gt;&gt; &gt;myConnection.Open()<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt; &gt; &gt;Dim
result As SqlDataReader =<BR>&gt; &gt;&gt;
&gt;myCommand.ExecuteReader(CommandBehavior.CloseConnection)<BR> &gt; &gt;&gt;
&gt;<BR>&gt; &gt;&gt; &gt;' Return the datareader result<BR>&gt; &gt;&gt;
&gt;<BR>&gt; &gt;&gt; &gt;Return result<BR>&gt; &gt;&gt; &gt;<BR> &gt; &gt;&gt;
&gt;End Function<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt;&gt;
&gt;///////////////<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt;&gt; &gt;The sproc is
here:<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt;&gt; &gt;CREATE PROCEDURE
[dbo].[ShoppingCartList]<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt;&gt; &gt;@CartID
nvarchar (50)<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt;&gt; &gt;AS<BR>&gt; &gt;&gt;
&gt;<BR>&gt; &gt;&gt; &gt;SELECT&nbsp;&nbsp;&nbsp;&nbsp;
dbo.Products.ProductID,<BR>&gt; &gt;&gt;
&gt;dbo.Products.ProductName said:
&gt;&gt; &gt;dbo.ShoppingCart.Quantity,<BR>&gt; &gt;&gt;
&gt;dbo.Products.Price,<BR>&gt; &gt;&gt; &gt;...<BR>&gt; &gt;&gt;
&gt; said:
&gt;&gt; &gt;FROM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbo.Products
INNER JOIN<BR>&gt; &gt;&gt;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
dbo.ShoppingCart ON dbo.Products.ProductID <BR>&gt; =<BR>&gt; &gt; &gt;
&gt;dbo.ShoppingCart.ProductID INNER JOIN<BR>&gt; &gt;&gt;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
...<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt;&gt; &gt; WHERE<BR>&gt; &gt; &gt;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Products.ProductID =
ShoppingCart.ProductID<BR>&gt; &gt;&gt; &gt;&nbsp;&nbsp;&nbsp;
&nbsp; ... said:
&gt;&gt; &gt;<BR>&gt; &gt;&gt; &gt;GO<BR>&gt; &gt;&gt;
&gt;_________________________________<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt;&gt;
&gt;<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt;&gt; &gt;Now I would like to bring this
HTML code to code-behind page <BR>&gt; (.vb).<BR>&gt; &gt;&gt; &gt;
&gt;&gt; &gt;So, I have (the name is different because I had to use
a said:
FindControl<BR>&gt; &gt;&gt; &gt;construct)<BR>&gt; &gt;&gt; &gt;
&gt;&gt; &gt;MailHyperLink.NavigateUrl = .... (Do I use the same quote, '&lt;%#
<BR>&gt; .... %&gt;'<BR>&gt; &gt; ..<BR>&gt; &gt;&gt; &gt;what do I put
here??)<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt;&gt; &gt;How do I
continue? said:
&gt;&gt; &gt;<BR>&gt; &gt;&gt; &gt;If you could also explain the "why" for your
answer ,that would <BR>&gt; be even<BR>&gt; &gt;&gt; &gt;better.
&gt;&gt; &gt;<BR>&gt; &gt;&gt; &gt;TIA,<BR>&gt; &gt;&gt; &gt;<BR> &gt; &gt;&gt;
&gt;Paolo<BR>&gt; &gt;&gt; &gt;<BR>&gt; &gt;&gt;<BR>&gt; &gt; <BR> &gt; &gt;
<BR>&gt; <BR>&gt; You don't use the script delimiters in
codebehind. said:
<BR>&gt; Your codebehind file is pure code. Essentially, the task is to create
<BR>&gt; a page object that implements your particular task. In this case,
<BR>&gt; looks like you wish to fill a cart, which is a datagrid?
<BR>&gt; Looks like you already have the code for creating the
sqldatareader.<BR>&gt; <BR>&gt; The datagrid accepts the SqlDataReader as it's
DataSource. Given:<BR>&gt; <BR>&gt; Dim grid As DataGrid<BR>&gt;
assign the reader as:<BR>&gt; <BR>&gt; grid.DataSource = GetItems
(someCartID)<BR>&gt; <BR>&gt; then you need to bind the two<BR>&gt;
grid.DataBind()<BR>&gt; <BR>&gt; <BR>&gt; Is this what you're talking about? If
so, take a look at the .NET <BR>&gt; examples for ADO, of which you can see 2 or
3 examples of this kind <BR>&gt; of thing, using codebehind with asp.net
datalist type controls.<BR>&gt; <BR>&gt; </FONT><A
href="http://samples.gotdotnet.com/quickstart/aspplus/"><FONT face=Arial
size=2>http://samples.gotdotnet.com/quickstart/aspplus/</FONT></A>
face=Arial size=2>&gt; <BR>&gt; -- ipgrunt</FONT></BODY></HTML>

------=_NextPart_000_0008_01C51727.C29BF010--



Honestly, I believe you are doing this correct way. In my philosophy
of code design, (which I believe is similar to M$ thinking), the
codebehind page serves up the data while the ASPX page formats and
displays the data.

But, if you really want to, you can build the values for the
Hyperlink control in codebehind. The overal scheme is to read the
SqlDataReader values and assign to the Webcontrol.

First you must read the SqlDataReader values....It is convenient to
place them in local variables for this example, (but not necessary)

Dim StoreEmail As String
Dim ProductName As String
Dim ProductID As Int


Then assume a SqlDataReader as so:


Dim SqlDataReader As myReader = cart.GetItems(cartId)


you need to test if it was successful:


If myReader.HasData Then


Then, inside this If statement, read the data.


myReader.Read()


This loads the reader with the first row of data (only one row if
your Sproc worked properly). You then read the values into your local
variables IN THE ORDER IN WHICH THEY ARE ASSIGNED WITHIN THE SPROC.
This is important. So, if your SQL statement is:

SELECT StoreEmail, ProductName, ProductID FROM Products WHERE
ProductID = @ProductID


then you read them in this order:


StoreEmail = myReader.GetString(0)
ProductName = myReader.GetString (1)
ProductID = myReader.GetInt32 (2)


Don't forget to close the reader...

myReader.Close()

(By the way, when you open the reader with the
CommandBehavior.CloseConnection argument, like this (in C#):


SqlDataReader rdr =
myCommand.ExecuteReader(CommandBehavior.CloseConnection);


then the connection will close when the reader closes. This is
important as you will usually return a reader from a business or data
object (using n-tier design), and no longer have access to the
connection. Even with garbage collection, you must close those
connections when finished with them.)


Now you can build your link string for the web control:


HyperLink1.NavigateUrl = "mailto: " & StoreEmail
& "&amp;Subject=" & ProductName
& "&amp;ID=" & ProductID.ToString()


When the codebehind finishes, the page is set with this link. You
might do this in the PageLoad method, or in the delegate responding
to a button push.

So, please excuse my syntactical mistakes in VB (I code in C# most of
the time), but I hope you get the idea.

Again, there is nothing wrong with how you are doing this in ASPX.
But, if you want to use codebehind, you can because of the wonderful
design of WebControls that make them all objects and how dotnet
provides access to all the fields.

Have fun!

-- ipgrunt
 
P

Paolo Pignatelli

Wow! Thank you ever so much not only for the programming help, but also in
explaining the philosophy of how the code is divided.

Paolo

(snip)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top