Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: hyperlink menu from datareader


Message #1 by "John Tyson" <jtyson@t...> on Fri, 24 May 2002 09:55:06 -0700
Hi,

I have a SQL table called UserMenu.  Based on the user's department, I
read in available menu links from the UserMenu table to my page with a
datareader.  I can list them as text fine, but I want to list them as
asp:hyperlinks.  Can anyone show me how to do this?  I do not know how
to bind the datareader output to a DataView or DataTable, and then place
it in a DataList or Repeater control.

Here is a snippet of my code (right now I am simply reading into a
span):

Try
   dbconn.Open()
   dr =3D cmd.ExecuteReader(CommandBehavior.CloseConnection)

   spnUsrMenu.InnerHtml =3D ""
   While dr.Read()

   If dr.IsDBNull(0) Then
      spnUsrMenu.InnerHtml =3D spnUsrMenu.InnerHtml & dr.GetString(1) &
"<br>"
   Else
      spnUsrMenu.InnerHtml =3D spnUsrMenu.InnerHtml & dr.GetString(0) &
"/" & dr.GetString(1) & "<br>"
   End If

   End While

Any help or code examples would be much appreciated!  Thanks,

John
Message #2 by Imar@S... on Sat, 25 May 2002 20:23:39
Hi John,

You can simply put a hrefs in your repeater control, like this:

<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
  <table>
</HeaderTemplate>
<ItemTemplate>
<tr>
   <td>
   <a href="<%# Container.DataItem("URL") %>"><%# Container.DataItem
("URL") %></a>
   </td>
</tr>				
</ItemTemplate>
<FooterTemplate>
   </table>
</FooterTemplate>			
</asp:Repeater>

and then simply bind the datareader to the Repeater:

   Dim myReader As SqlDataReader
   myConnection.Open()
   myReader = myCommand.ExecuteReader
   Repeater1.DataSource = myReader
   Repeater1.DataBind()
   myReader.Close()
   myConnection.Close()

If you need to know whether records were returned or not, you could get 
your data into a dataset for instance and then bind the repeater to that 
dataset. Another alternative is to use an output parameter that returns 
the number of records.

HtH

Imar


> Hi,

I have a SQL table called UserMenu.  Based on the user's department, I
read in available menu links from the UserMenu table to my page with a
datareader.  I can list them as text fine, but I want to list them as
asp:hyperlinks.  Can anyone show me how to do this?  I do not know how
to bind the datareader output to a DataView or DataTable, and then place
it in a DataList or Repeater control.

Here is a snippet of my code (right now I am simply reading into a
span):

Try
   dbconn.Open()
   dr =3D cmd.ExecuteReader(CommandBehavior.CloseConnection)

   spnUsrMenu.InnerHtml =3D ""
   While dr.Read()

   If dr.IsDBNull(0) Then
      spnUsrMenu.InnerHtml =3D spnUsrMenu.InnerHtml & dr.GetString(1) &
"<br>"
   Else
      spnUsrMenu.InnerHtml =3D spnUsrMenu.InnerHtml & dr.GetString(0) &
"/" & dr.GetString(1) & "<br>"
   End If

   End While

Any help or code examples would be much appreciated!  Thanks,

John
Message #3 by "John Tyson" <jtyson@t...> on Tue, 28 May 2002 07:39:27 -0700
Hi Imar,

Thanks for the response.  I actually got this working with a DataList
before I left for the weekend - so here's the dumb question: can you
tell me which is better to use, a Repeater or a DataList, and why?

Thanks,

John

-----Original Message-----
From: Imar@S... [mailto:Imar@S...]
Sent: Saturday, May 25, 2002 1:24 PM
To: aspx_beginners
Subject: [aspx_beginners] Re: hyperlink menu from datareader

Hi John,

You can simply put a hrefs in your repeater control, like this:

<asp:Repeater id=3D"Repeater1" runat=3D"server">
<HeaderTemplate>
  <table>
</HeaderTemplate>
<ItemTemplate>
<tr>
   <td>
   <a href=3D"<%# Container.DataItem("URL") %>"><%# Container.DataItem
("URL") %></a>
   </td>
