Repeater ItemCommand does not fire when clicking a button inside composite control - c#

I have a Repeater containing nested custom CompositeControl controls in the following way:
Wrapper
Head
Body
<asp:UpdatePanel ID="noteArea" UpdateMode="Conditional" ChildrenAsTriggers="false" runat="server" >
<ContentTemplate>
<asp:Repeater ID="noteRepeater" runat="server" EnableViewState="true" OnItemDataBound="noteRepeater_ItemDataBound" OnItemCommand="noteRepeater_ItemCommand">
<ItemTemplate>
<asp:Button runat="server" CommandName="edit" ID="testButton" />
<easit:NoteControl ID="noteControl" runat="server" />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
Head control contains two Buttons. When I click on either of them, the ItemCommand event of Repeater doesn't get invoked. If I move the buttons right to ItemTemplate, it works. But I need to keep them where they are.
What is the correct way to bubble them up the control hierarchy?

You can have the control throw an event which your page (with the repeater) can listen for. Any information you need for the event can be provided when initializing each control.

These controls are outside the ItemTemplate .. so they really can't trigger an ItemCommand.
An ItemCommand has specific attributes that will not be available to your buttons if they are outside the ItemTemplate (ItemIndex for instance, to determine the index of the clicked item)

Related

Dynamic data in Repeater

in my webpage different users can visit same profile, but users can be ither Owner of a profile, or Visitors, and content have to change for those two conditions.
if visitor check profile he see this editor:
<asp:Repeater id="UserLoginRepeater" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server">label Text</asp:Label> <asp:Label runat="server">label text 2</asp:Label>
</br>
<asp:Button runat="server" Text="TestButton"/>
</br>
<asp:Label ID="Label3" runat="server">Test</asp:Label> <textarea><%# Eval("Content") %></textarea>
</br>
</br>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
codebehinde:
if User is Owner of a profile TextArea have to be replaced by a label and an other label with textbox have to be added.
I found couple of possible implementations of this on internet, but they are not so easy to maintain. for example
http://tinyurl.com/9764eys
What would be the best way to Load controls dynamicly into Repeater?
Use a <asp:ContentPlaceholder> then in your code behind add a handler for the OnItemDataBound event. If you want those controls to handle events, make sure you bind your repeater before OnPreRender for the page.
Why not just create two repeaters for two conditions?
Just bind the repeater for the specific user and hide the other one.
Made Dynamic load of Controllers in code-behinde.

can't find control in FormView?

I need find this <a> tag resided in a FormView control, I need to remove this a tag depending on the condition but I can't find it using FormView.FindControl method
<asp:UpdatePanel ID="upDiscipline" runat="server">
<ContentTemplate>
<asp:FormView ID="fvMediaIntro" runat="server">
<ItemTemplate>
<div class="clipControls">
<a runat="server" id="iNeedToFindThis" href="#">here</a>
</div>
</ItemTemplate>
</ContentTemplate>
</asp:UpdatePanel>
I tried fvMediaIntro.FindControl() and fvMediaIntro.Row.FindControl(), neither worked.
Any idea please??
FindControl will work only after those controls are created i.e when data is bound to the FormView. So you need to use appropriate event on FormView such ItemCreated or DataBound. For example,
protected void fvMediaIntro_ItemCreated(Object sender, EventArgs e)
{
var control = fvMediaIntro.Row.FindControl("iNeedToFindThis") as HtmlAnchor;
}
Assuming, you are binding in page_load or using mark-up, you can also use prerender event of parent page/control safely to do FindControl.

Bring up input form on same page with dropdownlist?

I'm brand new at this, using vs2010 with asp.net and c#, and I'm trying to use a button click event to display forms on my "Add Product" page (the forms will be textbox/label based for inputting data), using items from a dropdownlist of products. Which methods are available/is there a 'best practice' for this sort of thing? I've been fooling around with an if (Dropdownlist.SelectedIndexChanged) statement, but I'm not quite clear on why the syntax requires the SelectedIndexChanged method to preclude a += or -=. Thoughts?
The SelectedIndexChanged method has the += because you are adding an event handler to a specific elements event. Is the button you click on the Add Product page? If so for the OnClick event you could just set your form details based on the DropDownList SelectedItem or SelectedIndex. You may also need to wrap that panel in an UpdatePanel so that it can update visibility without reloading the whole page.
<asp:UpdatePanel ID="updateFormPanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblName" runat="server" Text="" />
<asp:TextBox ID="txtDetails" runat="server" Text="" />
//More TextBox's or whatever you want
</ContentTemplate>
</asp:UpdatePanel?
<asp:DropDownList ID="ddlProductCategory" runat="server" />
<asp:Button ID="btnAddProduct" runat="server" OnClick="AddProduct_Click" Text="Add Product" />
//Code File Behind
protected void AddProduct_Click(Object sender, EventArgs e)
{
lblName.Text = ddlProductCategory.SelectedItem.Text;
txtDetails.Text = ddlProductCategory.SelectedItem.Value;
}

