ASP.NET User Control Repeater.ItemDataBound Event Not Being Triggered - c#

Event registered in aspx
<asp:Repeater ID="StepRepeater" OnItemDataBound="StepRepeater_ItemDataBound1" runat="server">
Tried with AutoEventWireUp true & false
Here's the method in the code behind:
public void LoadSteps(Request request)
{
Repeater StepRepeater = new Repeater();
StepRepeater.DataSource = request.Steps;
StepRepeater.DataBind();
}
protected void StepRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
}
When stepping through, it just goes straight through "StepRepeater.DataBind();" without hitting the ItemDataBound event.
Please let me know if any additional information would help.

Your OnItemDataBound value doesn't match your method name.
OnItemDataBound="StepRepeater_ItemDataBound1"
protected void StepRepeater_ItemDataBound
Remove 1 from the end of OnItemDataBound or change your method name.
Also as #Adil has stated, remove the new Repeater() line:
Repeater StepRepeater = new Repeater();
UPDATE: After reading your comment on another answer regarding adding the new Repeater() line to prevent a null reference error:
Adding new Repeater() is going to create a new instance of a Repeater control, therefore not referencing the Repeater on your ASPX markup file.
If you are receiving a null reference exception, you should check that your Inherits property in your #Page directive (usually the very top line of your ASPX file) matches the class in your .aspx.cs file, and that your CodeFile property matches your .aspx.cs filename.

You have binded event ItemDataBound to StepRepeater in html but you are assigning that datasource of newly created repeater object and no event is attached to this repeater object.
Remove this statement
Repeater StepRepeater = new Repeater();
You code will be
public void LoadSteps(Request request)
{
StepRepeater.DataSource = request.Steps;
StepRepeater.DataBind();
}
Change the name of OnItemDataBound event in html to match the code behind
<asp:Repeater ID="StepRepeater" OnItemDataBound="StepRepeater_ItemDataBound" runat="server">

Related

Click Event of Dynamically generated Anchor Not Firing

I have an anchor on Lable Text.I am creating anchor with runat="server" dynamically on click of a button, It gets created as expected. I want to use its click event but it does not fire.
My code :
lblEmail.Text = email + " <a href='#' runat='server' class='crossicon' onclick='removebtn_Click'></a> ";
protected void removebtn_Click(object sender, EventArgs e)
{
}
How Can I create event for this button?I don't want to use JS, as in that case I will have to use a hidden field for new value
Adding markup/text to the label in that way wouldn't add the linkbutton or register the event to the control tree at the server. For that behavior of dynamically adding controls along with the server events to be achieved, you need to register the controls and events (as shown below)
aspx:
<asp:Panel runat="server" id="pnlEmail">
<asp:Label runat="server" id="lblEmail"/>
</asp:Panel>
aspx.cs:
In whichever event, you want to set the label text (along with the link)
lblEmail.Text = email;
LinkButton lnkbtnEmail = new LinkButton();
lnkbtn.Click += lnkbtn_Click;
lnkbtn.Text = "Dynamic Link";
pnlEmail.Controls.Add(lnkbtnEmail);
And the Handler would be
void lnkbtn_Click(object sender, EventArgs e)
{
// code for your dynamically generated link
}
By default, controls use __doPostBack to do the postback to the server. __doPostBack takes the UniqueID of the control (or in HTML, the name property of the HTML element). The second parameter is the name of the command to fire.
<a href='#' runat='server' class='crossicon' href="javascript:void(0);" onclick="__doPostBack('someuniqueid', '');></a>
System.Web.UI.Page already implements the IPostBackEventHandler interface by default, so you don't need to implement it on every page - it's already there.
You can override the RaisePostBackEvent method on the page like this:
protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
//call the RaisePostBack event
base.RaisePostBackEvent(source, eventArgument);
if (source == SomeControl)
{
//do something
}
}
I hope it will help you.
I don't think so it is created dynamically. Where is ID of that control. You have to Create Server side control and add it into some placeholder/panel e.t.c

How to find a HTML tag with runat=server into a repeater?

I'm generating a table using a Repeater and I need to set a <td> as runat=server to set visibility for it.
I'm trying to find it into ItemDataBound event using FindControl method, but it doesn't work.
Hot can I achieve this?
If you want to do that, you should write like this:
Visible=<%= SetVisiblity() %>
where SetVisiblity is a public function
This should do the trick. First, create a method called to catch the repeater's OnDataItemBound event.
protected void MyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// Use FindControl, but start from the context of the RepeaterItem.
//
HtmlTableCell cell = e.item.FindControl("CellID") as HtmlTableCell;
if ( cell != null )
{
// Do what you gotta do.
}
}
You can explicitly wire the event up on the repeater markup.
<asp:Repeater ID="MyRepeater" runat="server" OnItemDataBound="MyRepeater_ItemDataBound">
</asp:Repeater>

how to find control in ItemTemplate of the ListView from code behind of the usercontrol page?

