Query string not sending values in HeaderTemplate - asp.net

I am using Itemtemplate and headertemplate inside datalist. But in case of headertemplate it is not sending query string value to other page
following is the code of datalist for brandwise.aspx
<asp:DataList ID="DataList2" runat="server" DataSourceID="SqlDataSource2" >
<HeaderTemplate>
<a href='pc1.aspx?<%# Eval("categoryId","cid={0}") %>' style="text-decoration:none">
ALL BRANDS
</a>
</HeaderTemplate>
<ItemTemplate>
<ul>
<li>
<a href='brandwise.aspx?<%# Eval("categoryId","cid={0}") %>&<%# Eval("brandId","bid={0}") %>' style="text-decoration:none">
<%# Eval("brandName", "{0}") %>
</a>
</li>
</ul>
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:DBCS %>" SelectCommand="select distinct b.brandName,p.categoryId,b.brandId from Brand1 b, Category c, Product1 p where b.brandId = p.brandId and p.categoryId = c.categoryId and p.categoryId = #cid;">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="" Name="cid" QueryStringField="cid" />
</SelectParameters>
</asp:SqlDataSource>
The problem is in following header template
<HeaderTemplate>
<a href='pc1.aspx?<%# Eval("categoryId","cid={0}") %>' style="text-decoration:none">
ALL BRANDS
</a>
</HeaderTemplate>
because when it redirect to pc1.aspx in the browser window it displays
http://localhost:3492/pc1.aspx?
Which means it does not take into account query string value and categoryId is the name of the column.
I am basically making an e-commerce website for my project. In this code I am trying to display the brands for a given category as in amazon. For e.g the category maybe "mobile" and brands may be samsung,apple,nokia. So if user clicks on a particular brand it displays mobiles for that brand onlys. AND i also need to include one "ALLBRANDS" option clicking on which displays all the brands. Is there any why i can do so???
Plz dont try to run it. It wont.

HeaderTemplate is common for all elements and is rendered only once for DataList control, it's not tight to data source so I'm afraid your code doesn't make sense.
If you need content of HeaderTemplate be item data related, then you need to put it on ItemTemplate

Related

Cannot change SelectCommand contents dynamically

I am having an issue trying to change SelectCommand contents dynamically.
There is this Telerik searchbox control that uses SQLDataSource to constantly bang the DB with a select query and show up a list of words filtered by your typing. Then if you pick an entry from the list it will become a "token" and then you can start typing again.
In my case, the query should return a list of car makes and the initial query is:
SELECT DISTINCT mfrname
FROM Manufacturers
WHERE mfrname IS NOT NULL
ORDER BY mfrname
So if I type "Che" it will show up "Checker" and "Chevrolet". So if I click "Chevrolet" it will become a token and my typing is reset so I can start typing again.
For tests purposes after the token is generated I slighted changed my query to:
SELECT DISTINCT mfrname
FROM manufacturers
WHERE mfrname IS NOT NULL and mfrname like 'm%'
ORDER BY mfrname
Notice that now it should only select the car makes started with an "M" like "Mercedes", "Maseratti", etc, so if I type anything else that doesn't starts with an "M" it shouldn't show anything.
However if I type "Che" there it is "Checker" and "Chevrolet" again, making it clear that it's still using the initial query and not the new one.
OKay, I know that the event IS being triggered and the SelectCommand value IS being changed (I know this because I added a label that changes and shows up the new value). What I don't know is why the control insists on keep using the old query and not the new one! Any idea?
My code front:
<form id="form1" runat="server">
<telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
<telerik:RadAutoCompleteBox RenderMode="Lightweight" runat="server" ID="RadAutoCompleteBox1" autopostback="true" EmptyMessage="Type in car make..."
DataSourceID="SqlDataSource1" DataTextField="mfrname" InputType="Token" Width="450" DropDownWidth="150px" OnEntryAdded="RadAutoCompleteBox1_EntryAdded" >
</telerik:RadAutoCompleteBox>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" CancelSelectOnNullParameter="false" ConnectionString="<%$ ConnectionStrings:MyConn %>" SelectCommand="SELECT DISTINCT mfrname FROM Manufacturers WHERE mfrname IS NOT NULL ORDER BY mfrname">
</asp:SqlDataSource>
<div>
<br />
<asp:label id="label1" runat="server">Waiting for update...</asp:label>
</div>
</form>
My code behind:
Protected Sub RadAutoCompleteBox1_EntryAdded(sender As Object, e As Telerik.Web.UI.AutoCompleteEntryEventArgs)
SqlDataSource1.SelectCommand = "SELECT DISTINCT mfrname FROM manufacturers WHERE mfrname IS NOT NULL and mfrname like 'm%' ORDER BY mfrname"
RadAutoCompleteBox1.DataBind()
label1.Text = e.Entry.Text + " was added. (" + SqlDataSource1.SelectCommand + ")"
End Sub
I found the solution. Well, more like a work around but it works.
In my case I didn't need to change entirely the query, but actually only the WHERE clause. So I added a dynamic parameter to the query text and tied it to another control (an invisible label) forcing the AutoCompleteBox to always look into the contents of the label before to run the query.
This way, changing the contents of the label will change the parameter used in my WHERE clause and so I can control the query. It may be expanded by simply adding more parameters to the SQLDataSource and adding additional invisible controls to the page.
So here it goes the implemented solution since it may be of help for someone else...
Code front:
<form id="form1" runat="server">
<telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
<telerik:RadAutoCompleteBox RenderMode="Lightweight" runat="server" ID="RadAutoCompleteBox1" autopostback="true" EmptyMessage="Type in car make..." DataSourceID="SqlDataSource1" DataTextField="mfrname" InputType="Token" Width="450" DropDownWidth="150px" OnEntryAdded="RadAutoCompleteBox1_EntryAdded" >
</telerik:RadAutoCompleteBox>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" CancelSelectOnNullParameter="false" ConnectionString="<%$ ConnectionStrings:MyConn %>" SelectCommand="SELECT DISTINCT mfrname FROM Manufacturers WHERE mfrname like #mypar and mfrname IS NOT NULL ORDER BY mfrname">
<SelectParameters>
<asp:ControlParameter ControlID="lblSQLpar" Name="mypar" PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
<div>
<!-- initial value -->
<asp:label id="lblSQLpar" runat="server" Visible="false">%</asp:label>
</div>
</form>
code behind:
Protected Sub RadAutoCompleteBox1_EntryAdded(sender As Object, e As Telerik.Web.UI.AutoCompleteEntryEventArgs)
'Use CASE structure to change the parameter accordingly to your needs
lblSQLpar.Text = "m%"
End Sub

