CompositeDataBoundControl - databound values overwritten before event is fired due to DummyDataSource - c#

I have a custom servercontrol that inherits from CompositeDataBoundControl. I have three templates: one header template, one footer template and one item template. The item template can contain a checkbox that I use to decide if I should delete the item.
In the footer and/or header templates I have a button with a CommandName of "DeleteItem". When that button is clicked, I handle the event in OnBubbleEvent:
if (cea.CommandName == "DeleteItem") {
//loop through the item list and get the selected rows
List<int> itemsToDelete = new List<int>();
foreach(Control c in this.Controls){
if (c is ItemData) {
ItemData oid = (ItemData)c;
CheckBox chkSel = (CheckBox)oid.FindControl("chkSelected");
if (chkSel.Checked) {
itemsToDelete.Add(oid.Item.Id);
}
}
}
foreach (int id in itemsToDelete) {
DeleteItem(id);
}
}
}
The problem is that Item is null since the CreateChildControls method already has been run as asp.net needs to recreate the control hierarchy before the event fire. It uses the DummyDataSource and a list of null objects to recreate the control hierarchy:
IEnumerator e = dataSource.GetEnumerator();
if (e != null) {
while (e.MoveNext()) {
ItemData container = new ItemData (e.Current as OrderItem);
ITemplate itemTemplate = this.ItemTemplate;
if (itemTemplate == null) {
itemTemplate = new DefaultItemTemplate();
}
itemTemplate.InstantiateIn(container);
Controls.Add(container);
if (dataBinding) {
container.DataBind();
}
counter++;
}
}
The problem is this line: ItemData container = new ItemData (e.Current as OrderItem); When the control hierarchy is rebuilt before the event is fired, the e.Current is null, so when I try to find out which item was marked for deletion, I get 0 since the original value has been overwritten.
Any suggestions on how to fix this?

I've finally found a solution that works. The problem is that the bound data is only connected to the control when being bound and directly after(normally accessed in a ItemDataBound event).
So to solve it I had to add a hidden literal containing the data item id to the container control. In the OnBubbleEvent I find the hidden literal and get the id:
ItemData oid = (ItemData)c;
CheckBox chkSel = (CheckBox)oid.FindControl("chkSelected");
if(chkSel != null) {
if(chkSel.Checked) {
Literal litId = (Literal)oid.FindControl("litId");
itemsToDelete.Add(Utils.GetIntegerOnly(litId.Text));
}
}

Related

Using findControl to find a child element

I have a Placeholder and I have a dynamically created panel in the placeholder, I also have some dynamically added radio buttons in the panel, now I can usefindControl() to find the radio buttons if they are direct children of the placeholder.
I've literally spent the whole of yesterday trying to find them when they are the child elements of the Panel. How is there a way to do this?
Here's my code below:
PlaceHolder1.Controls.Add(myPanel); //add the panel to the placeholderenter code here
myPanel.Controls.Add(myRadioButton); //add the radiobutton to the panel
You should make method that recursively searches for a control using it's Id. That mean that the method will search for a control inside of (in your case) placeholder. If method finds control, it will return it. If not, it will go search every placeholder's subcontrol, going "deeper". And then, if nothing is found, it will search one more level down, in every placeholder subcontrols' subcontrol etc.)
private Control FindControl(string ctlToFindId, Control parentControl)
{
foreach (Control ctl in parentControl.Controls)
{
if (ctl.Id == ctlToFindId)
return ctl;
}
if (ctl.Controls != null)
{
var c = FindControl(ctlToFindId, ctl);
if (c != null) return c;
}
return null;
}
and then use it like this:
Control ctlToFind = FindControl(myRadioButton.Id, Placeholder1);
if (ctlToFind != null)
{
//your radibutton is found, do your stuff here
}
else
{
// not found :(
}
Finding Controls recursive is an option, but it also has a couple of down-sides.
If you know the ID's of all the controls you can just use FindControl
RadioButtonList myRadioButton = PlaceHolder1.FindControl("Panel1").FindControl("RadioButtonList1") as RadioButtonList;
Label1.Text = myRadioButton.SelectedValue;
But you will need to give your dynamically added controls an ID.
Panel myPanel = new Panel();
myPanel.ID = "Panel1";
RadioButtonList myRadioButton = new RadioButtonList();
myRadioButton.ID = "RadioButtonList1";
PlaceHolder1.Controls.Add(myPanel);
myPanel.Controls.Add(myRadioButton);

Control is removed from old parent control but not if i use a List<Control>, why?

