I am using Repeater control and in it I have one Image tag having id as 'imgGallery'. but in code behind file, I cannot access it.
aspx
<div class="col-md-4 col-sm-12">
<div class="col-md-2">
<asp:Repeater ID="rptImage" runat="server">
<ItemTemplate>
<asp:Image ID="imgGallery" runat="server" />
</ItemTemplate>
</asp:Repeater>
</div>
</div>
aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
using (DBDataContext dt = new DBDataContext())
{
var frm = from i in dt.GalleryMasters
where i.CategoryId == 1
select i;
foreach (var item in frm)
{
Image img = (Image)rptImage.FindControl("imgGallery");
img.ImageUrl = item.Image;
((HtmlControl)(FindControl("ifrm"))).Attributes["src"] = item.Video;
}
}
}
If I use FindControl then var is null.
First you need to bind your repeater:
rptImage.DataSource = frm.ToList();
rptImage.DataBind();
In your view should be possible to do this:
<asp:Image ID="imgGallery" runat="server" ImageUrl='<%# Eval("Video") %>' />
In code behind, to get items in your repeater, you need to iterate throught his items. The single item contains your image control. This should work:
foreach (RepeaterItem item in rptImage.Items) {
Image img = (Image)item.FindControl("imgGallery");
// Some other code
}
If you want to bind image in Repeater control based on Database result then you can directly assign datasource to Repeater control instead of finding image control.
Also Repeater does not have any record then it will not find any control which is inside the repeater as there is not any row in repeater.
Related
I have used repeater in asp.net
<div class="slider-inner">
<div id="daslider" runat="server" class="da-slider">
<asp:Repeater ID="rptSlider" runat="server">
<ItemTemplate>
<asp:Panel ID="sld" runat="server" class="da-slide">
<h2><asp:Literal ID="lblTitle" runat="server"></asp:Literal></h2>
<p>
<asp:Literal ID="lblDescription" runat="server"></asp:Literal>
</p>
<div class="da-img">
<iframe id="framevid" runat="server" visible="false" width="530" height="300" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
<asp:Image ID="sldrimg" runat="server" CssClass="img-responsive"/>
</div>
</asp:Panel>
</ItemTemplate>
<FooterTemplate>
<asp:Panel ID="btnlinks" runat="server" class="da-arrows">
<span class="da-arrows-prev"></span>
<span class="da-arrows-next"></span>
</asp:Panel>
</FooterTemplate>
</asp:Repeater>
</div>
</div>
In CS File I want to bind them programically using loop as below lines of code
private void GetMainAppSettings()
{
MainSetting Item = context.FetchMainAppSettings();
SliderContext contextSlider = new SliderContext();
Slider SW = new Slider();
string PageName = "Home Page";
IEnumerable<_14Muslims.Domain.Entity.Slider> pType = contextSlider.SliderFetchAllEnabled(PageName);
foreach (Slider item in pType)
{
lblTitle.Text = item.SliderTitle;
lblDescription.Text = item.SliderDescription;
framevid.Attributes.Add("src", item.SliderImage);
sldr.Attributes.Add("src", item.SliderImage);
daslider.Style.Add("background-image", WebUtility.UrlSchemeAuthority() + #"/FileStore/AppSettingsSiteLogos/" + item.BackgroundImage);
}
}
Note that GetMainAppSettings() is called on page_load event
Please Help me !!!
There are two separate things that you need to do:
Set the source of the repeater
Tell the repeater what to do for each item in the source.
To achieve the first, you just need to set the DataSource property of the repeater to the collection of items you need displayed, and execute a DataBind call:
private void GetMainAppSettings()
{
MainSetting Item = context.FetchMainAppSettings();
SliderContext contextSlider = new SliderContext();
Slider SW = new Slider();
string PageName = "Home Page";
IEnumerable<_14Muslims.Domain.Entity.Slider> pType = contextSlider.SliderFetchAllEnabled(PageName);
rptSlider.DataSource(pType);
rptSlider.DataBind();
}
When this is done, the repeater will loop through each item, process it and, display whatever is needed. To customize this process, the repeated provides an ItemDataBound event where you can set how the template should look for a specific item:
protected void rptSlider_ItemDataBound(object sender, RepeaterItemEventArgs e) {
// This event is raised for the header, the footer, separators, and items.
// Execute the following logic for Items and Alternating Items.
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
//get the item from the event arguments
var item = (Slider)e.Item.DataItem;
//get the controls
var lblTitle = (Label)e.Item.FindControl("lblTitle");
var lblDescription= (Label)e.Item.FindControl("lblDescription");
var framevid= (HtmlGenericControl)e.Item.FindControl("framevid");
var sldr= (HtmlGenericControl)e.Item.FindControl("sldr");
//set the values
lblTitle.Text = item.SliderTitle;
lblDescription.Text = item.SliderDescription;
framevid.Attributes.Add("src", item.SliderImage);
sldr.Attributes.Add("src", item.SliderImage);
}
}
This will execute once for each item in the data source, and you have complete control over what goes where and how. The looping is done implicitly for you by the repeater itself.
No need to loop the data in your code behind, you can directly assign the DataSource and Repeater control will take care of rest.
In Code behind, you can programatically set the DataSource like this:-
rptSlider.DataSource = pType;
rptSlider.DataBind();
In your repeater control, you can put the Data Binder code nuggets to assign particular properties to control like this:-
<h2><asp:Literal ID="lblTitle" runat="server" Text='<%# SliderTitle%>'></asp:Literal></h2>
and so on..for other controls.
Why use loop for bind repeater? You can directly assign your object "pType" to repeater data source. Like
IEnumerable<_14Muslims.Domain.Entity.Slider> pType = contextSlider.SliderFetchAllEnabled(PageName);
rptSlider.DataSource=pType;
rptSlider.DataBind();
After you can access all your field in repeater on .aspx page.
More Details see below article:
http://www.c-sharpcorner.com/UploadFile/5089e0/how-to-use-repeater-control-in-Asp-Net/
I am trying to get the text of my label which is inside a repeater, but I keep getting a NullPointerException.
All of the data is coming from database and it is coming correctly.
When I click on the LinkButton, I want to use the Label Text for next bit code.
Aspx page:
<asp:Repeater ID="RepeaterDepartmentParent" runat="server">
<ItemTemplate>
<div id="outerDiv" class="col-lg-3 col-xs-6" runat="server">
<!-- small box -->
<div>
<div class="inner">
<p>
<%# DataBinder.Eval(Container.DataItem, "Department_Namestr")%>
</p>
</div>
<asp:Label ID="lblDepartmentId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Department_Idint")%>' Visible="true"></asp:Label>
<asp:LinkButton ID="linkChildDepartment" CommandName="Click" runat="server" CssClass="small-box-footer" OnClick="linkChildDepartment_Click">More info<i class="fa fa-arrow-circle-right"></i></asp:LinkButton>
</div>
</div><%--<%-- ./col -->--%>
</ItemTemplate>
</asp:Repeater>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
DataSet dsParentDepartment = null;
dsParentDepartment = objDepartmentBL.viewDepartmentparent();
RepeaterDepartmentParent.DataSource = dsParentDepartment.Tables[0];
RepeaterDepartmentParent.DataBind();
}
protected void linkChildDepartment_Click(Object sender, EventArgs e)
{
//what to write here??
//i have tried the bellow code but it gives me every data in that loop but i
//want the single data for a link button click.
//foreach (RepeaterItem item in RepeaterDepartmentParent.Items)
// {
// Label myLabel = (Label)item.FindControl("lblDepartmentId");
// myLabel.Text = Id;
//}
//edited code that works properly
LinkButton linkChildDepartment = (LinkButton)sender;
RepeaterItem item = (RepeaterItem)linkChildDepartment.NamingContainer;
Label myLabel = (Label)item.FindControl("lblDepartmentId");
}
How can I correctly reference the Link Button Label Text?
You can use the NamingContainer property to get the reference of the RepeaterItem. From there it's a short way to your label:
protected void linkChildDepartment_Click(Object sender, EventArgs e)
{
LinkButton linkChildDepartment = (LinkButton) sender;
RepeaterItem item = (RepeaterItem) linkChildDepartment.NamingContainer;
Label myLabel = (Label)item.FindControl("lblDepartmentId");
// ...
}
Binding data to checkbox list inside a repeater controls ItemDataBound event.
Here is Aspx code:
<asp:Repeater ID="rptrSelectedRcds" runat="server">
<ItemTemplate>
<div id="groupsDiv" runat="server">
<div class="hidden" id="divGroupSelector" runat="server">
<asp:CheckBoxList runat="server" ID="chkLstGroups"
RepeatDirection="Vertical">
</asp:CheckBoxList>
</div>
<asp:Button ID="btnConfirmGroups" runat="server" Text="Confirm" />
</div>
</ItemTemplate>
</asp:Repeater>
Code used inside ItemDataBound to bind data to repeater control:
protected void rptrSelectedRcds_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
Int32 iProviderID = Convert.ToInt32(((Label)e.Item.FindControl("lblIndividualID")).Text);
HtmlGenericControl selectGroupsDiv = (HtmlGenericControl)e.Item.FindControl("divGroupSelector");
if (divGroupSelector != null)
{
CheckBoxList chkLstGroups = (CheckBoxList)e.Item.FindControl("chkLstGroups");
chkLstGroups.DataSource = GetStateGroupsByProvider(stateValue, iProviderID);
//This code line gives different values for each record
chkLstGroups.DataTextField = "Name";
chkLstGroups.DataValueField = "pkID";
chkLstGroups.DataBind();
}
}
I can see in firebug the ids for chkLstGroups are different, but values bind to it are same.I want different values for each different checkbox id generated as from method - GetStateGroupsByProvider
Please help me out..!
Thanks.!
I want to use FindControl to find the value of the HiddenField i.e hfBlogID. I want to find the value on a ButtonClick
<asp:ListView ID="lvArticle" runat="server">
<LayoutTemplate>
<div runat="server" id="itemPlaceHolder">
</div>
</LayoutTemplate>
<ItemTemplate>
<asp:HiddenField ID="hfBlogID" Value='<%#Eval("BlogID")%>' runat="server" />
<p>
<%#Eval("BlogTitle")%></p>
<p>
<%#Eval("BlogDetails")%></p>
</ItemTemplate>
</asp:ListView>
In order to determine the correct row index you should place your button inside of your ListView.ItemTemplate and handle the ListView.ItemCommand event.
In order to implement this approach you would have to change your code as follows:
<asp:ListView ID="lvArticle" runat="server" OnItemCommand="lv_ItemCommand">
..
<ItemTemplate>
<asp:HiddenField ID="hfBlogID" Value='<%#Eval("BlogID")%>' runat="server" />
<p>
<%#Eval("BlogTitle")%></p>
<p>
<%#Eval("BlogDetails")%></p>
<asp:Button runat="server" CommandName="find" CommandArgument='<%# Eval("yourIDField") %>' />
</ItemTemplate>
...
In code behind:
protected void lv_ItemCommand(object sender, ListViewCommandEventArgs e)
{
switch (e.CommandName)
{
case "find":
var hidden = e.Item.FindControl("your hidden id") as HiddenField;
break;
}
}
If your button is not inside your ListView, then you would need a way to identify the row you want to extract the hidden value from.
For example, if you allow to select a row in your ListView then you could get the hidden value from the selected row as follows:
protected void find_Click(object sender, EventArgs e)
{
var hidden = this.lv.Items[this.lv.SelectedIndex].FindControl("your hidden ID") as HiddenField;
}
If the button is in the same item template then use ItemCommand event handler and in that handler you can fetch the hidden field directly.
If the button is outside of list view then you need to get the index of item whose hidden field'd value you want to get.
you can use if button is in your listview
var control = (HiddenField)e.Item.FindControl("hfBlogID");
or if button is not in your listvew
var contorl = (HiddenField)this.lvArticle.Items[this.lvArticle.SelectedIndex].FindControl("hfBlogID");
Here you can access the hidden field for each item:
protected void Button1_Click(object sender,EventArgs e)
{
foreach(ListViewDataItem item in lvArticle.Items)
{
HiddenField hf=(HiddenField)item.FindControl("hfBlogID");
}
}
if you have index of item already then you can get it directly like this
HiddenField hf=(HiddenField)lvArticle.Items[index].FindControl("hfBlogID");
Hope this will help..
All the ways I can think to do this seem very hackish. What is the right way to do this, or at least most common?
I am retrieving a set of images from a LINQ-to-SQL query and databinding it and some other data to a repeater. I need to add a textbox to each item in the repeater that will let the user change the title of each image, very similar to Flickr.
How do I access the textboxes in the repeater control and know which image that textbox belongs to?
Here is what the repeater control would look like, with a submit button which would update all the image rows in Linq-to-SQL:
alt text http://casonclagg.com/layout.jpg
Edit:
This code works
Just make sure you don't blow your values away by Binding outside of if(!Page.IsPostBack) like me.. Oops.
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="itemBox">
<div class="imgclass">
<a title='<%# Eval("Name") %>' href='<%# Eval("Path") %>' rel="gallery">
<img alt='<%# Eval("Name") %>' src='<%# Eval("Path") %>' width="260" />
</a>
</div>
<asp:TextBox ID="TextBox1" Width="230px" runat="server"></asp:TextBox>
</div>
</ItemTemplate>
</asp:Repeater>
And Submit Click:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in Repeater1.Items)
{
TextBox txtName = (TextBox)item.FindControl("TextBox1");
if (txtName != null)
{
string val = txtName.Text;
//do something with val
}
}
}
Have you tried something like following on the button click:-
foreach (RepeaterItem item in Repeater1.Items)
{
TextBox txtName= (TextBox)item.FindControl("txtName");
if(txtName!=null)
{
//do something with txtName.Text
}
Image img= (Image)item.FindControl("Img");
if(img!=null)
{
//do something with img
}
}
/* Where txtName and Img are the Ids of the textbox and the image controls respectively in the repeater.*/
Hope this helps.
.aspx
<asp:Repeater ID="rpt" runat="server" EnableViewState="False">
<ItemTemplate>
<asp:TextBox ID="txtQty" runat="server" />
</ItemTemplate>
</asp:Repeater>
.cs
foreach (RepeaterItem rptItem in rpt.Items)
{
TextBox txtQty = (TextBox)rptItem.FindControl("txtQty");
if (txtQty != null) { Response.Write(txtQty.Text); }
}
Be sure to add EnableViewState="False" to your repeater, otherwise you will get empty string. (That wasted my time, dont waste yours :) )
On postback, you can iterate over the collection of RepeaterItems in repeater.Items. You could then retrieve each TextBox with code such as
TextBox tbDemo = (TextBox)rptr.Items[index].FindControl("textBox");