<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>
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"); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)