</tr>			=09
</ItemTemplate>
<FooterTemplate>
   </table>
</FooterTemplate>		=09
</asp:Repeater>

and then simply bind the datareader to the Repeater:

   Dim myReader As SqlDataReader
   myConnection.Open()
   myReader =3D myCommand.ExecuteReader
   Repeater1.DataSource =3D myReader
   Repeater1.DataBind()
   myReader.Close()
   myConnection.Close()

If you need to know whether records were returned or not, you could get
your data into a dataset for instance and then bind the repeater to that

dataset. Another alternative is to use an output parameter that returns
the number of records.

HtH

Imar


> Hi,

I have a SQL table called UserMenu.  Based on the user's department, I
read in available menu links from the UserMenu table to my page with a
datareader.  I can list them as text fine, but I want to list them as
asp:hyperlinks.  Can anyone show me how to do this?  I do not know how
to bind the datareader output to a DataView or DataTable, and then place
it in a DataList or Repeater control.

Here is a snippet of my code (right now I am simply reading into a
span):

Try
   dbconn.Open()
   dr =3D3D cmd.ExecuteReader(CommandBehavior.CloseConnection)

   spnUsrMenu.InnerHtml =3D3D ""
   While dr.Read()

   If dr.IsDBNull(0) Then
      spnUsrMenu.InnerHtml =3D3D spnUsrMenu.InnerHtml & dr.GetString(1) 
&
"<br>"
   Else
      spnUsrMenu.InnerHtml =3D3D spnUsrMenu.InnerHtml & dr.GetString(0) 
&
"/" & dr.GetString(1) & "<br>"
   End If

   End While

Any help or code examples would be much appreciated!  Thanks,

John
Message #4 by Imar Spaanjaars <Imar@S...> on Tue, 28 May 2002 18:43:14 +0200
Hi John,

It all depends on what you need it for. A Repeater is quite a "dumb" 
object. All it can do is show stuff from the templates, and repeat the 
items that are bound to it.
It doesn't support (native) editing, paging etc. But what I like about the 
repeater is that it gives me fill controll over each element and their 
attributes, whereas the datalist or dateagrid has a tendency to create or 
remove code that shouldn't be created or removed.

Would you mind sharing your solution with this list?


Imar


At 07:39 AM 5/28/2002 -0700, you wrote:
>Hi Imar,
>
>Thanks for the response.  I actually got this working with a DataList
>before I left for the weekend - so here's the dumb question: can you
>tell me which is better to use, a Repeater or a DataList, and why?
>
>Thanks,
>
>John


Message #5 by "John Tyson" <jtyson@t...> on Tue, 28 May 2002 09:55:50 -0700
Hi Imar,

Thanks for the response.  I'd be glad to share what I've come up with,
this list has been very helpful to my learning asp.net.

