开发者

Problemswith DropDownList in customized ASP.NET CreateUserWizard

I am using a customized ASP.NET CreateUserWizard in my web application.

Here I used a dropdownlist to populate the countries when user registering. In the page load its populate the countries as expected.

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

But when I trying to obtain the values from dropdownlist in the OnCreatedUser event it generating me an error saying

System.FormatException: Input string was not in a correct format

What I am doing in the is OnCreatedUser is

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

var nationality = Convert.ToIn开发者_如何学编程t32(national.SelectedValue); <<-(where the error is)

The complete code of the page is below

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

}

Any ideas will be appreciate. Thanks


Can you put the code that populates DropDownList in Page_Load method under !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);
    }
}

It could be that when you postback, your DropDownList gets re-bound, so you always get the first item and you tried to convert empty string to an int which gives you the error message.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