Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
<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
C#
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");
            }
        }
Posted
Comments
ZurdoDev 31-Jan-14 11:59am    
Check for IsPostBack in your page load. That will fire before other post back events.
Mike Calerbosi 31-Jan-14 12:07pm    
Omg, wow I can't believe I missed that. Thank you that did it, you are a god amongst men.
ZurdoDev 31-Jan-14 12:10pm    
You're welcome. I'll post as solution then.
bowlturner 31-Jan-14 12:07pm    
Where is you're ddlCopyStatus defined? It's not displayed.
Mike Calerbosi 31-Jan-14 12:08pm    
I defined it here "DropDownList ddlCopyStatus = (DropDownList)currentrow.FindControl("TitleList") as DropDownList;"

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900