I've been searching all over the internet, but I cannot get things to work. So now I'm writing here in hope that somebody can teach me what to do :-)
Basically I want to create a form which contains 2 dropdownlists (products and towns). The form shall send an email, and at the same time save the data in SQL. The dropdownlists shall have the Name.property from the children nodes of a specific node (that i select via a content-picker)
The way I'm trying to do it is to create an usercontrol in which i put the ASP.NET form. My problem is databinding the dropdown-lists with the Razor-code in which i make the list of children-nodes
I've found numerous articles on the internet, but I think i'm having trouble passing the razor-data to ASP.Net
Another way to do it, could be to get the XML from @Model.NodebyID() passed, and then create the list directly in ASP.Net
I hope you can help me, as I've run out of ideas. And if you have another way of doing the above I'm also interested :-)
I've actually been reading that post like a million times :-( but I don't see how that helps me with binding the dropdownlist with the list created in Razor.
How can I transfer the variable i've created in Razor and use it to databind the Dropdownlist in the usercontrol.
It might just be me that is not getting it, but if someone could make it clear even for me I would greatly appreciate it
Hmm you're not trying to databind values in Razor with code from a UserControl? Isn't it easier to also have the dropdownlist as a webcontrol in the usercontrol? That's easier to databind and to get the values in the postback. If you're going to test and you use 4.7.1 than you can use RenderEvent="PreRender" on the macro. That way the page load of the UserControl is hit first.
Do you have a code-example I can see? I'm the closest thing to a newbie regarding usercontrols and Razor, so I'm having trouble imagining what you are writing
I think I'm more confused now than before :-( But as far as I can comprehend, then I'm trying to do the opposite of what you are describing.
I need to be able to use a variable created in Razor (containing the list of childrens for a specific node) in the code-behind of the Usercontrol. This list I want to bind to a dropdownlist.
I've never tried getting a razor value in a usercontrol, but I think it's possible. If you have an input field on a form in razor and you cause a postback the value should be here:
And then on the template showing this usercontrol I have the code creating the foreach-loop:
<umbraco:Macro runat="server" language="cshtml"> @{ var Department= @Model.findDepartment; var parent = @Model.NodeById(@Model.Afdelinger); var strModeller = "";
Did you know you can also use that code inside the usercontrol? It's much better to just create a DropDownList there as a webcontrol and databind it with what you got there. Something like this can be done in the code behind of your usercontrol:
protected void Page_Load(object sender, EventArgs e)
{
dynamic model = new umbraco.MacroEngines.DynamicNode(umbraco.NodeFactory.Node.GetCurrent());
var Department = model.findDepartment;
var parent = model.NodeById(model.Afdelinger);
System.Collections.Generic.List<string> names = new System.Collections.Generic.List<string>();
if (parent != null)
{
foreach (var item in parent.Children.Where("Visible && " + Afdeling))
{
names.Add(@item.Name);
};
};
DdlNames.DataSource = names;
DdlNames.DataBind();
}
Yes that should also be possible in VB. I'm no VB expert, but I used an online converter. You might need to change some stuff.
Protected Sub Page_Load(sender As Object, e As EventArgs)
Dim model As dynamic = New umbraco.MacroEngines.DynamicNode(umbraco.NodeFactory.Node.GetCurrent())
Dim Department = model.findDepartment
Dim parent = model.NodeById(model.Afdelinger)
Dim names As New System.Collections.Generic.List(Of String)()
If parent IsNot Nothing Then
For Each item As var In parent.Children.Where("Visible && " & Afdeling)
names.Add(item.Name)
Next
End If
DdlNames.DataSource = names
DdlNames.DataBind()
End Sub
Use Razor code in UserControl
Hi guys (and girls)
I've been searching all over the internet, but I cannot get things to work. So now I'm writing here in hope that somebody can teach me what to do :-)
Basically I want to create a form which contains 2 dropdownlists (products and towns). The form shall send an email, and at the same time save the data in SQL. The dropdownlists shall have the Name.property from the children nodes of a specific node (that i select via a content-picker)
The way I'm trying to do it is to create an usercontrol in which i put the ASP.NET form. My problem is databinding the dropdown-lists with the Razor-code in which i make the list of children-nodes
I've found numerous articles on the internet, but I think i'm having trouble passing the razor-data to ASP.Net
Another way to do it, could be to get the XML from @Model.NodebyID() passed, and then create the list directly in ASP.Net
I hope you can help me, as I've run out of ideas. And if you have another way of doing the above I'm also interested :-)
Have a look at this post :) http://our.umbraco.org/forum/developers/razor/22305-ASPNET-Usercontrol-inside-Razor-script#comment83834
Jeroen
I've actually been reading that post like a million times :-( but I don't see how that helps me with binding the dropdownlist with the list created in Razor.
How can I transfer the variable i've created in Razor and use it to databind the Dropdownlist in the usercontrol.
It might just be me that is not getting it, but if someone could make it clear even for me I would greatly appreciate it
Jimmy
Does this post help? There I set a value in the usercontrol and get it in razor.
Jeroen
I need to test, but isn't it the opposite that I'm trying to do?
Jimmy
Hmm you're not trying to databind values in Razor with code from a UserControl? Isn't it easier to also have the dropdownlist as a webcontrol in the usercontrol? That's easier to databind and to get the values in the postback. If you're going to test and you use 4.7.1 than you can use RenderEvent="PreRender" on the macro. That way the page load of the UserControl is hit first.
Jeroen
Do you have a code-example I can see? I'm the closest thing to a newbie regarding usercontrols and Razor, so I'm having trouble imagining what you are writing
Jimmy
Here are some pieces of code. Hope it helps.
Usercontrol frontend .ascx
Usercontrol backend .ascx.cs
The razor file .cshtml
It's a bit messy and I removed a lot of code, but I hope you get the point :-).
Jeroen
Hi Jeroen
I think I'm more confused now than before :-( But as far as I can comprehend, then I'm trying to do the opposite of what you are describing.
I need to be able to use a variable created in Razor (containing the list of childrens for a specific node) in the code-behind of the Usercontrol. This list I want to bind to a dropdownlist.
Jimmy
I've never tried getting a razor value in a usercontrol, but I think it's possible. If you have an input field on a form in razor and you cause a postback the value should be here:
Jeroen
Hi Jeroen
I don't have the form in Razor. I have the form in the Usercontrol. Let me paste some code - that might be able to better clarify what I want to do:
The usercontrol.ascx:
And then on the template showing this usercontrol I have the code creating the foreach-loop:
How do i use this foreach-loop to databind the Dropdown-lists?
Jimmy
Did you know you can also use that code inside the usercontrol? It's much better to just create a DropDownList there as a webcontrol and databind it with what you got there. Something like this can be done in the code behind of your usercontrol:
Jeroen
That works perfect :-)
However the above code is C# - is the same thing possible if I've chosen VB as language?
Jimmy
Yes that should also be possible in VB. I'm no VB expert, but I used an online converter. You might need to change some stuff.
Jeroen
If you never need more examples (in C#) you can find them in this wiki: http://our.umbraco.org/wiki/reference/code-snippets/databind-node-children.
Jeroen
is working on a reply...