Как получить выпадающее( которое является динамическим) значение, присутствующее внутри grid-view в функции нажатия кнопки?
Hi,i have added dynamic drop down list to the grid-view columns starting from 5th column in row databound.Now i want to save those dropdown values to the database using button click function.When i tried doing so,by getting the droodown selected value in button click ,its taking null value.Please help me out in this.
Что я уже пробовал:
protected void Button1_Click(object sender, EventArgs e) { foreach (GridViewRow row in GridView1.Rows) { // int devopsid = (int)GridView1.DataKeys[row.RowIndex].Value; if (row.RowType == DataControlRowType.DataRow) { // string ddlValue = row.Cells[5].Text; // foreach (DropDownList ddl in GridView1.Rows) // { // for (int j = 0; j < GridView1.Rows.Count; j++) // { for (int i = 5; i < row.Cells.Count; i++) { //string str = ht[devopsid].ToString(); GridView1_ddl5_0 //var ddl1 = (DropDownList)dataRow.Cells[3].FindControl("ddl1"); DropDownList ddl1 = row.Cells[i].FindControl("ddl") as DropDownList; // for (int i = 5; i < row.Cells.Count; i++) { // DropDownList ddl = (DropDownList)((Control)).NamingContainer.FindControl("ddl"); // String selectedValue = ddl.SelectedValue; if (GridView1.HeaderRow.Cells[i].Text == DropDownList1.SelectedItem.Text.Trim()) { string query = " update devopstable set " + GridView1.HeaderRow.Cells[i].Text + " = '"+ddl1.SelectedItem.Value+"' "; SqlCommand cmd = new SqlCommand(query, cnn1); cnn1.Open(); // cmd.Parameters.AddWithValue("@devopsid", devopsid); cmd.ExecuteNonQuery(); cnn1.Close(); } //} } } // } } }
и под это событие связано строки.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { // int devopsid = Convert.ToInt16(GridView1.DataKeys[RowIndex].Values["devopsid"].ToString()); if (e.Row.RowType == DataControlRowType.DataRow) { for (int i = 5; i < e.Row.Cells.Count; i++) { DropDownList ddl = new DropDownList(); ddl.ID = "ddl" + i; // ddl.SelectedIndex = 0; ddl.Items.Add("Yes"); ddl.Items.Add("No"); ddl.DataBind(); e.Row.Cells[i].Controls.Add(ddl); } } }