自定义 ASP.NET CreateUserWizard 中的 DropDownList 问题
我在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能否将填充
DropDownList
的代码放在!IsPostBack
下的Page_Load
方法中?可能是当您回发时,您的 DropDownList 被重新绑定,因此您总是获得第一个项目,并且您尝试将空字符串转换为 int ,这会给出错误消息。
Can you put the code that populates
DropDownList
inPage_Load
method under!IsPostBack
?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.