Skip Navigation LinksHome > Browse by Catagory > Database > Setting Query in Datalist


Setting Query in Datalist

Make Customized Link in FormView
Let's say we have following database table.
10101
Coffe ($3)
20992
Cheese Cake ($6)
28281
Tea ($2)

<asp:DataList ID="DataList1" runat="server" DataKeyField="fldProductID" DataSourceID="sqlProduct">
    <ItemTemplate>
    <P/>
      <asp:Label ID="lblProductID" runat="server" Text='<%#Eval("fldProductID")%>'/>
      <br />
      <asp:Label ID="lblProductName" runat="server" Text='<%#Eval("fldProductName")%>'/>
      (<asp:Label ID="lblProductPrice" runat="server" Text='<%#Eval("fldProductPrice")%>'/>)
    </ItemTemplate>
</asp:DataList>

<asp:SqlDataSource ID="sqlProduct" runat="server"
ConnectionString="<%$ ConnectionStrings:YourDbConnectionString %>"
SelectCommand="YourSelectCommand"/>


You want to make it so the user can click on product name and jump to the product detail page.

On the product detail page (call it = pgePorductDetail.aspx), the data is filtered by the Query name BID. If you want to jump to product detail page for Tea, you need to create a link pgeProductDetail.aspx?BID=28281.

Let's make public function that returns such address when you feed product ID. (in your code behind page)

Function strProductDetailPageByProductID(ByVal intProductID) As String
    Return "pgeProductDetail.aspx?BID=" & intProductID
End Function

Now here is how you can call you function in your datalist.

<asp:Label ID="lblProductName" runat="server"
Text='<%# strProductDetailPageByProductID(DataBinder.Eval(Container.DataItem, "fldProductID"))%>'/>
Tag: Query ASP.NET
By: syantien | 12/30/2007 9:11:35 PM / 12/30/2007 9:40:37 PM

Comment

No comments yet.


Comment

Name
 

Title

Email

Comment
 

Your IP Address is: 38.107.191.84

* to keep comments safe and clean, we display and keep your ip address.