AJAX Update Panel - Action button click on drop down selection

Evening all
I have the following scenarion. I have a range of drop down menus where a clietn can select. The code below is wrapped in an update panel but without this, the button click fires a method to retrieve the number of products. So for example, an item is selected from ddlCategory, the btnValidate is clicked and the label returns the number of products within that category.
I have the following code for an update panel - I'm just not sure how to implement if effectively.
<asp:UpdatePanel ID="UpdatePanel1" runat="Server">
<ContentTemplate>
<asp:Label ID="lblSearchResultsStatus" runat="server" Text="Number of results found: "></asp:Label>
<asp:Label ID="lblSearchResults1" runat="server" Text=""></asp:Label>
<br />
<br />
<asp:Button ID="btnValidate" runat="server" Text="Validate Search"
OnClick="btnValidate_Click" Width="120px" />
</ContentTemplate>
</asp:UpdatePanel>
How do I go about wiring the update panel so that when a drop down list item is selected, the buttong is effectively clicked?
Do I have to implement something on each of the ddlSelectedIndexChanged event or is there a property within the update panel that does?
Apologies for the noob question.
The point of the UpdatePanel is to update a portion of the page with an AsyncPostBack instead of reloading the whole page, but in order for the drop-down lists to trigger an AsyncPostBack automatically, they must be on an UpdatePanel. In order to update the labels, they must be on the same UpdatePanel with the labels.
A common pattern to implement what you want to accomplish:
Put the DDLs on the UpdatePanel, and set AutoPostBack="true" on each DDL, so they trigger AsyncPostBacks.
Add an event handler for SelectedIndexChanged on each DDL (can be the same event handler).
Move whatever you do in btnValidate_Click to another method.
Call the new method from btnValidate_Click and the SelectedIndexChanged event handler(s) so they all perform the same function.
You can call your btnValidate_Click event from codebehind at any point, i.e. Page_Load
protected void Page_Load(object sender, EventArgs e)
{
btnValidate_Click(btnValidate, new EventArgs());
}

How Do I Get a Dynamic Control's Value after Postback?

I have a listview that adds controls in the ItemDataBound Event. When the postback occurs, I cannot find the new controls. After a bit of research, I found that ASP .NET needs these controls created every time, even after postback. From there I moved the function to bind the ListView outside of the if (!Page.IsPostBack) conditional. Now I get the dynamic controls values but the static controls I have are set to their defaults. Here is a sample of what I am trying to accomplish:
For brevity, I left some obvious things out of this example.
<asp:ListView runat="server" ID="MyList" OnItemDataBound="MyList_ItemDataBound">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceholder" />
</LayoutTemplate>
<ItemTemplate>
<asp:PlaceHolder runat="server" ID="ProductPlaceHolder">
<asp:TextBox runat="server" ID="StaticField" Text="DefaultText" />
<asp:PlaceHolder ID="DynamicItems" runat="server" />
</asp:PlaceHolder>
</ItemTemplate>
</asp:ListView>
and here is the codebehind:
protected void MyList_ItemDataBound(object sender, System.Web.UI.WebControls.ListViewItemEventArgs e) {
PlaceHolder DynamicItems = (PlaceHolder)e.Item.FindControl("DynamicItems");
DynamicItems.Controls.Add(textbox);
}
So, like I said, if I only databind when Page != PostBack then I cant find my dynamic controls on postback. If I bind every time the page loads then my static fields get set to their default text.
Try moving the data binding of the ListView into the OnInit() event.
Very similar question (instead of populating a ListView the guy is generating a set of buttons). Briefly, you'll find that you have to store the items in the list in your Viestate - than fish it out on Postback and re-populate the list.
Note that this solutions implies dropping data-binding (which you might not wanna do for others reasons).
Hope it helps.

Categories