I have a Repeater with a ListView and its Datapager inside.
The information is populated correctly but, the datapager does not refresh the listview when clicking the page numer.
How do I refresh he list view based on datapager selection
<asp:Repeater runat="server" ID="Rptr" DataSourceID="Categories" >
<asp:HiddenField runat="server" id="Hidden" Value='<%# Eval("Category") %.' />
<ItemTemplate>
<asp:DataPager runat="server" ID="DtPgr_Top" PagedControlID="List" PageSize="40">
...
</asp:DataPager>
<asp:ListView runat="server" ID="List" DataSourceID="Items" >
<ItemTemplate>
...
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource runat="server" ID="Items" ConnectionString="<%$ ConnectionStrings:SAP_B1 %>"
SelectCommand=" SELECT Item FROM Items WHERE Category = #Category " >
<SelectParameters>
<asp:ControlParameter ControlID="Hidden" Name="Category" PropertyName="Value" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
" SelectCommand=" SELECT Category FROM Categories " />
enter code here
Solution 1 /
You have to put the listview outside the repeater.
Solution 2 /
Do not determine the datasource of repeater inline. Populate it from codebehind only once and only on load with - if not ispostback - directive.
Related
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.
I have this asp in a formview insert template.
<asp:FormView ID="FormViewReport" runat="server">
<InsertItemTemplate>
GAME_ID:
<asp:DropDownList ID="DropDownListReportIns" runat="server" AutoPostBack="true"
DataSourceID="SqlDataSourceGetGame"
DataTextField="GAME" DataValueField="ID" SelectedValue='<%# Bind("GAME_ID") %>' AppendDataBoundItems="True">
<asp:ListItem Text="ΠΑΡΑΚΑΛΩ ΕΠΙΛΕΞΤΕ ΑΓΩΝΑ" />
</asp:DropDownList>
HOME_PLAYER:
<asp:DropDownList ID="DropDownListRepo" runat="server"
DataSourceID="SqlDataSourceGetPlayers"
DataTextField="HOME_PLAYER_S" DataValueField="HP_ID" SelectedValue='<%# Bind("HOME_PLAYER_ID") %>' >
</asp:DropDownList>
The sql datasource of the second dropdownlist:
<asp:SqlDataSource ID="SqlDataSourceGetPlayers" runat="server" ConnectionString='<%$ ConnectionStrings:BasketballConnectionString1 %>' SelectCommand="SELECT HP.SURNAME blah blah blah WHERE (GAMES.ID = #GAME_ID)">
<SelectParameters>
<asp:ControlParameter ControlID="FormViewReport" PropertyName="SelectedValue" Name="GAME_ID"></asp:ControlParameter>
</SelectParameters>
In the second Dropdownlist the query needs the GAME_ID parameter and this is the DatavalueField of the first dropdownlist.How I can get the selected value from the first dropdownlist and give it to the second dropdownlist?
Put your SqlDatasource control inside the InsertItemTemplate of your FormView (with the DropDownLists), and then change it to this:
<asp:SqlDataSource ID="SqlDataSourceGetPlayers" runat="server" ConnectionString='<%$ ConnectionStrings:BasketballConnectionString1 %>' SelectCommand="SELECT HP.SURNAME blah blah blah WHERE (GAMES.ID = #GAME_ID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownListReportIns" PropertyName="SelectedValue" Name="GAME_ID"></asp:ControlParameter>
</SelectParameters>
</asp:SqlDataSource
That way your ControlParameter is referencing the selected value of the 1st drop down.
Finally I did it using asp for the second dropdownlist:
<asp:DropDownList ID="DropDownListRepIns" runat="server" AppendDataBoundItems="True" EnableViewState="false"
DataSourceID="SqlDataSourceGetPlayers"
DataTextField="HOME_PLAYER_S" DataValueField="HP_ID" >
<asp:ListItem Text="ΠΑΡΑΚΑΛΩ ΕΠΙΛΕΞΤΕ ΑΓΩΝΑ" Value="0" />
</asp:DropDownList><br />
and a c# code for event item inserting that binds manually:
protected void FormViewReport_ItemInserting(object sender, FormViewInsertEventArgs e)
{
e.Values["HOME_PLAYER_ID"] = ((DropDownList)((FormView)sender).FindControl("DropDownListRepIns")).SelectedValue;}
Also the solution this solution works without needing code behind:
SelectedValue='<%# DataBinder.Eval (Container.DataItem, "HOME_PLAYER_ID") %>'
You have to use this in the second dropdownlist in asp.
I have an outer listview and inside its itemtemplate an inner listview. Each has its own SQLDataSource. A Id field from the outerlistview works as a select parameter for the second listview.
When stepping through with the debugger it works perfectly, unfortunately the rendered page only contains data from the outer listview. It appears the databinding of the outer listview fires and renders the page (including the inner listview) before the codebehind can provide a where parameter to the second listview.
The aspx page is below.
<%# Page Title="Nested ListView" Language="VB" MasterPageFile="~/MasterPages/SimpleMasterPage.master"
CodeFile="NLV2PA_A.aspx.vb" Inherits="Demos_NLV2PA_A" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="PlaceHolder1" DataKeyNames="Id"
DataSourceID="SqlDataSource1">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
</LayoutTemplate>
<EmptyDataTemplate>
<span>ListView 1 No data was returned. </span>
</EmptyDataTemplate>
<ItemTemplate>
<span style="">Id:
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
<br />
Name:
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
<br />
<br />
</span>
<asp:ListView ID="ListView2" runat="server" ItemPlaceholderID="PlaceHolder2" DataSourceID="SqlDataSource2">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="PlaceHolder2"></asp:PlaceHolder>
</LayoutTemplate>
<EmptyDataTemplate>
<span>ListView2 No data was returned. </span>
</EmptyDataTemplate>
<ItemTemplate>
<span style="">Title:
<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
<br />
<h1>
<%# Eval("Title") %></h1>
<br />
Summary:
<asp:Label ID="SummaryLabel" runat="server" Text='<%# Eval("Summary") %>' />
<br />
PhotoAlbumID:
<asp:Label ID="PhotoAlbumIdLabel" runat="server" Text='<%# Eval("PhotoAlbumId") %>' />
<br />
<br />
</span>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:RenaissanceConnectionString1 %>"
SelectCommand="SELECT [Id], [Name] FROM [PhotoAlbum] ORDER BY [Id]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:RenaissanceConnectionString1 %>"
SelectCommand="SELECT [Title], [Summary], [PhotoAlbumId] FROM [Apartments] WHERE ([PhotoAlbumId] = #PAId)">
<SelectParameters>
<asp:ControlParameter ControlID="ListView1" DefaultValue="0" Name="PAId" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Content>
The code behind is
Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView1.ItemDataBound
If e.Item.ItemType = ListViewItemType.DataItem Then
SqlDataSource2.SelectParameters("PAId").DefaultValue = DirectCast(e.Item.FindControl("IdLabel"), Label).Text
End If
End Sub
The output to the browser is:
Id: 8
Name: First set of Photos
ListView2 No data was returned.
Id: 9
Name: Second set of Photos
ListView2 No data was returned.
etc.
Microsoft make the point in an old article that:
You could, in theory, intercept the
ItemDataBound event of the parent
ListView, walk your way through the
control tree, grab a reference to the
child ListView, and bind it
programmatically to data. If you do,
you won't throw an exception, but the
binding command on the inner ListView
is lost because it fires too late to
affect the rendering.
Unfortunately, they don't tell me how to fix it.
If there is any genius out there who could tell me how to get this to work it would be much appreciated.
Put your SqlDataSource2 inside the ItemTemplate of ListView1.
for your ControlParameter, set ControlID="IdLabel" and PropertyName="Text"
You wont need any code behind
I am very new to ASP.NET.
I have a an ASP.NET page with an AJAX Combobox and a TextBox. The combobox populates from a database and has value of ID and displays Name. The textbox should display Address. All i want to do is change the value in the Address textbox when the index on the Name combo box changes.
And then be able to change the address and save it back to the database.
How do I do this (simple?) task?
Code so far...
<form id="form1" runat="server">
<div>
<asp:ComboBox ID="ComboBox1" runat="server" DataSourceID="AccessDataSource1" DataTextField="CompositeName"
DataValueField="Id" MaxLength="0" Style="display: inline;"
AutoCompleteMode="SuggestAppend"
onselectedindexchanged="ComboBox1_SelectedIndexChanged">
</asp:ComboBox>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/StudentDB.accdb"
SelectCommand="SELECT [Id], [Name], [Address] FROM [tblStudents]">
</asp:AccessDataSource>
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
Try with the below markup. I've used DropDownList, it could be replaced with AJAX ComboBox.
The DetailsView could be further enhanced with CSS and ItemTemplate.
You could put more fields into the ItemTemplate like City, Country and so on.
<asp:AccessDataSource DataFile="App_Data/Students.accdb" ID="AccessDataSource1"
DataSourceMode='DataSet' SelectCommand='Select ID, [First Name], [Last Name] from Students'
runat='server' >
</asp:AccessDataSource>
<asp:AccessDataSource DataFile="App_Data/Students.accdb" ID="AccessDataSource2"
SelectCommandType="Text" SelectCommand='Select [ID], [Address] from [Students] where ID=#id'
runat='server'>
<SelectParameters>
<asp:ControlParameter ControlID='DropDownList1' Name='id'/>
</SelectParameters>
</asp:AccessDataSource>
<asp:DropDownList ID='DropDownList1' runat='server' AutoPostBack='true' DataSourceID='AccessDataSource1'
DataTextField="First Name" DataValueField='ID'>
</asp:DropDownList>
<asp:DetailsView ID='DetailsView' runat="server" DataSourceID='AccessDataSource2' DataKeyNames='ID'>
<Fields>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID='AddressTextBox' runat='server' Text='<%# Bind("Address") %>'></asp:TextBox>
<asp:Button ID='AddressSaveButton' runat='server' Text='Save' UseSubmitBehavior='true' />
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
In your ComboBox1_SelectedIndexChanged event. Set TextBox1.Text = CurrentAddress variable.
In reality, I am not a big fan of bindings commands directly in the asp.
I would create a Sub that is called LoadMyComboBox() which would fire in the Page_Load() event.
I would assume your going to have a button or some type of event that would be fired to perform your update? TextChanged would be overkill here. Adding an update button and then building your update command in the code behind would handle your update. Once that update is complete you can simply call LoadMyComboBox() again to refresh your combobox to refresh any necessary changes and do whatever you want with your textbox at this time as well.
u can do this by using a dropdownlist itself. In the DropDownList's SelectedIndexChanged event, bind the address to the TextBox control according the dropdownlist selection and pass this as a parameter and save it in database.
Using Visual Web Developer Express 2010 with ASP.NET 4.0.
I have a FormView and I want to set a default value from the database. I can't get it to bind to the value in the database. My FormView looks like this:
<asp:FormView
ID="frmOrderDetails"
DataSourceID="sdsFormOrderDetails"
runat="server"
DataKeyNames="orderId">
<EditItemTemplate>
<h3>Edit Order Details</h3>
<asp:Label ID="lblStrategy" Text="Strategy:" AssociatedControlID="ddlStrategies" runat="server" />
<asp:DropDownList SelectedValue='<%# Bind("strategyId") %>'
ID="ddlStrategies"
runat="server"
DataTextField="strategy"
DataValueField="strategyId"
DataSourceID="sdsStrategies"
/>
<asp:LinkButton
id="lnkUpdate"
Text="Update Order"
CommandName="Update"
Runat="server" />
|
<asp:LinkButton
id="lnkCancel"
Text="Cancel"
CommandName="Cancel"
Runat="server" />
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="sdsFormOrderDetails" runat="server"
ConnectionString="<%$ ConnectionStrings:LocalSQLServer %>"
ProviderName="<%$ ConnectionStrings:LocalSQLServer.ProviderName %>"
SelectCommand="usp_GetOrderDetails" SelectCommandType="StoredProcedure"
UpdateCommand="usp_UpdateOrder" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter Name="orderId" ControlID="grdOrders" PropertyName="SelectedDataKey.Value" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="orderId" ControlID="grdOrders" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sdsStrategies" runat="server"
ConnectionString="<%$ ConnectionStrings:LocalSQLServer %>"
ProviderName="<%$ ConnectionStrings:LocalSQLServer.ProviderName %>"
SelectCommand="usp_GetStrategiesDropDown">
</asp:SqlDataSource>
The EditItemTemplate does not even load when I click the edit button on my FormView control, but I don't get an error message either.
You need to do the following to bind a DropDownList to the EditItemTemplate:
Add the DropDownList and its DataSource to the EditItemTemplate.
Set the DropDownList parameters:
DataSourceID="SqlDataSourceDropDownlist" SelectedValue=<%# Bind("ValueToBind") %>
DataTextField="ValueToDisplay" DataValueField="ValueToBind"
Set the ListView / FormView DataSource <UdateParameters>: <asp:Parameter Name="RankID" Type="Int32" />
you need to use formview Databound event like
protected void frmOrderDetails_DataBound(object sender, EventArgs e)
{
if (frmOrderDetails.CurrentMode == FormViewMode.Edit)
{
DropDownList ddlStrategies = (DropDownList)frmOrderDetails.FindControl("ddlStrategies");
ddlStrategies.SelectedValue = Your DB Value Goes here;
}
}