Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jimmy Dan Mortensen 77 posts 197 karma points
    Jan 09, 2012 @ 14:35
    Jimmy Dan Mortensen
    0

    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 :-)

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 09, 2012 @ 14:54
  • Jimmy Dan Mortensen 77 posts 197 karma points
    Jan 09, 2012 @ 15:01
    Jimmy Dan Mortensen
    0

    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

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 09, 2012 @ 15:04
    Jeroen Breuer
    0

    Does this post help? There I set a value in the usercontrol and get it in razor.

    Jeroen

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Jan 09, 2012 @ 15:08
    Jimmy Dan Mortensen
    0

    I need to test, but isn't it the opposite that I'm trying to do?

    Jimmy

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 09, 2012 @ 15:15
    Jeroen Breuer
    0

    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

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Jan 09, 2012 @ 15:28
    Jimmy Dan Mortensen
    0

    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

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 09, 2012 @ 15:47
    Jeroen Breuer
    0

    Here are some pieces of code. Hope it helps.

    Usercontrol frontend .ascx

    <div class="left">
        <div id="projecten-zoeken">
            <div class="formProjects">
                <span class="field">
                <label for="woning">Ik zoek een project in</label><br />
                <asp:TextBox ID="TxtAddress" runat="server" CssClass="inputText"></asp:TextBox>
                </span>
                <span class="selectList">
                    <label for="straal" class="margin">Straal</label><br />
                    <asp:DropDownList ID="DdlDistance" runat="server" />
                </span>
                <span class="checkboxList">
                <label for="status">Status</label><br />
                <asp:CheckBoxList ID="CheckStatus" runat="server" />
                </span>
            </div>
            <asp:Button ID="BtnSend" runat="server" Text="Zoeken" OnClick="BtnSend_Click" ValidationGroup="SearchPortal" CssClass="Zoeken" />
        </div>
    
        <div id="objects" class="projecten">
            <umbraco:Macro ID="RenderSearchProjects" runat="server" RenderEvent="PreRender" filelocation="~/macroScripts/portal/SearchProjects.cshtml"/>
        </div>
    
    </div>

    Usercontrol backend .ascx.cs

    protected void BtnSend_Click(object sender, EventArgs e)
    {
            DynamicNodeList projectNodes = new DynamicNodeList();
    
            //Get the search results.
            ControllerLayer.SearchProjects(address, maxDistance, "," + CheckStatus.GetSelectedValues().TrimEnd(',') + ",", out projectNodes);
    
            RenderSearchProjects.MacroAttributes["LatAddress"] = 100;
            RenderSearchProjects.MacroAttributes["LngAddress"] = 200;
            HttpContext.Current.Items["ProjectNodes"] = projectNodes;
    }

    The razor file .cshtml

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using System.Globalization;
    @using System.Linq;
    @using umbraco;
    @using umbraco.MacroEngines;
    @using Wanrooij.General.BO;
    @using DigibizAdvancedMediaPicker;
    
    @{  
        //These are the backbone project nodes.
        dynamic projectNodes = HttpContext.Current.Items["ProjectNodes"];
    
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
        <script type="text/javascript">
    
            var markers = [];
            var map;
            var lokaties = [];
    
                    @foreach (dynamic d in projectNodes)
                    {
                        var projectIcon = d.websitePhase.Contains("In voorbereiding") ? "/images/maps/building.png" : "/images/maps/building_geel.png";
    
                        @:lokaties.push(['@d.projectName', @d.GetProperty("location").Value, '@RenderLinks(d.GetProperty("portalProject").Value)', "@projectIcon"]);
                    }
        </script>
    }

    It's a bit messy and I removed a lot of code, but I hope you get the point :-).

    Jeroen

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Jan 10, 2012 @ 10:54
    Jimmy Dan Mortensen
    0

    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

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 10, 2012 @ 11:08
    Jeroen Breuer
    0

    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:

    System.Web.HttpContext.Current.Request.Form

    Jeroen

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Jan 10, 2012 @ 11:40
    Jimmy Dan Mortensen
    0

    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:

    <form runat="server">
    <table cellpadding="0" cellspacing="0" class="style1">
        <tr>
            <td>
                Vehicletype</td>
            <td>
                Brande</td>
            <td>
                Department</td>
            <td>
                Model</td>
        </tr>
        <tr>
            <td>
              <umbraco:macro runat="server" language="cshtml">
                <p>@Model.Up(1).menuLinie2</p>
              </umbraco:macro>
            </td>
            <td>
              <umbraco:macro runat="server" language="cshtml">
                <p>@Model.Up(1).menuLinie1</p>
              </umbraco:macro>
            </td>
            <td>
                <asp:DropDownList ID="DDLDepartment" runat="server">
                </asp:DropDownList>
            </td>
            <td>
                <asp:DropDownList ID="DDLModel" runat="server">
                </asp:DropDownList>
            </td>
        </tr>
    </table>
    <table cellpadding="2" class="style2" title="500px" cellspacing="3">
        <tr>
            <td valign="top" width="100px">
                Firstname
                <asp:RequiredFieldValidator ID="rfvFornavn" runat="server"
                    ControlToValidate="tbFornavn" ErrorMessage="Fornavn skal udfyldes"
                    SetFocusOnError="True">*</asp:RequiredFieldValidator>
            </td>
            <td width="10px">
                &nbsp;</td>
            <td class="style3" width="300px" colspan="3">
                <asp:TextBox ID="tbFornavn" runat="server" Width="400px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td valign="top" width="100px">
                Lastname
                <asp:RequiredFieldValidator ID="rfvEfternavn" runat="server"
                    ControlToValidate="tbEfternavn" ErrorMessage="Efternavn skal udfyldes"
                    SetFocusOnError="True">*</asp:RequiredFieldValidator>
            </td>
            <td width="10px">
                &nbsp;</td>
            <td class="style3" width="300px" colspan="3">
                <asp:TextBox ID="tbEfternavn" runat="server" Width="400px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td valign="top" width="100px">
               Adress
                <asp:RequiredFieldValidator ID="rfvAdresse" runat="server"
                    ControlToValidate="tbAdresse" ErrorMessage="Adresse skal udfyldes"
                    SetFocusOnError="True">*</asp:RequiredFieldValidator>
            </td>
            <td width="10px">
                &nbsp;</td>
            <td class="style3" width="300px" colspan="3">
                <asp:TextBox ID="tbAdresse" runat="server" Width="400px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td valign="top" width="100px">
                Zip
                <asp:RequiredFieldValidator ID="rfvPostnummer" runat="server"
                    ControlToValidate="tbPostnummer" ErrorMessage="Postnummer skal udfyldes"
                    SetFocusOnError="True">*</asp:RequiredFieldValidator>
            </td>
            <td width="10px">
                &nbsp;</td>
            <td class="style4" width="75px">
                <asp:TextBox ID="tbPostnummer" runat="server" Width="70px"></asp:TextBox>
            </td>
            <td class="style3" width="25px">
                Town
                <asp:RequiredFieldValidator ID="rfvBy" runat="server"
                    ControlToValidate="tbBynavn" ErrorMessage="Bynavn skal udfyldes"
                    SetFocusOnError="True">*</asp:RequiredFieldValidator>
            </td>
            <td class="style3" width="275px">
                <asp:TextBox ID="tbBynavn" runat="server" Width="265px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td valign="top" width="100px">
                Phone</td>
            <td width="10px">
                &nbsp;</td>
            <td class="style3" width="300px" colspan="3">
                <asp:TextBox ID="tbTelefon" runat="server" Width="400px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td valign="top" width="100px">
              Cell
                <asp:RequiredFieldValidator ID="rfvMobiltelefon" runat="server"
                    ControlToValidate="tbMobiltelefon"
                    ErrorMessage="Mobiltelefon skal udfyldes" SetFocusOnError="True">*</asp:RequiredFieldValidator>
            </td>
            <td width="10px">
                &nbsp;</td>
            <td class="style3" width="300px" colspan="3">
                <asp:TextBox ID="tbMobiltelefon" runat="server" Width="400px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td valign="top" width="100px">
                E-mail
                <asp:RequiredFieldValidator ID="rfvEmail" runat="server"
                    ControlToValidate="tbEmail" ErrorMessage="Email skal udfyldes"
                    SetFocusOnError="True">*</asp:RequiredFieldValidator>
            </td>
            <td width="10px">
                &nbsp;</td>
            <td class="style3" width="300px" colspan="3">
                <asp:TextBox ID="tbEmail" runat="server" Width="400px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td valign="top" width="100px">
                Dateto</td>
            <td width="10px">
                &nbsp;</td>
            <td class="style3" width="300px" colspan="3">
                <asp:Calendar ID="tbDato" runat="server" BackColor="White"
                    BorderColor="#999999" CellPadding="4" DayNameFormat="Shortest"
                    FirstDayOfWeek="Monday" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black"
                    Height="180px" ShowGridLines="True" TitleFormat="Month" Width="200px">
                    <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
                    <NextPrevStyle VerticalAlign="Bottom" />
                    <OtherMonthDayStyle ForeColor="#808080" />
                    <SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
                    <SelectorStyle BackColor="#CCCCCC" />
                    <TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True"
                        Font-Names="Verdana" Font-Overline="False" Font-Strikeout="False" />
                    <TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
                    <WeekendDayStyle BackColor="#FFFFCC" Font-Strikeout="True" ForeColor="#FFFFCC"
                        Width="0px" Wrap="False" />
                </asp:Calendar>
            </td>
        </tr>
        <tr>
            <td valign="top" width="100px">
                Time</td>
            <td width="10px">
                &nbsp;</td>
            <td class="style3" width="300px" colspan="3">
                <asp:TextBox ID="tbTid" runat="server" Width="400px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td valign="top" width="100px">
                Comment</td>
            <td width="10px">
                &nbsp;</td>
            <td class="style3" width="300px" colspan="3">
                <asp:TextBox ID="tbKommentar" runat="server" Height="80px" Width="400px"
                    TextMode="MultiLine"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td valign="top" width="100px">
                &nbsp;</td>
            <td width="10px">
                &nbsp;</td>
            <td class="style3" width="300px" colspan="3">
                <asp:Button ID="Button1" runat="server" Text="Button" />
            </td>
        </tr>
    </table>
    </form>

    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 = "";

      if (parent != null)
      {
        foreach (var item in parent.Children.Where("Visible && " + Afdeling))
        {
            if (strModeller != "")
            {
                strModeller += "|";
            }
            strModeller += @item.Name;
        };
      };
    }
        </umbraco:Macro>

    How do i use this foreach-loop to databind the Dropdown-lists?

    Jimmy

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 10, 2012 @ 11:51
    Jeroen Breuer
    4

    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();
    }

    Jeroen

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Jan 10, 2012 @ 13:09
    Jimmy Dan Mortensen
    0

    That works perfect :-)

    However the above code is C# - is the same thing possible if I've chosen VB as language?

    Jimmy

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 10, 2012 @ 13:12
    Jeroen Breuer
    0

    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

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 11, 2012 @ 06:53
    Jeroen Breuer
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft