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;
这是我的主要方法。
protected override void CreateChildControls()
{
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()中静态添加。 ****** void btn_Click(对象发件人,EventArgs e) {
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 :(得分:0)
使用会话代替私有变量
使用asp.net隐藏控件来存储值。你将从viewstate获益。