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());
}
}
}
Related
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();
}
}
}
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.
I have check boxes for each task in a repeater. When I check the boxes, the value of true is stored in the database but when I uncheck the check box, it does not store the value of false properly. Only when I uncheck the latest check box created, then I can change value of false of the others when I uncheck it. Any help or suggestions would be great. Thanks in advance!
Here is the code-behind to the repeater:
protected void rptTask_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HyperLink hlTask = (HyperLink)e.Item.FindControl("hlTask");
CheckBox cboxTask = (CheckBox)e.Item.FindControl("cboxTask");
Task task = e.Item.DataItem as Task;
cboxTask.InputAttributes.Add("TaskId", task.TaskId.ToString());
}
}
protected void TaskCheckBoxChanged(object sender, EventArgs e)
{
var taskId = Convert.ToInt32((sender as CheckBox).InputAttributes["TaskId"]);
using (TaskManagerEntities myEntities = new TaskManagerEntities())
{
Task task;
task = (from t in myEntities.Tasks
where t.TaskId == taskId
select t).SingleOrDefault();
foreach (RepeaterItem item in rptTask.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
CheckBox cboxTask = (CheckBox)item.FindControl("cboxTask");
task.Completed = cboxTask.Checked;
}
}
myEntities.SaveChanges();
}
}
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
I had datalist and in datalist gridview and in gridview div and I wanted to find this div I did my code but error apeared (object refrence....) here (Techgr1.Attributes.Add("Class", "ff");
)
protected void Datalist_Categories_ItemDataBound(object sender, DataListItemEventArgs e)
{
Page.LoadComplete += new EventHandler(Page_LoadComplete);
string LanguageID = Globals.GetSuitableLanguage(Page);
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Techgr1 = e.Item.FindControl("TechnologyGr") as HtmlGenericControl;
}
GridView gridfeature = (GridView)e.Item.FindControl("grid_features");
foreach (DataControlField column in gridfeature.Columns)
{
column.HeaderText = Globals.Translate(column.HeaderText, LanguageID);
Techgr1.Attributes.Add("Class", "ff");
}
}
Try this:
protected void Datalist_Categories_ItemDataBound(object sender, DataListItemEventArgs e)
{
Page.LoadComplete += new EventHandler(Page_LoadComplete);
string LanguageID = Globals.GetSuitableLanguage(Page);
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Techgr1 = e.Item.FindControl("TechnologyGr") as HtmlGenericControl;
}
GridView gridfeature = (GridView)e.Item.FindControl("grid_features");
foreach (DataControlField column in gridfeature.Columns)
{
column.HeaderText = Globals.Translate(column.HeaderText, LanguageID);
if(Techgr1 != null)
{
Techgr1.Attributes.Add("Class", "ff");
}
}
}