将数据从telerik radgrid保存到数据库

时间:2011-05-27 16:44:33

标签: asp.net-ajax telerik radgrid telerik-grid

您好我使用telerik rad控制asp开发,我需要一些帮助。我有一个radGrid,当按下按钮时,它会填充数据,我在论坛中找到了一个解决方案,用于选择一行radGrid并获取每个单元格的值,但现在我想知道如何将这些值保存到我的数据库使用实体框架。我一直在尝试将值放入字符串,然后在我的保存方法中调用字符串,但是当我执行程序时,我添加了两个断点,一个在选择行的方法中取值,而另一个在保存方法,在第一个方法中查看断点的结果,该方法取所选行的值,每个字符串包含与radGrid中的单元格对应的数据,但在我的保存方法中,字符串显示为空,我该如何解决这个问题问题? 希望你能帮助我。 这是我选择和保存的两种方法

protected void RgSolic_SelectedIndexChanged(object sender, EventArgs e) 
   { 
       foreach (GridDataItem selectedItem in rgSolic.SelectedItems) 
       { 
           strNom = selectedItem["NombreCompleto"].Text; 
           strPuesto = selectedItem["nom_pue"].Text; 
           strCveAdscripcion = selectedItem["cve_adscripcion"].Text; 
           strArea = selectedItem["nom_area"].Text; 

       }  
   } 
   protected void btnRegistra_Click(object sender, EventArgs e) 
   { 
       //BD_SSEGUAEntities sseguaRegistro = new BD_SSEGUAEntities(); 
       //sseguaRegistro.spRegistraSolicitud1(); 
       //Valida Datos del Menor 
       using (var dbContext = new BD_SSEGUAEntities()) 
       { 
           var ResultSet = new Menor(); 
           var ResultSol = new Solicitud(); 
           var ResulBitacora = new Bitacora(); 
           if (chkMasc.Checked) 
           { 
               sexo = "M"; 
           } 
           if (chkFem.Checked) 
           { 
               sexo = "F"; 
           } 
           if (rdTxtAPatMen.Text != "" && rdTxtAMatMen.Text != "" && rdTxtNomMen.Text != "" && RadDatePicker1.SelectedDate != null && sexo != "" && rdCmbEdificio.SelectedValue != "" && rdTxtDomicilio.Text != "" && rdTxtHEnt.Text != "" && rdTxtHSal.Text != "" && rdTxtTelOfic.Text != "" && rdTxtExt.Text != "" && rdTxtPart.Text != "" && rdTxtCorreoE.Text != "" && rdTxtDomPart.Text != "" && rdTxtNHijos.Text != "" && rdCmbTEmbzo.SelectedValue != "0") 
           { 
               try 
               { 

                   System.DateTime.Now.Year.ToString(); 
                   //ResultSol.fcCvePuesto = rgSolic.Columns.FindByUniqueName("nom_pue").ToString(); 
                   //ResultSol.fcCveAdsc = rgSolic.Columns.FindByDataField("cve_adscripcion").ToString(); 
                   ResultSol.fcNomEmpleado = strNom; 

                            ResultSol.fcCveAdsc = strCveAdscripcion;

                   ResultSol.fcCvePuesto = strPuesto; 
                   ResultSol.fcDomLabora = rdTxtDomicilio.Text; 
                   ResultSol.fiHorEntLab = (Byte)rdTxtHEnt.Value; 
                   ResultSol.fiHorSalLab = (Byte)rdTxtHSal.Value; 
                   ResultSol.fiTelOfna = rdTxtTelOfic.Text; 
                   ResultSol.fiExtTel = rdTxtExt.Text; 
                   ResultSol.fiTelPart = rdTxtPart.Text; 
                   ResultSol.fcCorreoE = rdTxtCorreoE.Text; 
                   ResultSol.fcDomPart = rdTxtDomPart.Text; 
                   ResultSol.fiNumHijos = (Byte)rdTxtNHijos.Value; 

                   foreach (var menor in listaMenores) 
                   { 
                       ResultSol.Menor.Add(menor); 
                   } 
                   dbContext.AddToSolicitud(ResultSol); 
                   dbContext.SaveChanges(); 
                   //ResultSet.fcPatMenor = rdTxtAPatMen.Text; 
                   //ResultSet.fcMatMenor = rdTxtAMatMen.Text; 
                   //ResultSet.fcNomMenor = rdTxtNomMen.Text; 
                   //ResultSet.fdFchNacMenor = (DateTime)RadDatePicker1.SelectedDate; 
                   //ResultSet.fiAnosMenor = (Byte)rdTxtAnio.Value; 
                   //ResultSet.fiMesesMenor = (Byte)rdTxtMeses.Value; 
                   //ResultSet.fiSexoMenor = sexo; 
                   //ResultSet.fdFchRegMenor = DateTime.Today; 
                   //dbContext.Menores.AddObject(ResultSet); 
                   //dbContext.SaveChanges(); 
                   //RgMenor.DataSource = ResultSet; 
                   //RgMenor.Rebind(); 
               } 
               catch (Exception ex) 
               { 
                   Alerta(ex.Message); 
               } 
           } 
           else 
           { 
               Alerta("Captura todos lo datos del Menor"); 
           } 
       } 
   }

1 个答案:

答案 0 :(得分:0)

该代码不包含有关如何引用网格项内的文本框(rdTxtXXXXX)的部分。有两个选项,具体取决于它们是编辑行中的文本框还是模板列的项目模板(分别):

//inside the button click handler, textboxes in edited rows
//have in mind the detail about the edited items from [this doc][1]
foreach(GridEditableItem editedItem in rgSolic.EditItems)
{
  TextBox txtBox = editedItem.FindControl("rdTxtDomicilio") as TextBox;
  //retrieve the text here
  ...............................................
}

//inside the button click handler, textboxes in template columns' item templates
foreach(GridDataItem dataItem in rgSolic.MasterTableView.Items)
{
  TextBox txtBox = dataItem.FindControl("rdTxtDomicilio") as TextBox;
  //retrieve the text here
  ...............................................
}