Display Nested ListViews (Menus to Submenus) with AccessDataSource

Good Days,
By using ASP.NET and AccessDataSource, I need to display a listview inside a parent listview to be able to have menus and their submenus. Current state of my code is:
<asp:AccessDataSource ID="adsMenus" runat="server" DataFile="~/App_Data/menus.accdb" SelectCommand="SELECT MenuID, Menu FROM Menus" />
<asp:ListView ID="lvwMenus" runat="server" DataSourceID="adsMenus" DataKeyNames="MenuID">
<ItemTemplate>
<h2><%#Eval("Menu")%></h2>
<asp:AccessDataSource ID="adsSubmenus" runat="server" DataFile="~/App_Data/menus.accdb" SelectCommand="SELECT Submenu FROM Submenus WHERE MenuID = <%#Eval('MenuID')%>" />
<asp:ListView ID="lvwSubmenus" runat="server" DataSourceID="adsSubmenus">
<ItemTemplate>
<h3><%#Eval("Submenu")%></h3>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
When I debug and run the page, it gives a syntax error. I am in the phase of learning ASP.NET with C# and I believe this should be an easy fix but somehow I couldn't find a simple solution after reading several blogs and posts.
Thank you for your help and time!
Attribute that has a value formed from literals and evaluated fields should be, as a rule, constructued wholly in the <%# %>:
SelectCommand='<%# string.Format("SELECT Submenu FROM Submenus WHERE MenuID = {0}", Eval("MenuID")) %>'

How can use Repeater control for Image slider control?

I want fetch some images from database and show that images through Image slider.
For that purpose I want to use repeater control.
Please can you say what is the easy way to make use of repeater for Image slider.
Here is a way tom implement the repeater for your slider :
<!--Your repeater with the <img /> tags for your slider (can be <li><img /></li> or other pattern)-->
<asp:Repeater ID="RptImages" runat="server" DataSourceID="SqlDataSourceMention">
<ItemTemplate>
<img src='<%# Eval("ImgPath") %>' alt="image-slider" />
</ItemTemplate>
</asp:Repeater>
<!--Your data source using stored procedure for this sample-->
<!--The "ConnectionString" attribute is set in your web.config file-->
<asp:SqlDataSource ID="SqlDataSourceImages" runat="server" ConnectionString="<%$ ConnectionStrings:YourconnectionString %>"
SelectCommand="YourStoredProcedureName" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
This will output a list of tags with the src attribute bound on the "ImgPath" column of your stored procedure result.

Passing Value Through Link Button in ASP.NET

