In Classic ASP I was able to display a section break based on state. I have an SQL table with menu items, columns:(ListOrder(int),BeginUrl(varchar(20),ShowDate(varchar(20),EndUrl(varchar(20),Section(varchar(20)) BeginURL,ShowDate,EndUrl concatenate to create the link. Section is state (WA,OR,ID).
In classic I created a var nLevel set to "" then proceed to loop through the RS.
Do While NOT menuRS.EOF
If menuRS.Fields("Section") <> nLevel Then
Response.Write("<br><br>" & menuRS.Fields("Section"))
nLevel = menuRS.Fields("Section")
End If
If menuRS.Fields("Display") = 0 Then
Response.Write("<br>"& menuRS.Fields("BeginUrl")&menuRS.Fields("ShowDate")&menuRS.Fields("EndUrl")) & VbCrLf
End If
menuRS.MoveNext
Loop
Which would give me (roughly)
WA
[date]
[date]
OR
[date]
[date]
ID
[date]
Trying to duplicate that in .Net using a DataList, ItemTemplate, and ASP:Label of course using VB, is giving me fits on the 'section' break.
I have the main menu items looping out. Just failing on the Section Break.
<asp:DataList ID="MenuList1" runat="server" DataSourceID="NwccMenuList">
<ItemTemplate>
<asp:Label ID="SectionBreak" runat="server"
Text='<%# If(Eval("Section") <> Me.nLevel) Then (Eval("Section") Me.nLevel= Eval("Section") End If%>' />
</ItemTemplate>
<ItemTemplate>
<asp:Label ID="BeginUrlLabel" runat="server"
Text='<%# Eval("MenuUrl") %>' />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="NwccMenuList" runat="server"
ConnectionString="<%$ ConnectionStrings:nwcctanningConnectionString %>"
SelectCommand="SELECT ([BeginUrl]+[ShowDate]+[EndUrl]) AS MenuUrl, Section
FROM [Menu] WHERE ([Display] = 0) ORDER BY [Section] DESC, [ListOrder]">
<SelectParameters>
<asp:Parameter DefaultValue="0" Name="Display"
Type="Byte" />
</SelectParameters>
</asp:SqlDataSource>
Not sure the nLevel variable (in codefile) is reachable or not cannot get past the If statement!
I appreciate any pointers or where I went wrong!
Generally, you want to replace one dinosaur code with another ancient code. OK, Let's do it in ASP.NET way. First, asp:DataList is not the best choice because it renders table. Use the simplest databound control like asp:Repeater. This is a sample code which do the work declarativelly.
<asp:Repeater ID="rptMenu" runat="server" DataSource="NwccMenuList">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<FooterTemplate></ul></FooterTemplate>
<ItemTemplate>
<li>
<asp:Literal runat="server" ID="ltrSection" Text='<%#Eval("Section") %>'></asp:Literal>
<asp:Repeater runat="server" DataSource="sqlSubmenu">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<FooterTemplate></ul></FooterTemplate>
<ItemTemplate>
<li>
<asp:HyperLink runat="server" Text='<%#Eval("MenuUrl") %>' NavigateUrl='<%#Eval("MenuUrl") %>'></asp:HyperLink>
</li>
</ItemTemplate>
</asp:Repeater>
<%-- Note that a child datasource is inside parent ItemTemplate --%>
<asp:SqlDataSource ID="sqlSubmenu" runat="server"
ConnectionString="<%$ ConnectionStrings:nwcctanningConnectionString %>"
SelectCommand="SELECT ([BeginUrl]+[ShowDate]+[EndUrl]) AS MenuUrl
FROM [Menu] WHERE ([Display] = 0 and Section=#section) ORDER BY [ListOrder]">
<SelectParameters>
<%-- literal control from parent ItemTemplate --%>
<asp:ControlParameter ControlID="ltrSection" Name="section" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</li>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="NwccMenuList" runat="server"
ConnectionString="<%$ ConnectionStrings:nwcctanningConnectionString %>"
SelectCommand="SELECT distinct Section FROM [Menu] WHERE ([Display] = 0) ORDER BY [Section] DESC">
<SelectParameters>
<asp:Parameter DefaultValue="0" Name="Display"
Type="Byte" />
</SelectParameters>
</asp:SqlDataSource>
Again, this approach is more than 10 year old.
Related
Been using this site for years and have always found it very helpful and normally all my questions get answered by reading the site.
I have some case studies on a site than need to be related to other sections of the site and i am using this code to pull the records from the database :-
<div id="sidebar">
<asp:accessdatasource
id="cskey"
runat="server"
datasourcemode="DataSet"
datafile="_db/db.mdb"
selectcommand="SELECT id FROM markets_case_assign WHERE productid = #catid">
<SelectParameters>
<asp:QueryStringParameter Name="catid" QueryStringField="id" DefaultValue="1" />
</SelectParameters>
</asp:accessdatasource>
<asp:Repeater id="cskeycontent" runat="server" DataSourceID="cskey">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Visible="false" Text='<%#Eval("id")%>'></asp:Label>
<asp:accessdatasource
id="csval"
runat="server"
datasourcemode="DataSet"
datafile="_db/db.mdb"
selectcommand="SELECT id,title,company FROM casestudies WHERE id = #pid">
<SelectParameters>
<asp:controlparameter name="pid" controlid="Label1" DefaultValue="1" />
</SelectParameters>
</asp:accessdatasource>
<asp:Repeater id="csvalcontent" runat="server" DataSourceID="csval">
<ItemTemplate>
<div class="case-studies">
<h2><%#Eval("company")%></h2>
<p><%#Eval("title")%></p>
<p class="no-margin-bottom">Read case study</p>
</div>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</div>
I was just wondering if there is a simpler way of doing it.
Any help would be much appreciated.
Cheers Andy
i was wondering if there was a way of doing it with only one query
To make it one query, just change the SQL call as:
SELECT id,title,company FROM casestudies
WHERE id IN (SELECT id FROM markets_case_assign WHERE productid = #catid)
and use only one repeater (the inner one).
Now you lose the extra advanced to separate them with some extra header, line etc..
This is my dropdown list code........
<td valign="top" align="center">
<asp:DropDownList ID="StudentNameDropDownList" runat="server" Width="150px"
DataSourceID="SqlDataSource2" DataTextField="StudentName"
DataValueField="StudentName" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:dbbilling2.0ConnectionString3 %>"
SelectCommand="SELECT [StudentID], [StudentName] FROM [tblStudentInfo] WHERE ([Class] = #Class)">
<SelectParameters>
<asp:ControlParameter ControlID="ClassDropDownList" Name="Class"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<br />
Now I want to access the Student ID field in my code behind file..How can i achieve this?? What syntax i must use[like dropdownlist.selecteditem] ??
First of all in your DropDownList asp component you must set the property DataValueField="StudentID", then in your code behind you can get the Id of the selected student by writing : StudentNameDropDownList.SelectedValue
For Id You can try this code
StudentNameDropDownList.SelectedValue
I would like to do the following with asp.net and C#:
An SqlDataSource fetches a list of templates from the database. Then, in a repeater, for each template a DropDownList-element is created, that contains the found types for each template. Is this possible?
<asp:SqlDataSource ID="src_template" runat="server" ConnectionString="<%$ ConnectionStrings:VConStr %>" SelectCommand="SELECT [template] FROM [template]" />
<asp:Repeater ID="rpt_template" runat="server" DataSourceID="src_template" OnItemCommand="rpt_template_ItemCommand">
<ItemTemplate>
<p>
<asp:SqlDataSource ID="src_types" runat="server" ConnectionString="<%$ ConnectionStrings:VConStr %>" SelectCommand='SELECT [type] FROM [templatetype] WHERE [template] = <%# Eval("template") %>' />
<label for="DropDownList1"><%# Eval("template") %></label>
<asp:DropDownList ID="DropDownList1" runat="server" DatasourceID="src_types" DataTextField="type" DataValueField="type" />
</p>
</ItemTemplate>
</asp:Repeater>
How do I fill the where-part correctly?
Try something like this:
<ItemTemplate>
<asp:HiddenField ID="template" runat="server"
Value='<%# Eval("template") %>'
/>
<asp:SqlDataSource ID="src_types" runat="server"
ConnectionString="<%$ ConnectionStrings:VConStr %>"
SelectCommand="SELECT [type] FROM [templatetype] WHERE [template] = #template"
>
<SelectParameters>
<asp:ControlParameter
Name="template"
Type="String"
ControlID="template"
PropertyName="Value"
/>
</SelectParameters>
</asp:SqlDataSource>
...
</ItemTemplate>
I have two Formviews on the same page. I can use FindControl on the first without issue, but it always fails on the second one.
I am using the ItemTemplate for both, both default to ReadOnly, both are bound to SQLDataSources (different ones) but I cannot for the life of me work out why FindControl works for one and not the other.
I have removed a lot of the plain text from the code below.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SoftSaleDBConnectionString %>"
SelectCommand="SELECT * FROM [Apps] WHERE ([AppID] = #AppID)" >
<SelectParameters>
<asp:FormParameter FormField="AppID" Name="AppID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="AppID"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<h1>
<asp:Label ID="AppNameLabel" runat="server" Text='<%# Bind("AppName") %>' />
<asp:Label ID="VersionLabel" runat="server" Text='<%# Bind("Version") %>' /> for
<asp:Label ID="OSLabel" runat="server" Text='<%# Bind("OS") %>' />
</h1>
<p>Text here</p>
<p><asp:TextBox ID="LicenseTextBox" runat="server"
Text='<%# Eval("License")%>'
TextMode="MultiLine" Width="800px" Rows="25" ReadOnly="True"></asp:TextBox></p>
<asp:HiddenField ID="AppLocation" runat="server" ViewStateMode="Inherit" Value='<%# Bind("AppLocation") %>'/>
</ItemTemplate>
</asp:FormView>
<p><asp:Literal ID="SizeLiteral" runat="server"></asp:Literal></p>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:SoftSaleDBConnectionString %>"
SelectCommand="SELECT * FROM [Installations] WHERE (([AppID] = #AppID) AND ([Username] = #Username))" >
<SelectParameters>
<asp:FormParameter FormField="AppID" Name="AppID" Type="Int32" />
<asp:FormParameter FormField="Username" Name="Username" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:FormView ID="DLFormView" runat="server" DataSourceID="SqlDataSource4"
DataKeyNames="AppID,Username">
<ItemTemplate>
<p> <asp:Label ID="DLAppNameLabel" runat="server" />
<br />
<br />
<asp:Label ID="NumberOfInstallations" runat="server" Text='<%# Bind("Installations") %>'></asp:Label>
<br />
<asp:HiddenField ID="TotalInstallations" runat="server" />
Number of New Installations:
<asp:DropDownList ID="NumberOfNewInstallations" runat="server">
</asp:DropDownList>
<br />
<asp:Label ID="TotalNumberOfInstallations" runat="server" Text="Label"></asp:Label>
</p>
</ItemTemplate>
</asp:FormView>
And the FindControl coding is as follows...
TextBox LicTextBox = (TextBox)FormView1.FindControl("LicenseTextBox");
HiddenField AppLocCode = (HiddenField)FormView1.FindControl("AppLocation");
Label AppNameCode = (Label)FormView1.FindControl("AppNameLabel");
These always work...
Label DLAppNameCode = (Label)DLFormView.FindControl("DLAppNameLabel");
This always returns null. I've read a million bosts about the controls not being rendered until databinding has completed, but surely if the two FOrmViews are set up the same way, the result should be the same.
Any help would be much apreciated :)
Matt :)
I'd like to plead stupidity and but also provide some background to how I've used what I've learnt from my stupidity.
My SqlDataSource for DLFormView was returning records, of course I then used slightly different values by accident and so no records were returned. I only figured this out by making the ItemTemplate section of the form view only contain unbound (basically plain text) data. As nothing displayed, this pointed towards the page using a different tempalte. When I put a test line in the EmptyDataTemplate, it displayed, confirming there was nothing returned by the SqlDataSource. Perhaps a School boy error not putting something in this template during development.
As it happens I need to use the EmptyDataTemplate anyway, I just hadn't got to this point. As only one template is rendered at a time, I have been able to use the same ID names in both templates. This means there is no need to test for which template is being used. (although empty template is used when DataItemsCount = 0, otherwise you can test for CurrentMode for the remaining templates.)
So a big fail on my part, but I hope people can learn from my mistake.
Matt :)
I have an ASP.NET GridView in a Web Form. This GridView using a SqlDataSource and utilizes paging and searching. For the sake of reference, here is the trimmed down version of it:
<asp:SqlDataSource ID="myDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:DB %>"
SelectCommand="SELECT p.[ID], p.[Name], p.[Email], l.[State], l.[City] FROM [Person] p, [Location] l WHERE p.[LocationID]=l.[ID] ORDER BY p.[Name]"
UpdateCommand="UPDATE [Person] SET [Email] = #Email WHERE [ID] = #ID"
/>
<asp:GridView ID="myGridView" runat="server" DataSourceID="myDataSource"
AllowPaging="true" AllowSorting="true" PageSize="25" AutoGenerateEditButton="true"
DataKeyNames="ID">
<Columns>
<asp:BoundField DataField="ID" Visible="false" />
<asp:TemplateField HeaderText="Person" SortExpression="Name">
<ItemTemplate>
<asp:Literal ID="nameLit" runat="server" Text='<%# Bind("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Email" SortExpression="Email" HeaderText="Email Address" />
<asp:TemplateField HeaderText="Location" SortExpression="City">
<ItemTemplate>
<asp:Literal ID="cityLit" runat="server" Text='<%# Bind("City") %>' />
<asp:Literal ID="stateLit" runat="server" Text='<%# Bind("State") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The data source has a lot of records. At least 25,000. Because of this, I want to allow the user to filter by state. I want to provide a drop down list with a list of states. When a user changes the drop down list, the results in the grid view will be filtered by the selected state. However, I do not know how to do this with a SqlDataSource. Can someone please explain to me how to accomplish this?
Thank you!
First of all, let's add the DropDownList, and a SqlDataSource to populate it:
<asp:dropdownlist runat="server" id="StateDropDownList" DataSourceId="StateDataSource" DataTextField="State" DataValueField="State" />
<asp:sqldatasource runat="server" ConnectionString="<%$ ConnectionStrings:DB %>" SelectCommand="SELECT DISTINCT State FROM Location" />
Then we change your existing data source to use a State parameter and read the parameter's value from the DropDownList:
<asp:SqlDataSource ID="myDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:DB %>"
SelectCommand="SELECT p.[ID], p.[Name], p.[Email], l.[State], l.[City] FROM
[Person] p INNER JOIN [Location] l ON p.[LocationID]=l.[ID]
WHERE (l.State = #State) ORDER BY p.[Name]"
UpdateCommand="UPDATE [Person] SET [Email] = #Email WHERE [ID] = #ID"
<SelectParameters>
<asp:ControlParameter ControlID="StateDropDownList" Name="State"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
/>
That should be all you need.
BTW, top tip: I changed your query slightly to use INNER JOIN syntax rather than doing the table join in a WHERE clause. There's some great info here on INNER JOIN.
Top tip #2: Paging in the GridView is a bit naive - every time you change pages, the GridView is reading all 25 000 records in your table, then throwing away the ones it doesn't need. There's a great article here (and plenty of questions on SO!) on doing custom paging with a Gridview.