First, here is my list of items; the link redirects to a page that calls
a stored procedure to remove the item from the list (I haven't moved any
code to page behind yet as I've just been trying to get it to work ;-)

<%@ Import Namespace=3D"System.Data" %>
<%@ Import Namespace=3D"System.Data.SqlClient" %>
<%@ Import Namespace=3D"System.Web.Security" %>

<script language=3D"VB" runat=3D"server">

   Sub Page_Load(s As Object, e As EventArgs)

      If User.Identity.IsAuthenticated Then

         Dim dbconn As SqlConnection =3D New SqlConnection _
            (ConfigurationSettings.AppSettings("ConnectionString"))

         Dim cmd As SqlCommand =3D New SqlCommand()
         With cmd
            .Connection =3D dbconn
            .CommandText =3D "sp_ClmsToRemove"
            .CommandType =3D CommandType.StoredProcedure
         End With

         Dim dr As SqlDataReader

         Try
            dbconn.Open()
            dr =3D cmd.ExecuteReader(CommandBehavior.CloseConnection)

            lstClaimsToRemove.DataSource =3D dr
            lstClaimsToRemove.DataBind()

         Catch exc As Exception
            Response.Write("An error has occurred: " & exc.ToString())

         Finally
            If Not dr is Nothing Then
               dr.Close()
            End If

         End Try

      Else
         displayCredentials.InnerHtml =3D "You must be an authenticated
user prior to using this service."
      End If

   End Sub

</script>

<body>

<form runat=3D"server">

   <p><div id=3D"displayCredentials" runat=3D"server" /></p>

   <p>
      <asp:DataList id=3D"lstClaimsToRemove" runat=3D"server">

         <HeaderTemplate>
            <table>
               <tr>
                  <td>Company</td>
                  <td>Claim</td>
                  <td>Requested By</td>
                  <td>Date Requested</td>
                  <td>Remove Claim</td>
               </tr>
         </HeaderTemplate>

         <ItemTemplate>
               <tr>
                  <td><%# Container.DataItem(1) %></td>
                  <td><%# Container.DataItem(2) %></td>
                  <td><%# Container.DataItem(3) %></td>
                  <td><%# Container.DataItem(4) %></td>
                  <td><asp:Hyperlink id=3D"HyperLink1" text=3D"Remove 
Claim"
NavigateUrl=3D'<%# "RemoveClaim.aspx?id=3D" & Container.DataItem(0) %>'
runat=3D"server" /></td>
               </tr>   
         </ItemTemplate>

         <FooterTemplate>
            </table>
         </FooterTemplate>

      </asp:DataList>
   </p>

   <p><input type=3D"submit" onServerClick=3D"SignOut_Click" 
Value=3D"Sign
Out" runat=3D"server" /></p>

</form>


And here is my page that calls the stored procedure to remove the item
from the list, and then redirects the user back to the refreshed list.

<script language=3D"VB" runat=3D"server">

   Sub Page_Load(s As Object, e As EventArgs)

      If User.Identity.IsAuthenticated Then

         If Request.Params("id") <> Nothing Then

            Dim dbconn As SqlConnection =3D New SqlConnection _
               (ConfigurationSettings.AppSettings("ConnectionString"))

            Dim cmd As SqlCommand =3D New SqlCommand("sp_RemoveClaim",
dbconn)
               cmd.CommandType =3D CommandType.StoredProcedure

            Dim claimIDParam As SqlParameter =3D
cmd.Parameters.Add("@claimID", SqlDbType.Int)
            claimIDParam.Direction =3D ParameterDirection.Input
            claimIDParam.Value =3D CInt(Request.Params("id"))

            Dim compDateParam As SqlParameter =3D
cmd.Parameters.Add("@completeDate", SqlDbType.DateTime)
            compDateParam.Direction =3D ParameterDirection.Input
            compDateParam.Value =3D CDate(System.DateTime.Now)

            Try
               dbconn.Open()
               cmd.ExecuteNonQuery()

            Catch exc As Exception
               Response.Write("An error has occurred: " &
exc.ToString())

            Finally
               dbconn.Close
               Response.Redirect("ClaimRemReqList.aspx")

            End Try

         Else
            Response.Redirect("ClaimRemReqList.aspx")
         End If

      Else
         FormsAuthentication.SignOut()
         Response.Redirect("../TAIapps_login.aspx")
      End If

   End Sub

</script>


This may not be the best method of accomplishing what I am trying to do,
but it's what I came up with.  I hope it is of some use.

Thanks,

John

-----Original Message-----
From: Imar Spaanjaars [mailto:Imar@S...]
Sent: Tuesday, May 28, 2002 9:43 AM
To: aspx_beginners
Subject: [aspx_beginners] Re: hyperlink menu from datareader

Hi John,

It all depends on what you need it for. A Repeater is quite a "dumb"
object. All it can do is show stuff from the templates, and repeat the
items that are bound to it.
It doesn't support (native) editing, paging etc. But what I like about
the
repeater is that it gives me fill controll over each element and their
attributes, whereas the datalist or dateagrid has a tendency to create
or
remove code that shouldn't be created or removed.

Would you mind sharing your solution with this list?


Imar


At 07:39 AM 5/28/2002 -0700, you wrote:
>Hi Imar,
>
>Thanks for the response.  I actually got this working with a DataList
>before I left for the weekend - so here's the dumb question: can you
>tell me which is better to use, a Repeater or a DataList, and why?
>
>Thanks,
>
>John




  Return to Index