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

发布于 2024-12-07 02:38:03 字数 3380 浏览 3 评论 0原文

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

在这里,我使用 dropdownlist 在用户注册时填充国家/地区。在页面加载中,它会按预期填充国家/地区。

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

但是,当我尝试从 OnCreatedUser 事件中的 dropdownlist 获取值时,它会生成一个错误,提示我

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

}

任何想法将不胜感激。 谢谢

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.ToInt32(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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扶醉桌前 2024-12-14 02:38:03

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

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 ,这会给出错误消息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文