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

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

Related

foreach statement for data_ItemDataBound for each row in ASP.NET C#

protected void data_ItemDataBound(object sender, DataListItemEventArgs e)
{
//if (data.Items.Count > 0)
//{
//foreach (DataListItem item in (dt.DataItem)).Row.ItemArray[0])
//{
if (e.Item.ItemType == ListItemType.Item)
{
DataListItem dt = e.Item;
Label accept = (Label)e.Item.FindControl("acceptYN");
if (accept.Text == "0")
{
(e.Item.FindControl("acceptbut") as System.Web.UI.HtmlControls.HtmlControl).Visible = true;
(e.Item.FindControl("confirm") as System.Web.UI.HtmlControls.HtmlControl).Visible = false;
}
else
{
(e.Item.FindControl("confirm") as Web.UI.HtmlControls.HtmlControl).Visible = true;
(e.Item.FindControl("acceptbut") as System.Web.UI.HtmlControls.HtmlControl).Visible = false;
}
}
//}
//}
}
e.item.datalist.row.thearray

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.

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

Raising an event from usercontrol and notifying subscribers

I have a usercontrol DmsRegisterPod which is being output within a repeater which is itself, inside an UpdatePanel. I have an event on the DmsRegisterPod called OnUpdated which I'm subscribing to in the repeaters ItemDataBound event e.g:
protected void rptPendingDmsRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DealershipIRLink irLink = (DealershipIRLink)e.Item.DataItem;
DmsRegisterPod dmsRegisterPod = (DmsRegisterPod)e.Item.FindControl("ucDmsRegisterPod");
dmsRegisterPod.ValidationGroup = string.Format("dms-pod-{0}", e.Item.ItemIndex);
dmsRegisterPod.DealershipIRLink = irLink;
dmsRegisterPod.OnUpdated += dmsRegisterPod_OnUpdated;
}
}
private void dmsRegisterPod_OnUpdated(object sender, EventArgs e)
{
this.DataBind();
}
The event is setup as such in the usercontrol:
public event EventHandler OnUpdated;
private void Updated(EventArgs e)
{
if (this.OnUpdated != null)
{
OnUpdated(this, e);
}
}
It's being raised in the Accept click handler:
protected void btnAccept_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
this.DealershipIRLink.dms_account_number = txtDmsNumber.Text;
this.DealershipIRLink.id_dealer_ir_link_status = DealerIRLinkStatus.DealerIRLinkStatusIdentifier.Approved;
this._irLinkService.UpdateDealershipIRLink(this.DealershipIRLink);
this.Updated(e);
}
}
However, the handler, OnUpdated is always null so the event never gets raised. It's as though the control is losing the event binding somehow. Can anybody see what I've done wrong here?
Register the event handler in ItemCreated instead of ItemDataBound which is triggered only when you databind the control and not on every postback(required):
protected void rptPendingDmsRequests_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DmsRegisterPod dmsRegisterPod = (DmsRegisterPod)e.Item.FindControl("ucDmsRegisterPod");
dmsRegisterPod.OnUpdated += dmsRegisterPod_OnUpdated;
}
}
All other logic that depends on the datasource belongs to ItemDataBound:
protected void rptPendingDmsRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DealershipIRLink irLink = (DealershipIRLink)e.Item.DataItem;
DmsRegisterPod dmsRegisterPod = (DmsRegisterPod)e.Item.FindControl("ucDmsRegisterPod");
dmsRegisterPod.ValidationGroup = string.Format("dms-pod-{0}", e.Item.ItemIndex);
dmsRegisterPod.DealershipIRLink = irLink;
}
}

error when find div ingridview nested in datalist

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

Categories

Resources