2013-10-03 88 views
0

我在以下事件中綁定girdiview:(subjectdropdown_SelectedIndexChanged) 我將follwing事件中的gridview作爲參數發送給另一個方法:Button1_click事件。GridView數據在將Gridivew傳遞給其他方法時爲null

protected void subjectdropdown_SelectedIndexChanged(object sender, EventArgs e) 
{ 
     DataTable getmarkfdb = inter.getmarksfromdatabaseothers(comp); 
     if (getmarkfdb.Rows.Count > 0) 
     { 
       TemplateField lable1 = new TemplateField(); 
       lable1.ShowHeader = true; 
       lable1.HeaderText = "AdmissionNumber"; 
       lable1.ItemTemplate = new gridviewtemplate(DataControlRowType.DataRow, "AdmissionNumber", "AdmissionNumber", "Label"); 
       studmarkgrid.Columns.Add(lable1); 

       TemplateField label2 = new TemplateField(); 
       label2.ShowHeader = true; 
       label2.HeaderText = "RollNumber"; 
       label2.ItemTemplate = new gridviewtemplate(DataControlRowType.DataRow, "RollNumber", "RollNumber", "Label"); 
       studmarkgrid.Columns.Add(label2); 

       TemplateField label3 = new TemplateField(); 
       label3.ShowHeader = true; 
       label3.HeaderText = "Name"; 
       label3.ItemTemplate = new gridviewtemplate(DataControlRowType.DataRow, "Name", "Name", "Label"); 
       studmarkgrid.Columns.Add(label3); 

       TemplateField extmep = new TemplateField(); 
       extmep.ShowHeader = true; 
       extmep.HeaderText = "ExternalMark"; 
       extmep.ItemTemplate = new gridviewtemplate(DataControlRowType.DataRow, "ExternalMark", "ExternalMark", "TextBox"); 

       studmarkgrid.Columns.Add(extmep); 

       studmarkgrid.DataSource = getmarkfdb; 
       studmarkgrid.DataBind(); 


     } 
} 

在GridView的TextBoxTemplate欄我填的是馬克的學生,在以下情況下我送GridView控件insertstumark從GridView中讀取數據的方法和保存到數據庫中。但在insertstumark方法中,gridview的行數據爲null。

protected void Button1_Click(object sender, EventArgs e) 
{ 
      comp.ACADAMICYEAR = acyeardropdown.SelectedItem.Text; 
      comp.MEDIUM = mediumdropdown.SelectedItem.Text; 
      string clas = classdropdown.SelectedItem.Text; 
      string[] cs = clas.Split('-'); 
      comp.CLASSNAME = cs[0].ToString(); 
      comp.SECTIONNAME =Convert.ToChar(cs[1].Trim().ToString()); 
      comp.EXAMNAMES = examnamedropdown.SelectedItem.Text; 
      comp.SUBJECTID = subjectdropdown.SelectedValue.ToString(); 
      // studmarkgrid.DataBind(); 
      // System.Web.UI.WebControls.GridView grid = studmarkgrid; 
      // System.Web.UI.WebControls.GridView grid = ViewState["stdmarkgrid"] as System.Web.UI.WebControls.GridView; 
      DataTable studtable = null; 
      // System.Web.UI.WebControls.GridView grid = (System.Web.UI.WebControls.GridView)ViewState["stdmarkgrid"]; 
      bool studm = inter.inserstumark(comp,stumarkgrid); 
    } 

什麼問題。我試圖在viewstate中存儲gridview。但在下面的行中它通過了錯誤。如何解決這個問題呢?

System.Web.UI.WebControls.GridView grid = ViewState["stdmarkgrid"] as System.Web.UI.WebControls.GridView; 

回答

0

很多時候,如果數據是在頁面生命週期綁定到GridView爲時已晚,在GridView呈現,數據遺忘,所以在接下來的回傳,您將無法訪問GridView的底層數據源。

看起來像這裏發生了什麼。只需將getmarksfromdatabaseothers的結果保存到ViewState中,以便您可以根據需要再次訪問它。

+0

對不起。該值在getmarkfromdatabaseothers綁定到studmarkgrid後修改(這意味着我編輯gridview的ExternalMark值,然後我調用Button1_click事件。因此,如果我保存在視圖狀態getmarkfromothers值,那麼它會給我舊數據未編輯數據。我的問題。 – baskaran

相關問題