Conditions :
I have two tables,
category(id_kat,name_kat)
book(id_book,id_kat,name_book)
id_kat has a child and parent relations.
I'm a little bit confused using ASP.NET, especially when passing value between pages.
Now, i'm showing the category's datas with
<asp:listview id="listview1" runat="server" datakeynames"id_kat" datasourceID="sdskat">
//FYI sdskat is datasource for category table
<itemtemplate>
<li>
<asp:linkbutton id="linking" runat="server" ><%# Eval("name_kat") %></asp:linkbutton>
</li>
</itemtemplate>
</asp:listview>
The problem is, i wanted to pass "id_kat" value when i click the hyperlink. (but redirected to self page).
I want it as a query string (or another way if able).
Thanks a lot.
If you're linking to the same page with different query string parameters, you only need to include ? and after.
<%# Eval("name_kat") %>
You could use <asp:hyperlink id="linking" runat="server" ><%# Eval("name_kat") %></asp:hyperlink> instead. You can then add your url in the navigateURL attribute and append your id_kat on there.
Something like this should work:
<asp:hyperlink id="linking" runat="server" navigateURL="~/page.aspx?id_kat=<%# Eval('id_kat') %>" ><%# Eval("name_kat") %></asp:hyperlink>
the <asp:hyperlink> tag is essentially the tag but using a server side control.
If you have to use a linkbutton then in your code behind you can append the id_kat to the querystring and then response.redirect(url) to the new page.
Why not use a regular anchor?
<a href='foo.aspx?id=<%# Eval("id_kat") %>'><%# Eval("name_kat") %></a>

How to use value from primary accessdatasource control as parameter in select query for secondary accessdatasource control

I'm trying to display all orders placed and I have a primary accessdatasource control that has a select query to get the customer information and the orderID. I want to use the orderID value from this first query as a parameter for the secondary accessdatasource control that selects the product information of the products in the order. In plain english, I want to:- select product info from product table where orderID = ?
(where ? is the orderID value from the first query)
I have tried the <%#Eval("OrderID")%> but I get a "server tag not well formed" error, but I do get results returned when I just type the order ID in, but obviously every result (order) just contains the same product info...
<asp:Repeater ID="Repeater1" runat="server"
DataSourceID="AccessDataSource1">
<ItemTemplate>
<asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="~/App_Data/project.mdb" SelectCommand="SELECT orderDetails.OrderID, album.Artist, album.Album, album.Cost, album.ImageURL, orderDetails.Quantity, orderDetails.Total FROM (album INNER JOIN orderDetails ON album.AlbumID = orderDetails.AlbumID) WHERE (orderDetails.OrderID = ? )">
<SelectParameters>
// Error is on this line
<asp:Parameter Name="OrderID" DefaultValue="<%#Eval ("OrderID")%>" />
</SelectParameters>
</asp:AccessDataSource>
<div class="viewAllOrdersOrderArea">
<div class="viewAllOrdersOrderSummary">
<p><b>Order ID: </b><%#Eval("OrderID")%></p>
<h4>Shipping Details</h4>
<p><b>Shipping Address: </b><%#Eval("ShippingName")%>, <%#Eval("ShippingAddress")%>, <%#Eval("ShippingTown")%>, <%#Eval("ShippingPostcode")%></p>
<h4>Payment Details</h4>
<p><b>Cardholder's Address: </b><%#Eval("CardHolder")%>, <%#Eval("BillingAddress")%>, <%#Eval("BillingTown")%>, <%#Eval("BillingPostcode")%></p>
<p><b>Payment Method: </b><%#Eval("CardType")%></p>
<p><b>Card Number: </b><%#Eval("CardNumber")%></p>
<p><b>Start Date: </b><%#Eval("StartDate")%>, Expiry Date: <%#Eval("ExpiryDate")%></p>
<p><b>Security Digits: </b><%#Eval("SecurityDigits")%></p>
<h4>Ordered items:</h4>
<asp:Repeater ID="Repeater2" runat="server"
DataSourceID="AccessDataSource2">
<ItemTemplate>
<div style="display: block; float: left;">
<div class="viewAllOrdersProductImage">
<img width="70px" height="70px" alt="<%# Eval("Artist") %> - <%# Eval("Album") %>" src="assets/images/thumbs/<%# Eval("ImageURL") %>" />
</div>
<div style="display:block; float:left; padding-top:15px; padding-right:20px;"><p><b><%# Eval("Artist") %> - <%# Eval("Album") %></b></p>
<p>£<%# Eval("Cost") %> x <%# Eval("Quantity") %> = £<%#Eval("Total")%></p></div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
Inside of the Repeater ItemTemplate, you can create a HiddenField control and set its Value to <%#Eval("OrderID")%>. Then add a control parameter for this hidden field to the Select Parameter collection of AccessDataSource2.
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("OrderID") %>' />
<asp:AccessDataSource ID="AccessDataSource2" runat="server">
<SelectParameters>
<asp:ControlParameter ControlID="HiddenField1" PropertyName="Value" Type="Int32" />
</SelectParameters>
</asp:AccessDataSource>
Also, I think the error that you were getting may be because you used double quotes twice. Try single quotes around the server tags for the Eval statement, like I have above.

Resources