umbraco usercontrol wrapper control not showing current value
I've created an umbrac usercontrol wrapper Data Type which wraps a DropDownList populated with data from a database. I have the Database datatype for the Umbraco Data Type set to "Integer", which is correct to the Value property on the item in the DropDownList (an int primary key).
I can select the value from the data type within the document and save correctly. This is verified by rending the value of the control on the page.
However, when I move to a different document and come back to the original, the value displayed in the drop down list list is the first item in the list.
My Usercontrol codebehind is this:
public partial class CircuitsDataType : System.Web.UI.UserControl, umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
{
private ICircuitRepository _circuitRepository;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
var circuits = _circuitRepository.GetAll();
CircuitList.DataSource = circuits;
CircuitList.DataValueField = "Id";
CircuitList.DataTextField = "Name";
CircuitList.DataBind();
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
_circuitRepository = new CircuitRepository();
}
public object value
{
get { return CircuitList.SelectedValue; }
set
{
CircuitList.SelectedIndex = -1;
var item = CircuitList.Items.FindByValue(value.ToString());
if (item != null) item.Selected = true;
}
}
}
It appears as if the underlying circuitId on the Document is not getting passed into the value property from IUsercontrolDataEditor.
Also, for extra points ( :p ) can someone remind me the correct way to do this:
I think you'll want to set check and set the item to selected in the Page_Load event (if !Page.IsPostBack) rather than from the value property. See this blog post for a quick example (uses a textbox but should be the same).
That makes sense re in Page_Load. Also I think perhaps the repository isn't set. I've also created a ascx that displays the Circuit in the template, based on the value selected in the above Data Type and I noticed last night that OnInit wasn't being fired before the property to set CircuitId was being called, resulting in a fail to call the repository. I suspect that may also be hapenning here. Will investigate.
I tried {$circuitId}, {@circuitId} and the variations of those with square brackets. I remembered doing it a while back but couldn't find where, to refer back :)
umbraco usercontrol wrapper control not showing current value
I've created an umbrac usercontrol wrapper Data Type which wraps a DropDownList populated with data from a database. I have the Database datatype for the Umbraco Data Type set to "Integer", which is correct to the Value property on the item in the DropDownList (an int primary key).
I can select the value from the data type within the document and save correctly. This is verified by rending the value of the control on the page.
<umbraco:Item field="circuitId" runat="server"></umbraco:Item>
However, when I move to a different document and come back to the original, the value displayed in the drop down list list is the first item in the list.
My Usercontrol codebehind is this:
It appears as if the underlying circuitId on the Document is not getting passed into the value property from IUsercontrolDataEditor.
Also, for extra points ( :p ) can someone remind me the correct way to do this:
Hi,
I think you'll want to set check and set the item to selected in the Page_Load event (if !Page.IsPostBack) rather than from the value property. See this blog post for a quick example (uses a textbox but should be the same).
For your bonus question, use CircuitId="[#circuitId]" - see Advanced Macro Parameter Syntax for more info
Hope this helps,
Tom
Thanks Tom.
That makes sense re in Page_Load. Also I think perhaps the repository isn't set. I've also created a ascx that displays the Circuit in the template, based on the value selected in the above Data Type and I noticed last night that OnInit wasn't being fired before the property to set CircuitId was being called, resulting in a fail to call the repository. I suspect that may also be hapenning here. Will investigate.
I tried {$circuitId}, {@circuitId} and the variations of those with square brackets. I remembered doing it a while back but couldn't find where, to refer back :)
is working on a reply...