asp:repeater items not clearing when new dataSource set - c#

I have a form in which I put one repeater control and bind it on pageLoad,it bind as expected.
I have a dropDownList, when I select a value from it I want to clear the repeater dataSource and rebind it in dropDown_SelectedIndexChanged event handler on codebehind.
I set the repeater datasource as null then rebind it with new datasource it wont reflect the changes, it display the first binded value.
Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
return;
//Populate the drop down
PopulateLocationDropdown();
if (Request.QueryString["locationID"] != null)
{
int locationID=Request.QueryString["locationID"];
//Clear repeater
rpt_Displaytheater.DataSource = null;
rpt_Displaytheater.DataBind();
//Rebind repeater
rpt_Displaytheater.DataSource = GetTheaterDataSet(locationID);
rpt_Displaytheater.DataBind();
}
}
When dropDown selectionChanged :
protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
int locationID=ddlLocation.SelectedValue.ToInt();
//Clear repeater
rpt_Displaytheater.DataSource = null;
rpt_Displaytheater.DataBind();
//Rebind repeater
rpt_Displaytheater.DataSource = GetTheaterDataSet(locationID);
rpt_Displaytheater.DataBind();
}
Edit
here is my dropdown :
<asp:DropDownList ID="ddlLocation" DataTextField="LocationName" DataValueField="Record_Id" runat="server" CssClass="textbox_230"
OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
Here is my repeater :
<asp:Repeater ID="rpt_Displaytheater" runat="server"
OnItemCommand="rpt_Displaytheater_ItemCommand">
<ItemTemplate>
<div class="TheaterListing" style="background: #FFF;">
<div class="TheaterName">
<div class="Theaterhead">
<asp:Label ID="lblTheaterID" runat="server" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem, "Record_Id") %>'></asp:Label>
<asp:LinkButton ID="lnkbtnTheaterName" runat="server" Style="color: #000; font-weight: bold" Text='<%# DataBinder.Eval(Container.DataItem, "Theatre_Name") %>'></asp:LinkButton>
</div>
</div>
<div class="showlist">
<%-- show time repeater-Movies Tab --%>
<asp:Repeater ID="rpt_showtime" runat="server">
<ItemTemplate>
<ul style="padding-left: 10px; margin: -18px 0 0 163px;">
<li class="fl" style="font-weight: bold; padding-left: 8px;">
<asp:LinkButton ID="lnk_showtime" CommandName="Showtime" runat="server" CssClass="txtstyle1"
Text='<%# DataBinder.Eval(Container.DataItem, "Time_From") %>'></asp:LinkButton>
</li>
</ul>
</ItemTemplate>
</asp:Repeater>
<%-- show time repeater-Movies Tab --%>
<%--<asp:LinkButton ID="LinkButton1" runat="server" style="color: #000;font-weight:bold" Text="10.00 AM"></asp:LinkButton>--%>
</div>
</div>
<br clear="all" />
</ItemTemplate>
</asp:Repeater>
My problem is when I reset repeater control's dataSource to new dataSource on dropDown selection changed but the changes is not reflected on the repeater it just show the first bounded values on page load whats wrong with my code

You need to refresh your asp:updatepanel.
This can be achieved by calling yourPanel.Update() or set the UpdateMode of your asp:updatepanel to Always.

Related

How to get item from listview to disable href

