自定义ASP.NET CreateUserWizard中的DropDownList问题

时间:2011-09-26 10:25:36

标签: c# asp.net .net-4.0 drop-down-menu createuserwizard

我在网络应用程序中使用自定义的ASP.NET CreateUserWizard

我在用户注册时使用dropdownlist填充国家/地区。在page load中按预期填充国家/地区。

var query = GetNationality();
var national = (DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Nationality");
national.DataSource = query;
national.DataTextField = "CountryName";
national.DataValueField = "Id";
national.DataBind();

var item = new ListItem("Select Country", "");
national.Items.Insert(0, item);

但是当我尝试从dropdownlist事件中OnCreatedUser获取值时,会产生错误,说明

  

System.FormatException:输入字符串的格式不正确

我在OnCreatedUser中所做的是

var national = (DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Nationality");

var nationality = Convert.ToInt32(national.SelectedValue); <<-(where the error is)

该页面的完整代码如下

protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
            FillDropdown();
        }

    }
    protected void RegisterUser_CreatedUser(object sender, EventArgs e)
    {
        var newUser = Membership.GetUser(RegisterUser.UserName);
        var newUserId = (Guid)newUser.ProviderUserKey;

        var name1 = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("fname");
        var name2 = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("lname");
        var comp = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Company");
        var post = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Position");
        var birth = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Bday");
        var mob = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Mobile");
        var aphone = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("altPhone");
        var aemail = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("altEmail");
        var national = (DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Nationality");      
        var news = (CheckBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Newsletter");

        var title = tit.Text.Trim();        
        var nationality = national.Text;
        var preferred = method.Text.Trim();
        var newsleter = news.Checked;


        FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);

        var continueUrl = RegisterUser.ContinueDestinationPageUrl;
        if (String.IsNullOrEmpty(continueUrl))
        {
            continueUrl = "~/";
        }
        Response.Redirect(continueUrl);
    }

    public void FillDropdown()
    {
        var query = GetNationality();
        var national = (DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Nationality");
        national.DataSource = query;
        national.DataTextField = "CountryName";
        national.DataValueField = "Id";
        national.DataBind();
        var item = new ListItem("Select Country", "");
        national.Items.Insert(0, item);
    }

}

任何想法都会受到赞赏。 感谢

1 个答案:

答案 0 :(得分:1)

您能否在DropDownList下的Page_Load方法中填充!IsPostBack的代码?

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        var query = GetNationality();
        var national = (DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Nationality");
        national.DataSource = query;
        national.DataTextField = "CountryName";
        national.DataValueField = "Id";
        national.DataBind();

        var item = new ListItem("Select Country", "");
        national.Items.Insert(0, item);
    }
}

可能是当你回发时,你的DropDownList会被重新绑定,所以你总是得到第一个项目,并且你试图将空字符串转换为一个int,它会给你错误信息。