It's probably a very basic question about the behaviour of C# and WebControl. I got this working, but it would be nice if someone could clarify where the difference lays.
Before
I have a dictionary with a given key (Guid) and a Panel.
var tmpFormButtonPanel = new Panel();
_formButtonPanelDict.TryGetValue(new Guid(_hiddenField.Value), out tmpFormButtonPanel);
This panel contains a WebControl. Now I'd like to assign this button to another panel.
if (tmpFormButtonPanel != null)
{
var tmpControls = new List<Button>();
foreach (Button tmpButton in tmpFormButtonPanel.Controls)
{
tmpControls.Add(tmpButton);
}
tmpControls.Reverse();
foreach (var tmpButton in tmpControls)
{
tmpButton.AddCssClass("xy");
_buttonPanel.Controls.Add(tmpButton);
}
}
The moment I add the button to the _buttonPanel, it deletes the button out of tmpFormButtonPanel. From what I've heard or read, a WebControl can only be assigned to one panel. So this would explain why it doesn't work.
So I changed the code to this.
var tmpFormButtonList = new List<ButtonBaseUc>();
if (!_formButtonDict.TryGetValue(new Guid(_hiddenField.Value), out tmpFormButtonList))
{
tmpFormButtonList = new List<ButtonBaseUc>();
_formButtonDict.Add(new Guid(_hiddenField.Value), tmpFormButtonList);
}
foreach (var tmpButton in tmpFormButtonPanel.Controls)
{
if (tmpButton is ButtonBaseUc)
{
tmpFormButtonList.Add((ButtonBaseUc)tmpButton);
}
}
The last part does the same thing, but on the tmpFormButtonList.
if (tmpFormButtonList!= null)
{
var tmpControls = new List<Button>();
foreach (Button tmpButton in tmpFormButtonList)
{
tmpControls.Add(tmpButton);
}
tmpControls.Reverse();
foreach (var tmpButton in tmpControls)
{
tmpButton.AddCssClass("xy");
_buttonPanel.Controls.Add(tmpButton);
}
}
This is working. But why? I am only assigning the button to another list before adding it to the new panel. The references are still the same. What am I missing?
A control can only belong to one parent control. Since you have assigned it to the Panel in the dictionary-value, it will be removed there if you move it to the _buttonPanel.
This isn't documented but you can see it in the source:
// ...
if (control._parent != null) {
control._parent.Controls.Remove(control);
}
You have fixed this by not using a Panel as "storage" but a List<ButtonBaseUc>. This list is not a control(so the control has no parent), hence it must not be removed if you assign it to another (parent-)control.

Dynamically adding rows to ASP.NET repeater in reaction to event

