How to Fill Repeater ItemTemplate from Code Behind? - c#

I have to create a repeater dynamically from code behind. I want to fill the item template of this repeater from code behind but I couldn't find any kind of object to synchronize with the item template of the repeater.
Code:
Repeater rpr = new Repeater();
rpr.ItemTemplate = ??

protected override void OnItemCreated(RepeaterItemEventArgs e)
{
base.OnItemCreated(e);
if (e.Item.DataItem != null && (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem))
{
DataRowView dt = (DataRowView)e.Item.DataItem;
if (dt.DataView.Table.Columns["IsHeading"] != null)
{
if ((dt["IsHeading"].ToString()) == "true")
{
ItemHeaderContainer container = new ItemHeaderContainer();
ItemHeaderTemplate.InstantiateIn(container);
container.DataItem = e.Item.DataItem;
container.DataBind();
}
}
}
}
Refer:
http://www.neowin.net/forum/topic/658854-aspnet-repeaters-with-dynamic-itemtemplates/

You have to create instance of ITemplate. Read this example
http://www.codeproject.com/Articles/240760/Dynamically-create-item-templates-server-side

Related

How to get the parent Datalist's row ID

I have a nested Datalist.
I'm trying to get the parent Datalist's ID.
To do this I'm using ItemDataBound on parent Datalist. This line gets the parent Datalist object's row ID
but I'm trying to get the table row's ID which is on the table.
How can I do this?
protected void DataListMenu_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var innerDL = e.Item.FindControl("DataListSubMenu") as DataList;
if (innerDL != null)
{
string ID = (e.Item.ItemIndex).ToString();
}
}
}

Use DataItem in Code-behind to set class or style in Nested Repeater

I have looked around for awhile on this and I am not able to figure this out. I have a nested repeater that onItemDataBound event I would like to set class and style for some <DIV>.
HTML:
<%# DataBinder.Eval(Container.DataItem,"sServer") %>
>
CODE-BEHIND
protected void rpDB_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string _sql = "";
using(SqlConnection _conn = new SqlConnection(_sql))
{
_conn.Open();
DataTable _dt = new DataTable();
// Get repeater controls
Repeater rpDB_item = (Repeater)(e.Item.FindControl("rpDB_item"));
SqlCommand _cmd = new SqlCommand("", _conn);
SqlDataAdapter _da = new SqlDataAdapter(_cmd);
_da.Fill(_dt);
rpDB_item.DataSource = _dt;
rpDB_item.DataBind();
}
}
}
protected void rpDB_item_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (<value of dataitem("online")> == "Online")
{
((HtmlGenericControl)e.Item.FindControl("label2")).Attributes.Add("class", "glyphicon glyphicon-file");
((HtmlGenericControl)e.Item.FindControl("label2")).Attributes.Add("style", "color: green;");
((HtmlGenericControl)e.Item.FindControl("label2")).Attributes.Add("title", *<value of dataitem(sFile)>*);
}
}
}
Where I am stuck is in the code-behind I would like to use the value of one of the columns of the dataitem in some expressions, such as in the rpDB_item_ItemDataBound event above.
IE:
if (e.Item.DataItem("Online") == "Online")
{
((HtmlGenericControl)e.Item.FindControl("label2")).Attributes.Add("title", * e.Item.DataItem("sFile").ToString()*);
}
Obviously something is wrong I am just sure where to go from here. Ideally I am either setting a class or a title of a label based on the dataitem value or the value itself.
Maybe there is a better way of doing this, such as creating the <div> in code behind, not really sure how to do that either? Any help or suggestions would be appreciated (NOVICE C#)
EDIT:
I have added this function I think it is right
protected void FileExists(string url, RepeaterItemEventArgs e)
{
Label myLabel = (Label)(e.Item.FindControl("divfile"));
url = "#" + url;
if (File.Exists(url))
{
myLabel.Attributes.Add("class", "green");
}
else { myLabel.Attributes.Add("class", "red"); }
}
and the following label
<div class='anj red glyphicon glyphicon-file <%= %> id="dvFile" runat="server" title=<%# DataBinder.Eval(Container.DataItem,"FileName") %>></div>
How would I call the function? I tried
<%# FileExists(DataBinder.Eval(Container.DataItem,"FileName")) %>
inside the class but it is not sending the resulting string to the function.
The type of e.Item.DataItem is the type that's bound to the repeater.
So if you've bound a list of Foo to the repeater and you want to access the properties of an individual Foo then cast e.Item.DataItem as type Foo.
var myFoo = e.Item.DataItem as Foo
if(myFoo != null && myFoo.Online == "Online")
//Do something
protected void rpDB_item_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
HtmlGenericControl dbOnline = ((HtmlGenericControl)e.Item.FindControl("dbOnline"));
HtmlGenericControl sfile = ((HtmlGenericControl)e.Item.FindControl("lblfile"));
//HtmlGenericControl online = ((HtmlGenericControl)e.Item.FindControl("dbOnline"));
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string sonline = (string)(DataBinder.Eval(e.Item.DataItem, "Online/Offline").ToString());
string myfile = (string)(DataBinder.Eval(e.Item.DataItem,"FileName"));
if (sonline == "Online")
{
sfile.Attributes.Add("class", "green");
dbOnline.Attributes.Add("class", "led-green");
}
}
}
I added this and walked through it. Seems to be doing what is expected until the Attributes.Add section. It is not assigning the associated attributes. Again note that this is in a nested repeater if that makes a difference.

