[英]ASP.NET and C# - Get selected item in static DropDownList
I'm new to ASP.NET and I'm having problem with getting selected item in static DropDownList. 我是ASP.NET的新手,但是在静态DropDownList中获取所选项目时遇到问题。 This is the structure of my list: 这是我的清单的结构:
<asp:DropDownList ID="txtGender" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
<asp:ListItem>Rather not say</asp:ListItem>
</asp:DropDownList>
Then in the code where I want to get the selected item, I use txtGender.SelectedItem
and always get the first item as input. 然后在要获取所选项目的代码中,我使用txtGender.SelectedItem
并始终获取第一个项目作为输入。 I have another DropDownList where I have to fetch the items from database, then I use IsPostBack
before calling DataBind()
and it works fine. 我还有另一个DropDownList,必须从数据库中获取项目,然后在调用DataBind()
之前使用IsPostBack
,它工作正常。 So I'm wondering how should I use IsPostBack
in static DropDownList as above? 所以我想知道如何在上面的静态DropDownList中使用IsPostBack
? Please help. 请帮忙。
* EDITED * 编辑
Below is the method that I use the txtGender
: 下面是我使用txtGender
的方法:
protected void btnSave_Click(object sender, EventArgs e)
{
int uID= int.Parse(Session["uID"].ToString());
PhotoDataSetTableAdapters.MembersTableAdapter memAdap = new PhotoDataSetTableAdapters.MembersTableAdapter();
PhotoDataSet.MembersDataTable memTable = memAdap.GetMemberByID(uID);
DataRow[] dr = memTable.Select("userID = " + uID);
string Fname = memTable.AsEnumerable().Single().Field<String>("avatar");
if(PhotoUploader.HasFile)
{
Fname = Path.GetFileName(PhotoUploader.FileName);
PhotoUploader.SaveAs(Server.MapPath("~/avatar/") + Fname);
}
string newPass = memTable.AsEnumerable().Single().Field<String>("password");
if (txtPass.Text != "")
{
newPass = txtPass.Text;
}
string tmp = txtBday.Text;
DateTime bday = DateTime.ParseExact(tmp, "dd/MM/yyyy", null);
memAdap.UpdateMemberProfile(newPass, txtEmail.Text, txtRealname.Text, txtLocation.Text, txtGender.SelectedItem.Text, txtBio.Text, bday, Fname, uID);
//Response.Write(txtGender.SelectedItem.Text + " " + txtBio.Text);
Response.Redirect("Photostream.aspx");
}
Ps: I tried using Response.Write
to see the input for gender but it's always the first item of the list to be selected. 附:我尝试使用Response.Write
来查看性别输入,但这始终是列表中的第一项。 I also have no input for txtBio
even though everything seems alright. 即使一切似乎都不错,我也没有输入txtBio
。
If you are not rebinding the options for the drop down and you are not resetting its actual value on post back, you should not have to do anything with IsPostBack
. 如果您不重新绑定下拉菜单的选项,并且也没有在回发时重置其实际值,则您不必对IsPostBack
进行任何操作。 You should just be able to do the following to get the value and text: 您应该能够执行以下操作以获取值和文本:
string value = txtGender.SelectedValue;
string text = txtGender.SelectedItem.Text;
Note: the actual value of a control will not be available during Page_Init since the page has not been completely initialized yet. 注意:由于页面尚未完全初始化,因此控件的实际值在Page_Init期间将不可用。
You need to assign unique values to the ListItems in your DropDownList: 您需要为DropDownList中的ListItems分配唯一值:
<asp:DropDownList ID="txtGender" runat="server">
<asp:ListItem Value="0">Male</asp:ListItem>
<asp:ListItem Value="1">Female</asp:ListItem>
<asp:ListItem Value="2">Other</asp:ListItem>
<asp:ListItem Value="3">Rather not say</asp:ListItem>
</asp:DropDownList>
Try this and see if it helps. 试试这个,看看是否有帮助。
Bind the dropdown on page load and check the IsPostBack property 绑定页面加载下拉列表并检查IsPostBack属性
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Dropdown binding code goes here....
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.