I am trying to create ASP.NET server control (pure code, without ascx template - because control must be completly contained in .dll and it must not rely on external .ascx files), and I have a problem with dynamically adding items to repeater.
I want to add item to repeater in reaction to SelectedIndexChanged event, but when i do second DataBind() in that event, i lose data from ViewModel (for example, textboxes contains default data instead of text entered by user).
Simplified version of my code (in large portion borrowed from MS composite control example - http://msdn.microsoft.com/en-us/library/3257x3ea%28v=vs.100%29.aspx):
[ToolboxData("<{0}:FilterControl runat=server />")]
public class FilterControl : CompositeControl, IPostBackDataHandler
{
private List<FilteringProperty> elements = new List<FilteringProperty>();
private DropDownList filteringElementsDropDownList;
private Repeater usedFiltersRepeater;
[Bindable(true), DefaultValue(null), Description("Active filters")]
public List<FilteringProperty> UsedElements
{
get
{
EnsureChildControls();
if (ViewState["UsedElements"] == null)
{
ViewState["UsedElements"] = new List<FilteringProperty>();
}
return (List<FilteringProperty>)ViewState["UsedElements"];
}
set
{
EnsureChildControls();
ViewState["UsedElements"] = value;
}
}
protected override void RecreateChildControls()
{
EnsureChildControls();
}
protected override void CreateChildControls()
{
Controls.Clear();
filteringElementsDropDownList = new DropDownList { AutoPostBack = true };
usedFiltersRepeater = new Repeater();
foreach (var element in elements)
{
filteringElementsDropDownList.Items.Add(new ListItem(element.DisplayName));
}
filteringElementsDropDownList.SelectedIndexChanged += (sender, e) =>
{
string selectedText = filteringElementsDropDownList.SelectedValue;
FilteringProperty condition = elements.First(x => x.DisplayName == selectedText);
var toRemove = filteringElementsDropDownList.Items.Cast<ListItem>().FirstOrDefault(x => x.Text == condition.DisplayName);
if (toRemove != null)
{
filteringElementsDropDownList.Items.Remove(toRemove);
}
UsedElements.Add(condition);
// ======> A <========
};
usedFiltersRepeater.ItemDataBound += (sender, args) =>
{
FilteringProperty dataItem = (FilteringProperty)args.Item.DataItem;
Control template = args.Item.Controls[0];
TextBox control = (TextBox)template.FindControl("conditionControl");
control.Text = dataItem.DisplayName;
// ======> C <========
};
usedFiltersRepeater.ItemTemplate = // item template
usedFiltersRepeater.DataSource = UsedElements;
usedFiltersRepeater.DataBind();
// ======> B <========
Controls.Add(filteringElementsDropDownList);
Controls.Add(usedFiltersRepeater);
}
}
I marked important portions of code with (A), (B) and (C)
The problem is, (A) is executed after DataBinding (B and C), so changes in UsedElements are not visible until next postback.
It is possible to add usedFiltersRepeater.DataBind(); after (A), but than all controls are recreated without data from viewstate (i.e empty)
Is there a way to dynamically change repeater after databinding, such that data of contained controls is preserved?
Tl;dr - i have a DropDownList and I want to add editable items to Repeater on SelectedIndexChanged (without losing viewstate).
I finally solved my problem.
My solution is rather dirty, but it seems to work fine.
Instead of simple databinding:
I get state from all controls in repeater and save it in temporary variable (state for each control includes everything, such as selected index for dropdownlists) using my function GetState()
modify this state in any way i want
restore full state using my function SetState()
For example:
FilterState state = GetState();
state.Conditions.Add(new ConditionState { Item = condition });
SetState(state);

Add Items To Sitecore Combobox

I'm creating a Sitecore Sheer UI wizard which has markup like this
<WizardFormIndent>
<GridPanel ID="FieldsAction" Columns="2" Width="100%" CellPadding="2">
<Literal Text="Brand:" GridPanel.NoWrap="true" Width="100%" />
<Combobox ID="Brand" GridPanel.Width="100%" Width="100%">
<!-- Leave empty as I want to populate available options in code -->
</Combobox>
<!-- Etc. -->
</WizardFormIndent>
But I cannot seem to find a way to add options to the combobox "Brand" in the code beside. Does anyone know how to finish the code below?
[Serializable]
public class MySitecorePage : WizardForm
{
// Filled in by the sheer UI framework
protected ComboBox Brands;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!Context.ClientPage.IsEvent)
{
IEnumerable<Brand> brandsInSqlDb = GetBrands();
// this.Brands doesn't seem to have any methods
// to add options
}
}
}
First off, I'm assuming you're using the Sitecore Combobox from Sitecore.Web.UI.HtmlControls (and not the Telerik control for instance)?
Looking in Reflector, it end up doing something like this:
foreach (Control control in this.Controls)
{
if (control is ListItem)
{
list.Add(control);
}
}
So I'm expecting you'll need to build a loop through your brandsInSqlDb, instantiate a ListItem and add it to your Brands Combobox.Something like
foreach (var brand in brandsInSqlDb)
{
var item = new ListItem();
item.Header = brand.Name; // Set the text
item.Value = brand.Value; // Set the value
Brands.Controls.Add(item);
}
It should be lowercase B (Combobox not ComboBox). Full namespace is:
protected Sitecore.Web.UI.HtmlControls.Combobox Brands;
Then you can add options, e.g.:
ListItem listItem = new ListItem();
this.Brands.Controls.Add((System.Web.UI.Control) listItem);
listItem.ID = Sitecore.Web.UI.HtmlControls.Control.GetUniqueID("ListItem");
listItem.Header = name;
listItem.Value = name;
listItem.Selected = name == selectedName;
The way I do it is to 1st access the Combo box from the page:
ComboBox comboBox = Page.Controls.FindControl("idOfYourComboBox") as ComboBox
Now you got the access to the control you defined in your page. All now you have to do is to assign value to it:
foreach (var brand in brandsInSqlDb)
{
comboBox .Header = brand.Name; // Set the text
comboBox .Value = brand.Value; // Set the value
Brands.Controls.Add(item);
}

Actual Element That Lost Focus

I am working on an application that has a GridView item on an ASP.net page which is dynamically generated and does a partial post-back as items are updated within the grid-view. This partial post-back is causing the tab indices to be lost or at the very least ignored as the tab order appears to restart. The grid view itself already has the pre-render that is being caught to calculate the new values from the modified items in the grid-view. Is there a way to get what element had the focus of the page prior to the pre-render call? The sender object is the grid-view itself.
You can try using this function, which will return the control that caused the postback. With this, you should be able to reselect it, or find the next tab index.
private Control GetControlThatCausedPostBack(Page page)
{
//initialize a control and set it to null
Control ctrl = null;
//get the event target name and find the control
string ctrlName = Page.Request.Params.Get("__EVENTTARGET");
if (!String.IsNullOrEmpty(ctrlName))
ctrl = page.FindControl(ctrlName);
//return the control to the calling method
return ctrl;
}
Here's an instance where I had dynamically generated inputs that updated totals via AJAX on change. I used this code to determine the next tab index, based on the tab index of the control that caused the postback. Obviously, this code is tailored to my usage, but with some adjustments I think it could work for you as well.
int currentTabIndex = 1;
WebControl postBackCtrl = (WebControl)GetControlThatCausedPostBack(Page);
foreach (PlaceHolder plcHolderCtrl in pnlWorkOrderActuals.Controls.OfType<PlaceHolder>())
{
foreach (GuardActualHours entryCtrl in plcHolderCtrl.Controls.OfType<GuardActualHours>())
{
foreach (Control childCtrl in entryCtrl.Controls.OfType<Panel>())
{
if (childCtrl.Visible)
{
foreach (RadDateInput dateInput in childCtrl.Controls.OfType<RadDateInput>())
{
dateInput.TabIndex = (short)currentTabIndex;
if (postBackCtrl != null)
{
if (dateInput.TabIndex == postBackCtrl.TabIndex + 1)
dateInput.Focus();
}
currentTabIndex++;
}
}
}
}
}

Categories