How can i get text of each row from Label1?
My .aspx
<asp:ListView ID="lvRestaurant" runat="server" DataSourceID="ldsOrder" DataKeyName="restaurantID">
<LayoutTemplate>
<div>
<asp:PlaceHolder ID="groupPlaceholder" runat="server"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<GroupTemplate>
<div class="restaurant">
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</div>
</GroupTemplate>
<ItemTemplate>
<div><asp:Label ID="Label1" runat="server" Text='<%# Bind("restaurantID") %>' Visible="false"></asp:Label></div>
<div><asp:Image ID="imgRestaurant" runat="server" ImageUrl='<%# Eval("image", "../resImg/{0}") %>' draggable="false" CssClass="restaurant-img"/></div>
<div class="resName"><b><asp:Label ID="lblName" runat="server" Text='<%# Bind("restaurantName") %>'></asp:Label></b></div>
<div class="deliveryFee"><b>MYR<asp:Label ID="lblDeliveryFee" runat="server" Text='<%# Bind("deliverFee") %>'></asp:Label></b> <span style="color:#C3C3C3;">delivery fee</span></div>
</ItemTemplate>
</asp:ListView>
I try to make if cart exist 1 restaurant product, other restaurant will not be able to click.
My .cs
if(Session["BuyItems"]!=null{
string rID = //get from Label1;
if(rID==Session[existID].ToString()){
orderPage.HreF="#";
}
}
Have a look at foreach, maybe this links can help you.
https://www.codeproject.com/Questions/1029041/How-to-loop-through-a-listbox-items-in-asp-net
How to get all labels and its input elements in javascript
https://www.sitepoint.com/community/t/how-to-get-text-from-a-label-in-a-datalist-itemtemplate/6088/18
Pseudocode
if(Session["BuyItems"]!=null
{
string rID = 5; // for example
foreach (var item in YourListWithRestaurants)
{
if(rID != Session[existID].ToString())
{
// Make the restaurants unable to click
}
}
}
You can do this. Add an OnItemCommand with ListView:
OnItemCommand="ListView_ItemCommand" >
Then you can write an eventhandler:
protected void ListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
Label label1 = (Label)e.Item.FindControl("Label1");
}

ASP repeater collecting data from radiobuttons

So I have a Asp repeater that prints out some radiobuttons that a user will use to answer a test. What I need to do is to check which radiobuttons are checked so that I can save them to a XML file.
Here is the code
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="repeaterDiv">
<ul id="list">
<li style="list-style: none">
<p style="font-weight: bold">Fråga nummer: <%# Container.ItemIndex + 1 %></p>
<asp:Label ID="q1_label" runat="server" Text='<%#Eval ("text")%>'></asp:Label>
<asp:CheckBox ID="q1_svar1" Text='<%#Eval("q1")%>' runat="server" CssClass="radioButtons;" />
<asp:CheckBox ID="q1_svar2" Text='<%#Eval("q2")%>' runat="server" CssClass="radioButtons;"/>
<asp:CheckBox ID="q1_svar3" Text='<%#Eval("q3") %>' runat="server" CssClass="radioButtons;"/>
<asp:CheckBox ID="q1_svar4" Text='<%#Eval("q4") %>' runat="server" CssClass="radioButtons;"/>
<hr />
</li>
</ul>
</div>
</ItemTemplate>
</asp:Repeater>
I dont really know how to target the checked ones, what do I do from here? Thanks in advance.
You can use foreach loop in repeater items and them user FindControl method
protected void Button1_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in Repeater1.Items)
{
CheckBox chk = (CheckBox)item.FindControl("q1_svar1");
if (chk.Checked)
{
//Your code ...
}
}
}

How to show or hide a Panel control if a Repeater control's DataSource object contains no data in ASP.NET?

I have a Repeater control that's nested inside a Panel control.
<asp:Panel id="tags" runat="server">
<asp:Repeater ID="rptTags" runat="server" DataSource="<%# this.DataSource %>">
<ItemTemplate>
<span class="tag"><%# Eval("Name") %></span>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
I set the DataSource attribute of the Repeater control like this DataSource="<%# this.DataSource %>" because this code is actually part of a custom Web Control.
I want to show or hide the Panel control depending on whether the DataSource object has any data in it. I had done it like this in the code-behind:
if (rptTags.Items.Count == 0)
{
tags.Visible = false;
}
The thing is I want to do this in the ASPX part, something like:
<asp:Panel id="tags" runat="server" Visible="<%# ((IEnumerable)DataSource).LengthOrSomething %>">
You could do something like :
C#
public void TagsPreRender(object sender, EventArgs e) {
tags.Visible = (((Repeater)sender).Items.Count > 0);
}
ASPX
<asp:Panel id="tags" runat="server" >
<asp:Repeater OnPreRender="TagsPreRender" ID="rptTags" runat="server" DataSource="<%# this.DataSource %>" >
<ItemTemplate>
<span class="tag"><%# Eval("Name") %></span>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>

