error when find div ingridview nested in datalist - c#

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");
}
}
}

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();
}
}
}

Check box value is not storing properly when check box is unchecked

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();
}
}

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?

to disable a label of repeater on some condition in asp .net

Markup
<HeaderTemplate>
<table>
<tr>
<th>
<asp:Label ID="label12" runat="server" Text="Editor"></asp:Label>
</th>
</tr>
</HeaderTemplate>
Code behind
protected void ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label label12 = (Label)e.Item.FindControl("label12");
Label activeLabel = (Label)e.Item.FindControl("lblEditor");
string s = activeLabel.Text;
if (s != "Sao Palo")
{
activeLabel.Visible = true;
label12.Visible = true;
}
else
{
activeLabel.Visible = false;
label12.Visible = false;
}
}
}
I am getting a NullReferenceException at:
label12.visible=true;
This label is in the header, that's why it cannot be found in the repeater-items.
So change e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) to e.Item.ItemType == ListItemType.Header.
protected void ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
Label label12 = (Label)e.Item.FindControl("label12");
// ...
}
}
But since the other label is not in the hader but in in an item you need a different approach. You can also get the header-label via Repeater.Controls[0].Controls[0].FindControl("label12");.
So this should work:
protected void ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label label12 = (Label)((Repeater)sender).Controls[0].Controls[0].FindControl("label12");;
Label activeLabel = (Label)e.Item.FindControl("lblEditor");
string s = activeLabel.Text;
if (s != "Sao Palo")
{
activeLabel.Visible = true;
label12.Visible = true;
}
else
{
activeLabel.Visible = false;
label12.Visible = false;
}
}
}

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