Устранение неполадок при чтении динамически генерируемого поля шаблона в gridview с управлением меткой и выпадающим меню на кнопке сохранения.
I have taken a gridview with auto generatedcolumns is false. I am creating column of this gridview on run time by C# . I have createded columns of the gridview by datatable on OnRowCreated event of gridview . In each column of gridview there is some label control and a dropdown control . On Page Init I have bind the grid. Now grid is loaded with relevant data. But I am not able to read labels and dropdown control when want to save the selected dropdowns value by a button outside the grid.
Проблема, возникающая из-за переменного количества столбцов. Это зависит от запроса базы данных. При попытке найти элемент управления на кнопке сохранения вне сетки он выдает ошибку.
Что я уже пробовал:
охраняемых недействительными в(объект отправителя, EventArgs в электронной)
{
Набор Product_Sub = sub_get.Sp_Class_Subject_Get(school_id);
DataTable UniqueSubject = RemoveDuplicateRows(Product_Sub.Tables[0], "SUBJECT_NAME");
DataTable UniqueClass = Product_Sub.Tables[1];
CreateGridView(UniqueSubject);
Управления gridview1.Источник Данных = CreateDatatable(UniqueSubject, UniqueClass);
Управления gridview1.Привязку();
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (flagGenerateGrid) { if (e.Row.RowType == DataControlRowType.DataRow) { DataTable dt = new DataTable(); dt = (DataTable)ViewState["dt"]; DataTable dtProduct = new DataTable(); int counter = Convert.ToInt32(dt.Columns.Count); Label LblClass = new Label(); LblClass.ID = "LblClassName"; LblClass.Text = (e.Row.DataItem as DataRowView).Row[0].ToString(); e.Row.Cells[0].Controls.Add(LblClass); for (int column = 1; column < counter; column++) { Label LblClubId = new Label(); LblClubId.ID = "Lbl_Club_ID_" + column; LblClubId.Text = (e.Row.DataItem as DataRowView).Row[column].ToString(); LblClubId.Visible = false; e.Row.Cells[column].Controls.Add(LblClubId); string clubId = (e.Row.DataItem as DataRowView).Row[column].ToString(); string[] arrayId = clubId.Split(','); int classId = Convert.ToInt32(arrayId[0]); int productId = Convert.ToInt32(arrayId[1]); int subjectId = Convert.ToInt32(arrayId[2]); int subjectMasterId = Convert.ToInt32(arrayId[3]); Label LblClassId = new Label(); LblClassId.ID = "Lbl_CLASS_ID"; LblClassId.Text = Convert.ToString(classId); e.Row.Cells[column].Controls.Add(LblClassId); Label LblSubjectMasterId = new Label(); LblSubjectMasterId.ID = "Lbl_SUBJECT_MASTER_ID"; LblSubjectMasterId.Text = Convert.ToString(subjectMasterId); e.Row.Cells[column].Controls.Add(LblSubjectMasterId); Label LblAppId = new Label(); LblAppId.ID = "Lbl_APP_ID"; LblAppId.Text = Convert.ToString(productId); e.Row.Cells[column].Controls.Add(LblAppId); Label LblProductId = new Label(); LblProductId.ID = "Lbl_PRODUCT_ID"; LblProductId.Text = Convert.ToString(productId); e.Row.Cells[column].Controls.Add(LblProductId); Label LblSubjectId = new Label(); LblSubjectId.ID = "Lbl_SUBJECT_ID"; LblSubjectId.Text = Convert.ToString(subjectId); e.Row.Cells[column].Controls.Add(LblSubjectId); DropDownList DdlProduct = new DropDownList(); DdlProduct.ID = "DdlProductId"; DdlProduct.AppendDataBoundItems = true; DdlProduct.AutoPostBack = true; DdlProduct.DataTextField = "PRODUCT_NAME"; DdlProduct.DataValueField = "PRODUCT_ID"; DdlProduct.DataSource = null; DdlProduct.DataBind(); dtProduct = sub_get.Sp_Product_Subject_Class_Get(school_id, classId, subjectMasterId); DdlProduct.DataSource = dtProduct; DdlProduct.DataBind(); if (dtProduct.Rows.Count == 0) { LblClubId.Visible = false; LblClassId.Visible = false; LblSubjectMasterId.Visible = false; LblAppId.Visible = false; LblProductId.Visible = false; LblSubjectId.Visible = false; DdlProduct.Visible = false; } else { LblClubId.Visible = false; LblClassId.Visible = true; LblSubjectMasterId.Visible = true; LblAppId.Visible = true; LblProductId.Visible = true; LblSubjectId.Visible = true; DdlProduct.Visible = true; } DdlProduct.SelectedIndexChanged += DdlProductId_SelectedIndexChanged; e.Row.Cells[column].Controls.Add(DdlProduct); } } } }
protected void CreateGridView(DataTable dtSubject) { TemplateField field = new TemplateField(); PlaceHolder ph = new PlaceHolder(); field.HeaderText = "Class"; GridView1.Columns.Add(field); foreach (DataRow dr in dtSubject.Rows) { TemplateField tfield = new TemplateField(); tfield.HeaderText = Convert.ToString(dr["SUBJECT_NAME"]); GridView1.Columns.Add(tfield); } } protected DataTable CreateDatatable(DataTable dtSubject, DataTable dtClass) { try { DataTable dt = new DataTable(); dt.Columns.Add("Class", typeof(System.String)); foreach (DataRow dr in dtSubject.Rows) { dt.Columns.Add(Convert.ToString(dr["SUBJECT_NAME"]), typeof(System.String)); } DataRow DataRowNew; for (int row = 0; row < dtClass.Rows.Count; row++) { DataRowNew = dt.NewRow(); DataRowNew["Class"] = Convert.ToString(dtClass.Rows[row]["CLASS_NAME"]); for (int column = 0; column < dtSubject.Rows.Count; column++) { DataRowNew[Convert.ToString(dtSubject.Rows[column]["SUBJECT_NAME"])] = Convert.ToString(dtClass.Rows[row][0]) + "," + Convert.ToString(dtSubject.Rows[column]["PRODUCT_ID"]) + "," + Convert.ToString(dtSubject.Rows[column]["SUBJECT_ID"]) + "," + Convert.ToString(dtSubject.Rows[column]["SUBJECT_MASTER_ID"]); } //foreach (DataRow dr in dtSubject.Rows) //{ // DataRowNew[Convert.ToString(dr["SUBJECT_NAME"])] = Convert.ToString(dtClass.Rows[row][0]) + " " + Convert.ToString(dtSubject.Rows[row]["SUBJECT_ID"]); //} dt.Rows.Add(DataRowNew); } ViewState["dt"] = dt; return dt; } catch (Exception ex) { Elmah.ErrorSignal.FromCurrentContext().Raise(ex); throw ex; } }
protected void btn_Save_Click(object sender, EventArgs e) { DataTable dtGridViewBind = new DataTable(); DataColumn dtColumn; DataRow myDataRow; // Create SCHOOL_ID Column dtColumn = new DataColumn(); dtColumn.DataType = System.Type.GetType("System.Int32"); dtColumn.ColumnName = "SCHOOL_ID"; dtColumn.Caption = "SCHOOL_ID"; dtColumn.ReadOnly = false; dtGridViewBind.Columns.Add(dtColumn); // Create APP_ID Column dtColumn = new DataColumn(); dtColumn.DataType = System.Type.GetType("System.Int32"); dtColumn.ColumnName = "PRODUCT_ID"; dtColumn.Caption = "PRODUCT_ID"; dtColumn.ReadOnly = false; dtGridViewBind.Columns.Add(dtColumn); // Create SUBJECT_ID Column dtColumn = new DataColumn(); dtColumn.DataType = System.Type.GetType("System.Int32"); dtColumn.ColumnName = "SUBJECT_ID"; dtColumn.Caption = "SUBJECT_ID"; dtColumn.ReadOnly = false; dtGridViewBind.Columns.Add(dtColumn); // Create SUBJECT_MASTER_ID Column dtColumn = new DataColumn(); dtColumn.DataType = System.Type.GetType("System.Int32"); dtColumn.ColumnName = "SUBJECT_MASTER_ID"; dtColumn.Caption = "SUBJECT_MASTER_ID"; dtColumn.ReadOnly = false; dtGridViewBind.Columns.Add(dtColumn); // Create CREATED_BY Column dtColumn = new DataColumn(); dtColumn.DataType = System.Type.GetType("System.Int32"); dtColumn.ColumnName = "CREATED_BY"; dtColumn.Caption = "CREATED_BY"; dtColumn.ReadOnly = false; dtGridViewBind.Columns.Add(dtColumn); // Create APP_ID Column dtColumn = new DataColumn(); dtColumn.DataType = System.Type.GetType("System.Int32"); dtColumn.ColumnName = "NEW_PRODUCT_ID"; dtColumn.Caption = "NEW_PRODUCT_ID"; dtColumn.ReadOnly = false; dtGridViewBind.Columns.Add(dtColumn); // Create SUBJECT_ID Column dtColumn = new DataColumn(); dtColumn.DataType = System.Type.GetType("System.Int32"); dtColumn.ColumnName = "NEW_SUBJECT_ID"; dtColumn.Caption = "NEW_SUBJECT_ID"; dtColumn.ReadOnly = false; dtGridViewBind.Columns.Add(dtColumn);
try { //for (int i = 0; i < GridView1.Columns.Count; i++) //{ // DropDownList dl = (DropDownList)GridView1.Rows[i].FindControl("DdlProductId"); // for (int j = 0; j < GridView1.Rows.Count; j++) // { // Label l1 = (Label)GridView1.Rows[i].Cells[2].FindControl("lbl" + j); // TextBox t = (TextBox)GridView1.Rows[i].Cells[3].FindControl("txt" + j); // } //} for (int rows = 1; rows < GridView1.Rows.Count; rows++) { for (int column = 1; column < GridView1.Rows.Count; column++) { Label Lbl_SUBJECT_MASTER_ID = (Label)GridView1.Rows[column].FindControl("Lbl_SUBJECT_MASTER_ID_" + (rows-1)); int subjectMasterId = Convert.ToInt32(Lbl_SUBJECT_MASTER_ID.Text); //Label l1 = (Label)GridView1.Rows[column].FindControl("Lbl_SUBJECT_MASTER_ID"); //int sId = Convert.ToInt32(l1.Text); //DropDownList DdlProductId = (DropDownList)GridView1.Rows[column].FindControl("DdlProductId"); //int productId = Convert.ToInt32(DdlProductId.SelectedValue); //Label Lbl_SUBJECT_MASTER_ID = (Label)GridView1.Rows[rows].FindControl("Lbl_SUBJECT_MASTER_ID_"+ rows); //Label Lbl_SUBJECT_MASTER_ID = (Label)row.FindControl("Lbl_SUBJECT_MASTER_ID"); //Label Lbl_PRODUCT_ID = (Label)row.FindControl("Lbl_PRODUCT_ID"); //Label Lbl_APP_ID = (Label)row.FindControl("Lbl_APP_ID"); //DropDownList DropDownList1 = (DropDownList)row.FindControl("DdlProductId"); //Label Lbl_SUBJECT_ID = (Label)row.FindControl("Lbl_SUBJECT_ID"); //int subjectMasterId = Convert.ToInt32(Lbl_SUBJECT_MASTER_ID.Text); //int productId = Convert.ToInt32(Lbl_PRODUCT_ID.Text); //int appId = Convert.ToInt32(Lbl_APP_ID.Text); //int subjectId = Convert.ToInt32(Lbl_SUBJECT_ID.Text); } } //foreach (GridViewRow row in GridView1.Rows) //{ // Label Lbl_SUBJECT_MASTER_ID = (Label)row.FindControl("Lbl_SUBJECT_MASTER_ID"); // Label Lbl_CREATED_BY = (Label)row.FindControl("Lbl_CREATED_BY"); // Label Lbl_PRODUCT_ID = (Label)row.FindControl("Lbl_PRODUCT_ID"); // Label Lbl_APP_ID = (Label)row.FindControl("Lbl_APP_ID"); // DropDownList DropDownList1 = (DropDownList)row.FindControl("DdlProductId"); // Label Lbl_NEW_SUBJECT_ID = (Label)row.FindControl("Lbl_NEW_SUBJECT_ID"); // Label Lbl_SUBJECT_ID = (Label)row.FindControl("Lbl_SUBJECT_ID"); // DataSet Product_Get = sub_get.Sp_Product_Get(school_id); // DataView dt2 = Product_Get.Tables[0].DefaultView; // dt2.RowFilter = "PRODUCT_ID = '" + DropDownList1.SelectedValue + "'"; // DataRow[] rowSubject = Product_Get.Tables[0].Select("PRODUCT_ID=" + DropDownList1.SelectedValue + " and SUBJECT_MASTER_ID =" + Lbl_SUBJECT_MASTER_ID.Text); // if (Convert.ToString(Lbl_PRODUCT_ID.Text) != Convert.ToString(Lbl_APP_ID.Text)) // { // myDataRow = dtGridViewBind.NewRow(); // myDataRow["SCHOOL_ID"] = school_id; // myDataRow["SUBJECT_ID"] = Lbl_SUBJECT_ID.Text; // myDataRow["PRODUCT_ID"] = Convert.ToString(Lbl_APP_ID.Text); // myDataRow["SUBJECT_MASTER_ID"] = Lbl_SUBJECT_MASTER_ID.Text; // myDataRow["CREATED_BY"] = Login_ID; // myDataRow["NEW_PRODUCT_ID"] = Lbl_PRODUCT_ID.Text; // myDataRow["NEW_SUBJECT_ID"] = Lbl_NEW_SUBJECT_ID.Text; // dtGridViewBind.Rows.Add(myDataRow); // } //} //sub_get.Sp_Product_Saved(dtGridViewBind); //DataTable Product_Sub1 = sub_get.Sp_Product_Subject_Get(school_id); //DataTable UniqueRecords = RemoveDuplicateRows(Product_Sub1, "SUBJECT_NAME"); //GridView1.DataSource = UniqueRecords; //GridView1.DataBind(); //Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "myFunction('Saved')", true); } catch (Exception ex) { Elmah.ErrorSignal.FromCurrentContext().Raise(ex); throw ex; } }
Когда я пытаюсь найти значение метки или выбранное значение в выпадающем списке . Я получаю ошибку, что "object refrence не установлен в экземпляр объекта". Пожалуйста, помогите мне. Заранее спасибо.