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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

binding to a datalist using dataset 1

Status
Not open for further replies.

rds80

Programmer
Nov 2, 2006
124
US
On report.aspx, there is a dataset (tdsReportRetrieval), which has a table (tableReportRetrieval) that has a column (DisplayName) that I need to bind to a DataList (Sounds like that elementary song).

My questions are:
1) DisplayName is a field that shows the report names and a user can click on that report, which will navigate the user to ShowReport.aspx and open that report up. So do I need to add a hyperlink to the ItemTemplate?

2) What code do I have to add to the html page of report.aspx to have the above working?

3) I added this code in report.aspx.vb to bind the datasource to the dataset. Is it sufficient?

Code:
DataList1.DataSource = tdsReportRetrieval.Tables(0).Columns("DisplayName")
DataList1.DataBind()

An example or a link to a site that does this would be very helpful.

Thanks.
 
3. A couple options
Code:
DataList1.DataSource = tdsReportRetrieval.Tables(0)
DataList1.DataBind()
Code:
  <asp:DataList id=tdsReportRetrieval runat=server>
       <ItemTemplate>
            <a href='ShowReport.aspx?reportID=<%# DataBinder.Eval(Container.DataItem,"ReportID") %>'
            <%# DataBinder.Eval(Container.DataItem,"DisplayName") %></a>
       </ItemTemplate>
  </asp:DataList>

then have the showreport.aspx open the report depending on the querystring passed

or you can use the asp:hyperlink with NavigateURL or you can have a linkbutton and redirect via code in your vb file.

2. See Above
1. See Above
 
Adam,

I tried this:

Code:
<asp:DataList id="DataList1" style="Z-INDEX: 101; LEFT: 360px; POSITION: absolute; TOP: 312px" runat="server" Height="168px" Width="304">
<ItemTemplate>
  <asp:HyperLink id="HyperLink1" runat="server" Width="160px" NavigateUrl= '<%DataBinder.Eval(Container.DataItem, "ReportName"%>' Text = '<%DataBinder.Eval(Container.DataItem, "DisplayName"%>'/>
</ItemTemplate>
</asp:DataList>

But the DataList doesn't show. Is the syntax incorrect?
 
are you binding the datalist? when? do you have data in your query when run? Does your query have the column names specified in the DataBinder html? Are you try catching your binding even to see if its erroring?

looks ok for the html side.
 
Q1. are you binding the datalist?
A1. The datalist is being bound in the vb code behind. the vb code is above.

Q2. do you have data in your query when run?
a2. Yes, have data in the query.

q3. Does your query have the column names specified in the DataBinder html?
a3. Yup the sql query field names match the databinding fields.

q4. Are you try catching your binding even to see if its erroring?
a4. no, that would be done in the vb side?

I successfully was able to bind to a label server control. Meaning the datalist showed the two "DisplayNames" as labels.
 
OVERALL: It is incorrect - took a while to find, as you are right, it wasnt giving error...

NavigateUrl= '<%DataBinder.Eval(Container.DataItem, "ReportName"%>'
Is wrong...

needs to be[n] <%# [/b]DataBinder.Eval...
when accessing databinding event

add the # sign to both binders and you should be fine.

1. had to ask

2. had to ask

3. Always good to check

4. Ive moved on to c#, but heres a previous try catch in vb that ive done with a stored procedure (outputting error to a label called "message")
Code:
    Sub BindNotesGrid(ByVal fid As Integer)
        Dim myConnection As New SqlConnection(conString)
        Dim myCommand As SqlCommand = New SqlCommand("sp_ConfigNotes", myConnection)
        myCommand.CommandType = CommandType.StoredProcedure
        myCommand.Parameters.Add("@page", SqlDbType.VarChar, 20).Value = "config"
        myCommand.Parameters.Add("@cid", SqlDbType.Int, 4).Value = checkRev()
        myCommand.Parameters.Add("@fid", SqlDbType.Int, 4).Value = fid
        Dim da As New SqlDataAdapter(myCommand)
        Dim ds As New DataSet()
        Try
            da.Fill(ds)
            If ds.Tables(0).Rows.Count > 0 Then
                If ViewState("ndone") = "" Then
                    dgNotes.EditItemIndex = -1
                    dgNotes.DataSource = ds.Tables(0)
                    dgNotes.DataBind()
                Else
                    dgNotes.DataSource = ds.Tables(0)
                    dgNotes.DataBind()
                End If
            Else
                dgNotes.Dispose()
                dgNotes.DataBind()
            End If
        Catch ex As Exception
            message.Text = "Config Notes Error: " & ex.Message
        Finally
            myConnection.Close()
        End Try
    End Sub
 
oops...

needs to be <%# DataBinder.Eval...

my tgml was wrong...
Code:
NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "ReportName"%>' Text='<%# DataBinder.Eval(Container.DataItem, "DisplayName"%>'/>
 
AND you are missing the closing ")" after the Column Name

<%# DataBinder.Eval(Container.DataItem, "ReportName")%>
 
Got it to work using:
Code:
<asp:datalist id="DataList1" runat="server" Height="89px" Width="912px" BackColor="#f5f5f5">
   <ItemTemplate>						<asp:HyperLink id=HyperLink1 runat="server" Height="12px" Font-Size="Smaller" ForeColor="Navy" Width="300px" NavigateUrl='<%# "ShowReport.aspx?ReportName=" + DataBinder.Eval(Container.DataItem, "ReportName")%>'			
<%# DataBinder.Eval(Container.DataItem, "DisplayName") %>
        </asp:HyperLink>
   </ItemTemplate>
</asp:datalist>
Learned it from here:
Thanks for your help Adam.
 
How do I access the selected hyperlink that the user clicks from the datalist?

The suggested way to find out the Selected Index is:

dlstAuthors.SelectedIndex = e.Item.ItemIndex

but I read that only works with Button, LinkButton, and ImageButton.

What I need to do is check whether the selected report has criteria in tdsReportRetrieval.Tables(0).Columns("Criteria") and if so direct the user to Criteria.aspx. Otherwise, direct the user to ShowReport.aspx.
 
Use the onItemDataBound event to change the NavigateURL location.
Code:
<asp:DataList id=DataList1 runat=server [b]onItemDataBound=links_IDB[/b]>
<ItemTemplate>
...

Code:
    Sub links_IDB(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            If DataBinder.Eval(e.Item.DataItem, "criteria") = "needed" Then
                CType(e.Item.FindControl("HyperLink1"), HyperLink).NavigateURL = "criteria.aspx"
            Else
                CType(e.Item.FindControl("HyperLink1"), HyperLink).NavigateURL = "ShowReport.aspx?ReportName=" & DataBinder.Eval(e.Item.DataItem, "ReportName").ToString()
            End If
        End If
    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top