actually, i'm developing a web template using ASP.NET and C#.
i have a listview in a usercontrol page and inside the ItemTemplate i have a PlaceHolder as below:
<asp:PlaceHolder ID="ph_Lv_EditModule" runat="server"> </asp:PlaceHolder>
i want to access to this PlaceHolder from code behind and i have use different method as below but i couldn't access it.
PlaceHolder ph_Lv_EditModule = (PlaceHolder)lv_Uc_Module.FindControl("ph_Lv_EditModule");
or
PlaceHolder ph_Lv_EditModule = (PlaceHolder)this.lv_Uc_Module.FindControl("ph_Lv_EditModule");
could you please help me how to find this control at the code behind of my usercontrol page.
appreciate your consideration.
A ListView typically contains more than one item, therefore the NamingContainer(searched by FindControl) of your Placeholder is neither the UserControl, nor the ListView itself. It's the ListViewItem object. So one place to find the reference is the ListView's ItemDataBound event.
protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
var ph_Lv_EditModule = (PlaceHolder)e.Item.FindControl("ph_Lv_EditModule");
}
}
If you need the reference somewhere else, you must iterate the Items of the ListView and then use FindControl on the ListViewItem.
By the way, this is the same behaviour as in other DataBound Controls like GridView or Repeater.
As Tim Schmelter mentioned, you can also access your control by iterating through your ListView as follows
private void HideMyEditModule()
{
foreach (var item in lv_Uc_Module.Items)
{
PlaceHolder holder = item.FindControl("ph_Lv_EditModule") as PlaceHolder;
if (holder!= null)
holder.Visible = false;
}
}

How to call an event handler from one control to the another control where the second control is inside the first control?

i have a calender control like this
<asp:Calendar ID="CldrDemo" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66"
OnSelectionChanged="CldrDemo_SelectionChanged" OnDayRender="CldrDemo_DayRender">
</asp:Calendar>
OnDayRender event i have code like this
protected void CldrDemo_DayRender(object sender, DayRenderEventArgs e)
{if (e.Day.Date == Convert.ToDateTime("11/30/2010"))//comparing date
{
DropDownList ddlBlist = new DropDownList();//creating instance of ddl
ddlBlist.AutoPostBack = true;
ddlBlist.Items.Add("Ashrith");//adding values to the ddl
ddlBlist.Items.Add("Nayeem");//adding values to the ddl
ddlBlist.SelectedIndexChanged += new EventHandler(ddlBlist_SelectedIndexChanged);//want to call this
string name = ddlBlist.SelectedItem.Text;
e.Cell.Controls.Add(ddlBlist);//adding dropdownlist to the cell
e.Cell.BorderColor = System.Drawing.Color.Black;
e.Cell.BorderWidth = 1;
e.Cell.BackColor = System.Drawing.Color.LightGray;
}
i want to call the event handler for the dropdownlist - selectedIndexchanged and i have added it also like this
protected void ddlBlist_SelectedIndexChanged(object sender, EventArgs e)
{
}
but this is not getting fire when i am changing the item of the dropdownlist. Please help
try this
ddlBlist.SelectedIndexChanged += new EventHandler("ddlBlist_SelectedIndexChanged");
try putting your calendar control in a Ajax update panel
and put this line before adding items in your combo box:
ddlBlist.SelectedIndexChanged += new EventHandler(ddlBlist_SelectedIndexChanged);
ddlBlist.Items.Add("Ashrith");//adding values to the ddl
ddlBlist.Items.Add("Nayeem");//adding values to the ddl
I believe in order to get this to work you need to have re-added your drop-down list to the controls collection before the SelectedIndexChanged event would normally be fired.
What's happening is, you're adding your control dynamically at render time, but when a post-back happens the control doesn't actually exist any more, or at least it won't until your render method gets called again. And so the event will not fire.
In my experience with adding controls dynamically like this, in order to be able to handle any events they raise you need to be able to re-create your dynamic control tree before the page's Load event occurs. If you can do this, you will probably find that your event will fire as normal.

page variable in a repeater

Hi I'm having a bit of an issue with a asp.net repeater
I'm building a categories carousel with the dynamic categories being output by a repeater.
Each item is a LinkButton control that passes an argument of the category id to the onItemClick handler.
a page variable is set by this handler to track what the selected category id is....
public String SelectedID
{
get
{
object o = this.ViewState["_SelectedID"];
if (o == null)
return "-1";
else
return (String)o;
}
set
{
this.ViewState["_SelectedID"] = value;
}
}
problem is that i cant seem to read this value while iterating through the repeater as follows...
<asp:Repeater ID="categoriesCarouselRepeater" runat="server"
onitemcommand="categoriesCarouselRepeater_ItemCommand">
<ItemTemplate>
<%#Convert.ToInt32(Eval("CategoryID")) == Convert.ToInt32(SelectedID) ? "<div class=\"selectedcategory\">":"<div>"%>
<asp:LinkButton ID="LinkButton1" CommandName="select_category" CommandArgument='<%#Eval("CategoryID")%>' runat="server"><img src="<%#Eval("imageSource")%>" alt="category" /><br />
</div>
</ItemTemplate>
</asp:Repeater>
calling <%=SelectedID%> in the item template works but when i try the following expression the value of SelectedID returns empty..
<%#Convert.ToInt32(Eval("CategoryID")) == Convert.ToInt32(SelectedID) ? "match" : "not a match"%>
the value is being set as follows...
protected void categoriesCarouselRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
SelectedID = e.CommandArgument.ToString();
}
Any ideas whats wrong here?
Within the categoriesCarouselRepeater_ItemCommand code you've shown, you're assigning the CommandArgument to a property called 'SelectedCategory'.
Should this not be assigning the property to the 'SelectedID' property instead?
** EDIT..
The problem I see is one of two scenarios:
1) You are not rebinding the repeater with each postback, and therefore the expression within your ItemTemplate is not being evaluated - The output from the repeater will remain unchanged with each postback.
OR
2) You are rebinding the repeater control with each postback, however, upon clicking on your LinkButton for the first time, the repeater control is re-binded PRIOR to the ItemCommand event handler firing, and therefore, the 'SelectedID' property has not been set until after the repeater has finished being output.
If you were to click on one of your LinkButtons a 2nd time, the previously selected ID would be in viewstate at the time of the repeater control contents being rendered, and therefore be one step behind in rendering which category has been clicked, and so on...

Categories

Resources