2014-09-22 50 views
0
HiddenField HFSN = new HiddenField(); 
    DataTable table= new DataTable(); 
    DataTable result=new DataTable(); 
    private int srno = 1; 
    private string headlines; 
    private string date; 
    Button btn = new Button(); 

    private const string _ascxPath = @"~/_CONTROLTEMPLATES/GridViewWP/GridView8/VisualWebPart1UserControl.ascx"; 
    private Microsoft.SharePoint.WebControls.SPGridView _grid; 

這是我的主要方法。 保護覆蓋無效的CreateChildControls(){ srno值遞增,但頁面刷新後,其值重新復位並顯示srno = 1。如何增加值

 btn.Visible = true; 
     btn.Text = "Show"; 
     btn.Click += new EventHandler(btn_Click); 
     _grid = new Microsoft.SharePoint.WebControls.SPGridView(); 
     _grid.AutoGenerateColumns = false; 
     _grid.AllowPaging = true; 
     _grid.PageSize = 5; 
     //_grid.PageIndexChanging += _grid_PageIndexChanging; 
     foreach (DataColumn column in table.Columns) 
      _grid.Columns.Add(new BoundField() 
      { 
       DataField = column.ColumnName, 
       HeaderText = column.ColumnName 
      }); 
     //_grid.DataSource = table; 
     this.Controls.Add(_grid); 
     // _grid.PagerTemplate = null; 
     //_grid.DataBind(); 
     _grid.Visible = true; 
     this.Controls.Add(btn); 
    } 

一次BTN點擊只顯示一行。每按一次按鈕,它就會顯示一個靜態添加在GetDataTable()中的新行。 ******空隙btn_Click(對象發件人,EventArgs的) {

 srno = Convert.ToInt32(HFSN.Value); 
     DataRow[] rows = table.Select(); 
     if (srno <= rows.Length) 
     { 
      for (int i = 0; i < rows.Length; i++) 
      { 

       if (rows[i]["srno"].ToString() == srno.ToString()) 
       { 

        headlines = rows[i]["headlines"].ToString(); 
        date = rows[i]["date"].ToString(); 
       } 

      } 

      if ((srno) == 1) 
      { 
       result.Columns.Add("srno", typeof(int)); 
       result.Columns.Add("date", typeof(string)); 
       result.Columns.Add("Headlines", typeof(string)); 
      } 
      result.Rows.Add(new object[] { srno, date, headlines }); 
     } 

     srno++; 
     //HFSN.Value =Convert.ToString(srno); 

     _grid.DataSource = result; 
      _grid.DataBind(); 
      // DataTable table = GetDataTable(); 

      _grid.Visible = true; 


    } *********** 



    private DataTable GetDataTable() 
    { 
     // DataTable result = new DataTable(); 

     table.Columns.Add("srno", typeof(int)); 
     table.Columns.Add("headlines", typeof(string)); 
     table.Columns.Add("date", typeof(System.DateTime)); 
     //for (int i = 1; i <= 5; i++) 
     table.Rows.Add(new object[] { 1, "Chinese Troops Begin Withdrawing from Ladakh Hours after Xi-Modi meet", "2014-09-22" }); 
     table.Rows.Add(new object[] { 2, "BJP-Shiv Sena Alliance of 25 years on verge of Collapse", "2014-09-21" }); 
     table.Rows.Add(new object[] { 3, "Meet THe Highest Female Executive In the World", "2014-09-22" }); 
     table.Rows.Add(new object[] { 4, "Indian Muslims will Live And Die For India,PM Says", "2014-09-21" }); 
     table.Rows.Add(new object[] { 5, "Two Doctors, Who got stuck in J&K floods, turn saviours for this town", "2014-09-21" }); 
     return table; 


    } 

回答

0
  1. 使用會話代替專用變量

  2. 使用asp.net隱藏的控件來存儲值。你會得到視角的好處。