根据下拉列表选择值更新MemberType属性

时间:2014-05-13 15:00:53

标签: c# umbraco

我在MemberType下创建了一个名为" testField"它使用自定义DataType下拉列表,其中包含2个值(Employee和Subcontractor)。我希望会员能够选择一个,提交表格并将他们的选择存储在这个属性下,但我似乎无法弄明白。

是否可以在后端手动更改选择的同时执行此操作?

这是我的代码......

ASCX

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Register.ascx.cs" Inherits="nForum.usercontrols.nForum.membership.Register" %>

    <div id="forumregistration" class="validate">
            <dl class="form">
                <dt><label for="<%= tbLoginName.ClientID %>">Login/Username:</label></dt>
                <dd><asp:TextBox ToolTip="Enter username" CssClass="required" ID="tbLoginName" runat="server" /></dd>

                <dt><label for="<%= tbName.ClientID %>">Full Name:</label></dt>
                <dd><asp:TextBox ToolTip="Enter name" CssClass="required" ID="tbName" runat="server" /></dd>

                <dt><label for="<%= tbEmail.ClientID %>">Email:</label></dt>
                <dd><asp:TextBox ToolTip="Enter email address" CssClass="required email" ID="tbEmail" runat="server" /></dd>

                <dt><label for="<%= tbPassword.ClientID %>">Password:</label></dt>
                <dd><asp:TextBox ToolTip="Enter a password" CssClass="required password" ID="tbPassword" TextMode="Password" runat="server" /></dd>

                <dt><label for="">Status:</label></dt>
                <asp:dropdownlist ID="tbOrganisation" runat="server">
                    <asp:ListItem Text="Choose" Value="0"></asp:ListItem>
                    <asp:ListItem Text="Employee" Value="1"></asp:ListItem>
                    <asp:ListItem Text="Subcontractor" Value="2"></asp:ListItem>
                </asp:dropdownlist>
                <dt> </dt>
                <dd><asp:Button ID="btnSubmit" CssClass="textarea" runat="server" Text="Create Account" onclick="BtnSubmitClick" /></dd>

            </dl>
    </div>

CS

using System;
using System.Text;
using System.Web.Security;
using nForum.BusinessLogic;
using umbraco;
using umbraco.cms.businesslogic.member;

namespace nForum.usercontrols.nForum.membership
{
    public partial class Register : BaseForumUsercontrol
    {

        protected void BtnSubmitClick(object sender, EventArgs e)
        {
            var redirecturl = Settings.Url;

            // Check the user isn't already registered
            if (Member.GetMemberFromEmail(Helpers.GetSafeHtml(tbEmail.Text)) == null & Member.GetMemberFromLoginName(Helpers.GetSafeHtml(tbLoginName.Text)) == null)
            {
                // Set the member type and group
                var mt = MemberType.GetByAlias(MembershipHelper.ForumUserRoleName);
                var addToMemberGroup = MemberGroup.GetByName(MembershipHelper.ForumUserRoleName);

                //create a member
                var m = Member.MakeNew(Helpers.GetSafeHtml(tbName.Text), mt, new umbraco.BusinessLogic.User(0));

                //var mstatus = new MembershipCreateStatus();
                //var mp = Membership.CreateUser(tbName.Text, tbPassword.Text, tbEmail.Text, string.Empty, string.Empty, true, out mstatus);

                // Set the other properties
                m.Email = Helpers.GetSafeHtml(tbEmail.Text);
                m.LoginName = Helpers.GetSafeHtml(tbLoginName.Text);
                m.Password = Helpers.GetSafeHtml(tbPassword.Text);
                // Add 0 Karma to user, helps us later in the site
                m.getProperty("forumUserKarma").Value = 0;
                m.getProperty("forumUserAllowPrivateMessages").Value = 1;
                m.getProperty("forumUserLastPrivateMessage").Value = DateTime.Now;

                // Take selected dropdown value and store
                m.getProperty("testField").Value = tbOrganisation;

                //##### Manual Member Authorisation #####
                // If this is not enabled, mark the member as authorised
                if (!Settings.ManuallyAuthoriseNewMembers)
                {
                    m.getProperty("forumUserIsAuthorised").Value = 1;
                }

                m.AddGroup(addToMemberGroup.Id);

                //Save member
                m.Save();

                //Generate member Xml Cache
                m.XmlGenerate(new System.Xml.XmlDocument());

                if (!Settings.ManuallyAuthoriseNewMembers)
                {
                    //Login the user so they can be redirected to their profile page
                    FormsAuthentication.SetAuthCookie(tbLoginName.Text, false);
                }
                else
                {
                    redirecturl = string.Concat(CurrentPageAbsoluteUrl, "?m=", library.GetDictionaryItem("NotifiedWhenAccountAuth"));
                }

                // If admins wants email notification, then send it here
                if (Settings.EmailAdminOnNewMemberSignUp)
                {
                    SendAdminNotification(m);
                }
            }
            else
            {
                redirecturl = string.Concat(CurrentPageAbsoluteUrl, "?m=", library.GetDictionaryItem("UserAlreadyExists"));
            }

            // Now redirect to the correct page
            Response.Redirect("/discuss-it");

        }

        private void SendAdminNotification(Member newmember)
        {
            var sb = new StringBuilder();
            sb.AppendFormat(library.GetDictionaryItem("MemberSignUpEmailText"),
                Settings.Name,
                newmember.LoginName,
                newmember.Text,
                newmember.Email);
            Helpers.SendMail(Settings.EmailNotification, Settings.EmailAdmin, library.GetDictionaryItem("NewMemberSignUp"), sb.ToString());
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您的具体问题有两个方面:

1)您要将m.getProperty("testField")设置为DropDownList的实例 而不是SelectedValue

2)正如对问题的评论所述,您的硬编码DropDownList值不匹配 以及DataType属性的Umbraco testField中存储的值。

您可以通过将DropDownList与Umbraco调用的内容绑定来更有力地解决第二个问题 存储在PreValues数据类型上的testField将确保DropDownList中的可用选项 匹配DataType

上定义的选项

编写本文时,PreValues将作为XML片段存储在数据类型定义中 格式如下:

<preValues>
    <preValue id="1">Option 1</preValue>
    <preValue id="2">Option 2</preValue>
    <preValue id="3">Option 3</preValue>
</preValues>

以编程方式检索这些有点笨重(IMO);首先,您需要获取Id的{​​{1}} 将鼠标悬停在umbraco界面的DataType上并查看状态栏(您应该会看到DataType javascript:openDataType(1111) 1111之类的内容。

接下来调用Id方法,传入上面获得的umbraco.library.GetPreValues()。这种方法 返回Id,您可以使用该XPathNodeIterator以适合您需要的方式获取值,例如创建可以绑定到Dictionary<string,string>的{​​{1}}。以下示例取自Damiaan Peeters blog 并且稍微适应了。

DropDownList

然后在您的代码中,您可以执行以下操作:

private static Dictionary<string, string> GetPreValues(int dataTypeId)
{
    XPathNodeIterator preValueRootElementIterator = umbraco.library.GetPreValues(dataTypeId);
    preValueRootElementIterator.MoveNext(); //move to first 
    XPathNodeIterator preValueIterator = preValueRootElementIterator.Current.SelectChildren("preValue", "");
    var retVal = new Dictionary<int, object>();

    while (preValueIterator.MoveNext())        
        retVal.Add(preValueIterator.Current.GetAttribute("id", ""), preValueIterator.Current.Value);

    return retVal;
}