How to get Footer Item value on code behind with nested Repeater?

I have a nested repeater and I use a textbox in footer template. I wanto to get textbox.text value in button click. Here is my repeater:
<asp:Repeater ID="rprSSFirst" runat="server" OnItemDataBound="rprSSFirst_ItemDataBound" >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
//******Some Items******
<asp:Repeater ID="rprSSNested" runat="server" > //Nested Repeater
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
//******Some Items******
</ItemTemplate>
<FooterTemplate>
<div style=" padding: 20px 35px;" id='ajax'>
<asp:TextBox ID="textbox" TextMode="MultiLine" Columns="50" Rows="10" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server" OnClick="btn_Save_Click" Text="Save" />
</div>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
//In Code Behind
protected void btn_Save_Click(object sender, EventArgs e)
{
TextBox txtAns = (TextBox)rprSSFirst.Controls[rprSSFirst.Controls.Count - 1].FindControl("textbox");
}
But txtAns Value is always null. How to get footer item textbox value in button click? or any other way?
Thanks for your answers.
You have to find the nested RepeaterItem first where both controls are sitting. You can get it by casting the NamingContainer:
protected void btn_Save_Click(object sender, EventArgs e)
{
Button btnSave = (Button) sender;
RepeaterItem item = (RepeaterItem) btnSave.NamingContainer;
TextBox txtAns = (TextBox) item.FindControl("textbox");
}
You can use Commandname property like this for button of nested repeater:
<asp:Repeater ID="rprSSNested" runat="server" OnItemCommand="rprSSNested_ItemCommand" >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
//******Some Items******
</ItemTemplate>
<FooterTemplate>
<div style=" padding: 20px 35px;" id='ajax'>
<asp:TextBox ID="textbox" TextMode="MultiLine" Columns="50" Rows="10" runat="server" ></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Save" CommandName="cmd" CommandArgument="arg"/>
</div>
</FooterTemplate>
</asp:Repeater>
And add event in c# code like this:
protected void rprSSNested_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
if (e.CommandName == "cmd")
{
string ss = ((TextBox)e.Item.FindControl("textbox")).Text;
Response.Write(ss);
}
}
}

Pass an ID to OnClick event with Repeater control?

Here is my screen:
Here is my code for the ItemTemplate in the Repeater:
<ItemTemplate>
<div style="float: left; overflow: hidden; display: inline-block; border-style: solid; margin: 5px; background-color: Silver">
<div style="text-align:center">
<asp:Label ID="lblImage" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "image") %>'></asp:Label>
</div>
<asp:Image runat="server" ID="image1" Width="250px" Height="250px" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "url") %>' />
<div style="text-align: center;">
<asp:Button runat="server" ID="btnNew" Text="New" />
<asp:Button runat="server" ID="btnOriginal" Text="Original" />
</div>
</div>
The Repeater uses a dataset in my program to populate the ItemTemplate's label and image controls. There's another field in my dataset called graphicID. I'd like to, somehow, pass the value in that field to the 'Original' button, so that if a user presses that button, that particular graphicID is passed into the click event. Does this make sense?
For instance, the second image is Captain Harlock. The graphicID for this image is 93. If the user presses the Original button under Captain Harlock, I want to pass 93 to the onClick event. I'm not sure how to do this, though. If someone could point me in the right direction, I'd greatly appreciate it!
<div style="text-align: center;">
<asp:Button runat="server" ID="btnNew" Text="New" />
<asp:Button runat="server" CommandName="cmd_original" CommandArgument="name of field which you want to access" ID="btnOriginal" Text="Original" />
</div>
EDIT
In repeater control
you have to add an event as below:
onitemcommand="Repeater1_ItemCommand"
In code behind
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "cmd_original")
{
Int32 id = Convert.ToInt32(e.CommandArgument);
}
}

Categories