项目下拉列表的SelectedValue不是我选择的 [英] SelectedValue of item Dropdownlist not what I selected
本文介绍了项目下拉列表的SelectedValue不是我选择的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
单击链接按钮后,我的下拉列表的SelectedValue的默认值为2。无论我在下拉列表中选择什么,它仍然给我相同的价值。我做错了什么?
titleid 总是最终 2 ,第一个选项的价值下拉列表..
ASPX
I keep getting a default value of 2 for the SelectedValue of my dropdownlist upon clicking the link button. Whatever I choose in the dropdownlist it still gives me the same value. What am I doing wrong?
titleid always ends up being 2, the value of the first option of dropdownlist..
ASPX
<asp:GridView runat="server" ID="TitleView" OnRowDataBound="TitleView_RowDataBound" OnRowCommand="TitleView_OnRowCommand" AutoGenerateColumns="False">
<AlternatingRowStyle CssClass="even"></AlternatingRowStyle>
<Columns>
<asp:BoundField DataField="Fullname" HeaderText="Fullname" />
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="UserID" Value='<%# DataBinder.Eval(Container.DataItem, "UserID") %>' runat="server" />
<div class="input-append"><asp:DropDownList CssClass="span5" ID="TitleList" runat="server" />
<asp:LinkButton ID="lbnView" runat="server" Text="Update" CssClass="btn btn-primary" CommandName="ChangeCopyStatus"></asp:LinkButton></div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Codebehind
Codebehind
protected void Page_Load(object sender, EventArgs e)
{
bindTitleView();
}
public void bindTitleView()
{
//took out to save space
}
protected void TitleView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("TitleList");
using (SqlConnection conn = new SqlConnection(""))
{
SqlCommand cmd = new SqlCommand(@"SELECT ID, Name FROM EmployeeTitles ORDER BY Name ASC", conn);
conn.Open();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable myDataSet = new DataTable();
adp.Fill(myDataSet);
ddl.DataSource = myDataSet;
ddl.DataTextField = "Name";
ddl.DataValueField = "ID";
ddl.DataBind();
}
}
}
protected void TitleView_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("ChangeCopyStatus"))
{
GridViewRow currentrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
DropDownList ddlCopyStatus = (DropDownList)currentrow.FindControl("TitleList") as DropDownList;
HiddenField hfUserID = (HiddenField)currentrow.FindControl("UserID") as HiddenField;
int titleid = Convert.ToInt32(ddlCopyStatus.SelectedValue);
int userid = Convert.ToInt32(hfUserID.Value);
using (SqlConnection conn = new SqlConnection(""))
{
SqlCommand cmd = new SqlCommand(@"UPDATE [User] set Title2=@f1 where [UserID] = '" + userid + "'", conn);
conn.Open();
cmd.Parameters.Add("@f1", SqlDbType.Int).Value = titleid;
cmd.ExecuteNonQuery();
}
Response.Redirect("Title.aspx");
}
}
推荐答案
确保在Page_Load事件中测试!IsPostBack并且只进行数据绑定当它是页面的初始加载时。
Make sure to test for !IsPostBack in the Page_Load event and only do your databinding when it is the initial load of the page.
这篇关于项目下拉列表的SelectedValue不是我选择的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文