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:" & DataBinder.Eval(Container.DataItem,"StoreEmail")
& "&Subject=" & DataBinder.Eval
(Container.DataItem,"ProductName") & " ID=" & DataBinder.Eval
(Container.DataItem,"ProductID") & "&Body=Test" &
DataBinder.Eval(Container.DataItem,"ProductName") & " ID=" &
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:" &
DataBinder.Eval(Container.DataItem,"StoreEmail") & "&Subject="
&
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
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> </DIV>
<DIV><FONT face=Arial size=2>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 </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><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></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 .vb page. </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> </P>
<P><FONT face=Arial size=2>Paolo</FONT></P></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>"IPGrunt" <</FONT><A
href="mailto:
[email protected]"><FONT face=Arial
size=2>
[email protected]</FONT></A><FONT face=Arial size=2>> 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>> On 19 Feb 2005,
"Paolo Pignatelli" <</FONT><A href="mailto
[email protected]"><FONT
face=Arial size=2>
[email protected]</FONT></A><FONT face=Arial size=2>>
postulated <BR>> in </FONT><A
href="face=Arial
size=2>face=Arial
size=2>:<BR>> <BR>> > Yes, exactly, I am trying to bring the
DataBinding statements into <BR>> code<BR>> > behind. Is there also a
VB version of the article?<BR>> > <BR>> > TIA,<BR>>
> said:
> Paolo<BR>> > <BR>> > <BR>> > "Scott Allen"
< said:
href="mailto:
[email protected]"><FONT face=Arial
size=2>
[email protected]</FONT></A><FONT face=Arial size= 2>> wrote
in message<BR>> > </FONT><A
href="news:
[email protected]"><FONT face=Arial
<FONT
face=Arial size=2>...<BR>> >> Hi Paolo:<BR>> >>
>> Are you trying to bring the DataBinding statements into
code said:
behind?<BR>> >><BR>> >> This article may provide some
insight:<BR>> >> </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>> >><BR>> >> Let me know if I've misunderstood the
question...<BR>> >><BR>> >> --<BR>> >>
Scott said:
>> </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>> >><BR>> >> On Sat, 19 Feb 2005 08:23:07 -0500, "Paolo
Pignatelli"<BR>> >> <</FONT><A
href="mailto
[email protected]"><FONT face=Arial
size=2>
[email protected]</FONT></A><FONT face=Arial size=2>>
wrote:<BR>> >><BR>> >> >I have an aspx code behind page
that goes something like this in <BR>> the HTML<BR>> >>
>view:<BR>> >> ><BR>> >> > <asp:HyperLink
id=HyperLink1 runat="server"<BR>> >> ><BR>> >>
>NavigateUrl='<%#"mailto:" &amp;<BR>> >>
>DataBinder.Eval(Container.DataItem,"StoreEmail") &amp; <BR> >
"&amp;Subject="<BR>> > &amp;<BR>> >>
>DataBinder.Eval(Container.DataItem,"ProductName")<BR>> > >
><BR>> >> >...<BR>> >> ><BR>> > > >It
works. For each item in the datagrid, it displays the correct<BR> > >>
>information.<BR>> >> ><BR>> >> >... here is
additional info, if needed ...<BR>> >> ><BR>> > > >Public
Function GetItems(ByVal cartID As String) As SqlDataReader<BR>> >>
><BR>> >> >Dim myConnection As SqlConnection = New
>> >SqlConnection(ConfigurationSettings.AppSettings<BR> >
("ConnectionString"))<BR>> >> ><BR>> >> >Dim myCommand
As SqlCommand = New SqlCommand("ShoppingCartList",<BR>> >>
>myConnection)<BR>> >> ><BR>> >>
>myCommand.CommandType = CommandType.StoredProcedure<BR>> >>
><BR>> >> >Dim parameterCartID As SqlParameter = New
SqlParameter("@CartID",<BR>> >> >SqlDbType.NVarChar,
50) said:
>> ><BR>> >> >parameterCartID.Value = cartID
>> ><BR>> >>
>myCommand.Parameters.Add(parameterCartID)<BR>> >> >
>> >myConnection.Open()<BR>> >> ><BR>> > > >Dim
result As SqlDataReader =<BR>> >>
>myCommand.ExecuteReader(CommandBehavior.CloseConnection)<BR> > >>
><BR>> >> >' Return the datareader result<BR>> >>
><BR>> >> >Return result<BR>> >> ><BR> > >>
>End Function<BR>> >> ><BR>> >>
>///////////////<BR>> >> ><BR>> >> >The sproc is
here:<BR>> >> ><BR>> >> >CREATE PROCEDURE
[dbo].[ShoppingCartList]<BR>> >> ><BR>> >> >@CartID
nvarchar (50)<BR>> >> ><BR>> >> >AS<BR>> >>
><BR>> >> >SELECT
dbo.Products.ProductID,<BR>> >>
>dbo.Products.ProductName said:
>> >dbo.ShoppingCart.Quantity,<BR>> >>
>dbo.Products.Price,<BR>> >> >...<BR>> >>
> said:
>> >FROM dbo.Products
INNER JOIN<BR>> >>
>
dbo.ShoppingCart ON dbo.Products.ProductID <BR>> =<BR>> > >
>dbo.ShoppingCart.ProductID INNER JOIN<BR>> >>
>
...<BR>> >> ><BR>> >> > WHERE<BR>> > >
> Products.ProductID =
ShoppingCart.ProductID<BR>> >> >
... said:
>> ><BR>> >> >GO<BR>> >>
>_________________________________<BR>> >> ><BR>> >>
><BR>> >> ><BR>> >> >Now I would like to bring this
HTML code to code-behind page <BR>> (.vb).<BR>> >> >
>> >So, I have (the name is different because I had to use
a said:
FindControl<BR>> >> >construct)<BR>> >> >
>> >MailHyperLink.NavigateUrl = .... (Do I use the same quote, '<%#
<BR>> .... %>'<BR>> > ..<BR>> >> >what do I put
here??)<BR>> >> ><BR>> >> >How do I
continue? said:
>> ><BR>> >> >If you could also explain the "why" for your
answer ,that would <BR>> be even<BR>> >> >better.
>> ><BR>> >> >TIA,<BR>> >> ><BR> > >>
>Paolo<BR>> >> ><BR>> >><BR>> > <BR> > >
<BR>> <BR>> You don't use the script delimiters in
codebehind. said:
<BR>> Your codebehind file is pure code. Essentially, the task is to create
<BR>> a page object that implements your particular task. In this case,
<BR>> looks like you wish to fill a cart, which is a datagrid?
<BR>> Looks like you already have the code for creating the
sqldatareader.<BR>> <BR>> The datagrid accepts the SqlDataReader as it's
DataSource. Given:<BR>> <BR>> Dim grid As DataGrid<BR>>
assign the reader as:<BR>> <BR>> grid.DataSource = GetItems
(someCartID)<BR>> <BR>> then you need to bind the two<BR>>
grid.DataBind()<BR>> <BR>> <BR>> Is this what you're talking about? If
so, take a look at the .NET <BR>> examples for ADO, of which you can see 2 or
3 examples of this kind <BR>> of thing, using codebehind with asp.net
datalist type controls.<BR>> <BR>> </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>> <BR>> -- 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
& "&Subject=" & ProductName
& "&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