Repeater Control does not populate the first data

After binding data to repeater control I am trying to populate some control inside it through Placeholder.
I have written the following .
protected void RepeaterDetails_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
try
{
foreach (Control ri in RepeaterDetails.Items)
{
//if (ri.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
//{
Label lblDatatype = (Label)e.Item.FindControl("lblDatatype");
if (lblDatatype.Text != null)
{
if (lblDatatype.Text.ToLower() == "list")
{
PlaceHolder RepeaterPanel = (PlaceHolder)e.Item.FindControl("RepeaterPanel");
DropDownList ddl = new DropDownList();
ddl.Width = 150;
RepeaterPanel.Controls.Add(ddl);
i++;
break;
}
else
{
PlaceHolder RepeaterPanel = (PlaceHolder)e.Item.FindControl("RepeaterPanel");
tbx = new TextBox();
tbx.ID = "TextBox" + i;
tbx.Width = 150;
RepeaterPanel.Controls.Add(tbx);
i++;
break;
}
}
// }
}
}
catch
{
}
}
But the repeater control does not populate the first value.
please see the attached image
List and textboxes are getting populated except the first one. Can anyone help?

how to bind drop down list at edit item template Edit mode occurs in grid view

i just want to bind two drop down list dynamically when grid view in row editing mode. here i declares one code block that dynamically fetches that row state and binds up those two drop down list.
here is code :
protected void GV_ViewCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
{
...............
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
using (DataClassesDataContext db = new DataClassesDataContext())
{
DropDownList dl = (DropDownList)e.Row.FindControl("DDL_Types1");
dl.DataSource = db.PartyTypes.Select(t => t).ToList();
dl.DataBind();
dl.SelectedValue = DataBinder.Eval(e.Row.DataItem, "type_id").ToString();
DropDownList dl1 = (DropDownList)e.Row.FindControl("DDL_CountryNames1");
dl1.DataSource = db.Countries.Select(c => c).ToList();
if (!string.IsNullOrEmpty(DataBinder.Eval(e.Row.DataItem, "country_id").ToString()))
{
dl1.SelectedValue = DataBinder.Eval(e.Row.DataItem, "country_id").ToString();
DropDownList dl2 = (DropDownList)e.Row.FindControl("DDL_StateNames1");
dl2.DataSource = db.States.Where(s => s.country_id.Equals(int.Parse(DataBinder.Eval(e.Row.DataItem, "country_id").ToString()))).Select(s => s).ToList();
dl2.DataBind();
}
DataRowView rowView1 = (DataRowView)e.Row.DataItem;
if (rowView1["UserOFC"] != null)
{
(e.Row.FindControl("chk_UserOFC1") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserOFC").ToString());
}
if (rowView1["UserVAT"] != null)
{
(e.Row.FindControl("chk_UserVAT1") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserVAT").ToString());
}
if (rowView1["UserINV"] != null)
{
(e.Row.FindControl("chk_UserINV1") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserINV").ToString());
}
if (rowView1["UserNone"] != null)
{
(e.Row.FindControl("chk_UserNone1") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserNone").ToString());
}
}
}
..................
}
Is this right method to binds drop down list at row editing mode.
please help me....
It's fine.But there are some other ways.

How can I edit the DropDown item in the Repeater control

In this I have added one DropDownList in the Repeater control,
For that one DataTable is assigned as the DataSource.
But I want to edit the DropDownList.Items as per the DataSource data.
Means if the DataSource will give the 3 data then the DropDownLidt has the list items from 1,2,3
if that is 5 then 1,2,3,4,5 like this
So for that which Event I have to use and what code I should write?
In your itemdatabound of your Repeater, find your control, and bind to a database, or set values, or whatever you want as shown below:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView row = e.Item.DataItem as DataRowView;
DropDownList dl = e.Item.FindControl("ddlCategory") as DropDownList;
dl.DataSource = CategoriesDataTable;
dl.DataTextField = "CategoryDescription";
dl.DataValueField = "CategoryPK";
dl.SelectedValue = row["CategoryFK"].ToString();
dl.DataBind();
}
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
int count = 0;
// set count = your datatable count
DropDownList ddl = (DropDownList)e.Item.FindControl("ddl");
for(int i=1;i<=count;i++)
{
ddl.Items.Add(i.ToString());
}
}
}

Categories

Resources