GridView控件与寻呼机TopAndBottom [英] GridView with Pager TopAndBottom

查看:118
本文介绍了GridView控件与寻呼机TopAndBottom的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经扩展ASP.NET中的标准GridView控件。基本上它是一样的,但分页和排序是通过回调完成。

我已经成功地创建了自己的PagerTemplate,但是当PagerSettings.Position设为首页它仅适用。我想有该寻呼机在顶部和底部的两个

我得到一个HttpException说:该表必须包含在头部,身体,那么页脚定单行部分

任何人都知道这可能是什么?

我已经尝试调用MakeAccessible()方法,但这并不能帮助。

code:

 私人INT _IdColumnIndex = -1;公众覆盖对象的DataSource
{
  得到
  {
    返回HttpContext.Current.Session [this.ClientID +_DataSource];
  }
  组
  {
    如果(!value.GetType()。等于(typeof运算(数据视图)))
      抛出新的ArgumentException(只有System.Data.DataView被接受为DataGridView的数据源!);    HttpContext.Current.Session [this.ClientID +_DataSource] =值;
  }
}///<总结>
///设置默认设置。
///< /总结>
私人无效SetDefaultSettings()
{
  base.AllowPaging = TRUE;
  base.AllowSorting = TRUE;
  base.ShowHeader = TRUE;
  base.ShowFooter = TRUE;
  base.PagerTemplate =新GridPagerTemplate(PAGER_ID,这一点);
  base.PagerSettings.Position = PagerPosition.TopAndBottom;
  base.PageSize = 10;  AddSelectionCheckbox();
}//私人无效MakeAccessible()
// {
//如果(this.Rows.Count大于0)
// {
// //这将替换< TD>以<第i个并增加了scope属性
// this.UseAccessibleHeader = TRUE;// //这将增加的<&THEAD GT;和<&TBODY GT;分子
// this.HeaderRow.TableSection = TableRowSection.TableHeader;// //这增加了在<&TFOOT GT;元件。删除,如果你没有一个脚注行
// this.FooterRow.TableSection = TableRowSection.TableFooter;
//}
//}私人无效AddSelectionCheckbox()
{
  模板列TF =新的TemplateField();
  tf.HeaderText =;
  tf.ItemTemplate =新CheckboxTemplate(ROW_CHECKBOX_ID);  base.Columns.Add(TF);
}保护覆盖无效的OnInit(EventArgs的发送)
{
  SetDefaultSettings();  base.DataBound + =新的EventHandler(DataGridView_DataBound);
  base.RowDataBound + =新GridViewRowEventHandler(DataGridView_RowDataBound);  base.OnInit(E);
}保护覆盖无效在preRender(EventArgs的发送)
{
  base.On preRender(E);
  //this.PaintRows();
  //this.FormatCells();  //获取JavaScript这个DataGridView的,它包括在页面上。
  串在scriptUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(),Yakinix.UI.Controls.DataGridView.js);
  Page.ClientScript.RegisterClientScriptInclude(的DataGridView,在scriptUrl);  字符串JS =功能DGV_MakeCallback(参数)+
              {+
                  __theFormPostData =''; +
                  __theFormPostCollection =新的Array(); +
                  WebForm_InitCallback(); +
                  GetCallbackEventReference(DGV_Callback)+
              ;};  Page.ClientScript.RegisterClientScriptBlock(typeof运算(DataGridView中)的typeof(DataGridView中).FullName +DGV_MakeCallback,JS,真正的);  如果(this.HeaderRow!= NULL)
    this.HeaderRow.TableSection = TableRowSection.TableHeader;
}私人字符串GetCallbackEventReference(字符串FUNC)
{
  字符串callbackEventReference = Page.ClientScript.GetCallbackEventReference(这个说法,FUNC,'+客户端ID +',FALSE);  返回callbackEventReference;
}公共覆盖无效RenderBeginTag(HtmlTextWriter的作家)
{
  writer.RenderBeginTag(HtmlTextWriterTag.Div); //< D​​IV>
  base.RenderBeginTag(作家);
}公共覆盖无效RenderEndTag(HtmlTextWriter的作家)
{
  base.RenderEndTag(作家);
  writer.RenderEndTag(); //< / DIV>
}///<总结>
///呈现GridView控件和去除空白。它也是
///应用正确的格式。
///< /总结>
公共字符串RenderHtml()
{
  使用(StringWriter的SW =新的StringWriter(CultureInfo.InvariantCulture))
  {
    //this.PaintRows();
    //this.FormatCells();    如果(this.HeaderRow!= NULL)
      this.HeaderRow.TableSection = TableRowSection.TableHeader;    this.UseAccessibleHeader =真;
    this.RenderControl(新的HtmlTextWriter(SW));
    字符串的html = sw.ToString();    返回HTML;
  }
}// TODO:样式的所有行匹配当前SORTEX pression列。
保护无效DataGridView_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
  // DataGridView中的GridView =(DataGridView中)发送;  //如果(gridView.SortEx pression.Length大于0)
  // {
  // INT cellIndex = -1;  // //找到对应的排序前pression列索引
  //的foreach(在gridView.Columns的DataControlField场)
  // {
  //如果(field.SortEx pression == gridView.SortEx pression)
  // {
  // cellIndex = gridView.Columns.IndexOf(场);
  //突破;
  //}
  //}  //如果(cellIndex -1个)
  // {
  //如果(e.Row.RowType == DataControlRowType.Header)
  // {
  // //这是一个标题行,
  // //设置排序风格
  // e.Row.Cells [cellIndex] .CssClass + =(gridView.SortDirection == SortDirection.Ascendingsortascheader?:sortdescheader);
  //}
  //否则,如果(e.Row.RowType == DataControlRowType.DataRow)
  // {
  // //这是一个数据行
  // e.Row.Cells [cellIndex] .CssClass + =(e.Row.RowIndex%2 == 0sortaltrow?:sortrow);
  //}
  //}
  //}  RenderDetailsRow(E);
}///<总结>
///渲染细节排,渲染时,这是隐藏的。单击详细信息链接,显示的行和更详细的数据显示,并且可以编辑。
///< /总结>
///< PARAM NAME =E>的<见CREF =System.Web.UI.WebControls.GridViewRowEventArgs/> 。例如包含事件数据< /参数>
私人无效RenderDetailsRow(GridViewRowEventArgs E)
{
  //只添加一个细节行的DataRows
  如果(假放;&安培; e.Row.RowType == DataControlRowType.DataRow)
  {
    表的表= e.Row.Parent如表;    如果(表!= NULL)
    {
      GridViewRow行=新GridViewRow(-1,-1,DataControlRowType.DataRow,DataControlRowState.Normal);
      row.Style [HtmlTextWriterStyle.Display] =无;      TableCell的rowCell =新的TableCell();
      rowCell.ColumnSpan = this.Columns.Count;
      rowCell.Height =新单元(100);      row.Cells.Add(rowCell);
      table.Rows.Add(行);      的HtmlAnchor htmlAhref =新的HtmlAnchor();
      htmlAhref.HRef =JavaScript的:无效(0);;
      htmlAhref.Attributes.Add(的onclick,ToggleElement('+ row.ClientID +'););
      htmlAhref.InnerText =测试;      e.Row.Cells [e.Row.Cells.Count - 1] .Controls.Add(htmlAhref);
    }
  }
}保护无效DataGridView_DataBound(对象发件人,EventArgs的发送)
{
  FillPagerDetails(base.TopPagerRow);
  FillPagerDetails(base.BottomPagerRow);
}私人无效FillPagerDetails(GridViewRow pagerRow)
{
  如果(pagerRow!= NULL)
  {
    //从该行检索DropDownList的和Label控件。
    DropDownList的分页= pagerRow.Cells [0] .FindControl(PAGER_ID)为DropDownList的;    如果(分页!= NULL)
    {
      pageList.Attributes.Add(的onchange,DataGridViewPage(THIS.VALUE,'+客户端ID +'););      //页数添加到列表框
      的for(int i = 0; I< base.PageCount;我++)
      {
        INT PAGENUMBER = I + 1;        //创建一个列表项的再presents页面
        列表项项=新的ListItem(的String.Format({0},PAGENUMBER),i.ToString());        //如果该页面已被选中,确保
        //列表框选择所选页面
        如果(我== base.PageIndex)
          item.Selected = TRUE;        //添加ListItem对象到的DropDownList的Items集合。
        pageList.Items.Add(项目);
      }
    }
  }
}保护覆盖无效InitializeRow(GridViewRow行的DataControlField []字段)
{
  base.InitializeRow(行,场);  如果(row.RowType == DataControlRowType.Header)
  {
    的for(int i = 0; I< row.Cells.Count;我++)
    {
      row.Cells [I] .Controls.Clear();      如果(row.Cells [I] .Text.Equals(CHKCOL,StringComparison.Ordinal))
      {
        复选框chkSelectAll =新的复选框();
        chkSelectAll.ID =chkSelectAllRows;
        row.Cells [I] .Controls.Add(chkSelectAll);
      }
      其他
      {
        row.Cells [I]。文本=的String.Format(CultureInfo.InvariantCulture,< A HREF = \\JavaScript的:DataGridViewSort({0},{2});无效(0)\\称号= \\{1} \\> {1}&下; / A>中,字段[I] .SortEx pression,字段[Ⅰ] .HeaderText,this.ClientID);
      }
    }
    //如果(的ViewState [SORTEX pression]!= NULL)
    // {
    // INT指数;
    //指数= 0; // GetHeaderCellIndex(的ViewState [SORTEX pression]的ToString());
    //如果(指数!= -1)
    // {
    // //字面;
    // //如果((SortDirection)的ViewState [SortDirection] == SortDirection.Ascending)
    // // {
    // //如果(string.IsNullOrEmpty(SortAscendingImageUrl))
    // // {
    // //字面=新文字();
    // // literal.Text =&放大器;#8593;;
    // // row.Cells [指数] .Controls.Add(字面);
    // //}
    // //其他
    // // {
    // // sortImage.ImageUrl = SortAscendingImageUrl;
    // // row.Cells [指数] .Controls.Add(sortImage);
    // //}
    // //}
    // //其他
    // // {
    // //如果(string.IsNullOrEmpty(SortDescendingImageUrl))
    // // {
    // //字面=新文字();
    // // literal.Text =&放大器;#8595;;
    // // row.Cells [指数] .Controls.Add(字面);
    // //}
    // //其他
    // // {
    // // sortImage.ImageUrl = SortDescendingImageUrl;
    // // row.Cells [指数] .Controls.Add(sortImage);
    // //}
    // //}
    //}
    //}
  }
  否则,如果(row.RowType == DataControlRowType.DataRow)
  {
    的for(int i = 0; I< row.Cells.Count;我++)
    {
      如果(场[I] .HeaderText.Equals(ID,StringComparison.InvariantCultureIgnoreCase))
      {
        _IdColumnIndex = I;
        打破;
      }
    }
  }
}保护覆盖无效OnRowCreated(GridViewRowEventArgs E)
{
  base.OnRowCreated(E);  如果(e.Row.RowType == DataControlRowType.Header)
  {
    // HtmlImage IMG =新HtmlImage();
    //img.ID =lbCheckAll;
    //img.Src =/img/btn_slct_all.gif;
    //img.Alt =全选;
    //img.Attributes.Add(\"onclick,SelectAllRows('+ this.ClientID +',真);返回false;);
    //e.Row.Cells[0].Controls.Add(img);
  }
  否则,如果(e.Row.RowType == DataControlRowType.DataRow)
  {
    // AddCheckbox(E);
  }
  否则,如果(e.Row.RowType == DataControlRowType.Footer)
  {
    //e.Row.Cells[0].Text =sdsds;
  }
}保护覆盖无效OnRowDataBound(GridViewRowEventArgs E)
{
  base.OnRowDataBound(E);  如果(e.Row.RowType == DataControlRowType.DataRow)
  {
    // AddCheckbox(E);    如果(_IdColumnIndex!= -1)
      e.Row.Cells [_IdColumnIndex]。文本=< A HREF = \\Details.aspx ID =+ e.Row.Cells [_IdColumnIndex]。文本+\\?>编辑< / A>中;
  }
}公共覆盖无效排序(字符串SORTEX pression,SortDirection sortDirection)
{
  DataView的DV = this.DataSource的数据视图;  如果(DV!= NULL)
  {
    dv.Sort = SORTEX pression ++(sortDirection == SortDirection.Ascending?ASC:DESC);
  }
}#区域ICallbackEventHandler成员私人字符串callbackArg =的String.Empty;串ICallbackEventHandler.GetCallbackResult()
{
  //保存状态,PageStatePersister,并将其放置Page.ClientState
  System.Reflection.MethodInfo MI = typeof运算(页).GetMethod(SaveAllState,System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
  mi.Invoke(this.Page,NULL);  //从页的ClientState获取序列化视图状态
  System.Reflection.PropertyInfo stateProp = typeof运算(页).GetProperty(ClientState,System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
  字符串状态= stateProp.GetValue(this.Page,空)的ToString();  返回callbackArg +¤¤#+状态;
}无效ICallbackEventHandler.RaiseCallbackEvent(字符串eventArgument)
{
  尝试
  {
    //从客户端加载XML
    XmlDocument的DOC =新的XmlDocument();
    doc.LoadXml(eventArgument.Replace(与&,&放大器;放大器;));
    字符串pagerId =的String.Empty;    如果(doc.DocumentElement.Name ==回调)
    {
      字符串gridId = doc.DocumentElement [论据] [gridId]的InnerText。      开关(doc.DocumentElement [行动。的InnerText)
      {
        案排序:
          字符串SORTEX pression = doc.DocumentElement [论据] [SORTEX pression]的InnerText。
          排序(SORTEX pression.Split('')[0] .Trim(),SORTEX pression.Split('')[1] .Trim()。等于(ASC)?SortDirection.Ascending :SortDirection.Descending);
          打破;        案页:
          字符串的PageIndex = doc.DocumentElement [论据] [的PageIndex]的InnerText。
          this.PageIndex = Convert.ToInt32(PageIndex的);
          pagerId = base.BottomPagerRow.Cells [0] .FindControl(PAGER_ID).ClientID;
          打破;        案搜索:
          打破;
      }      this.DataBind();
      callbackArg =的String.Format({0}¤|¤{1}¤|¤{2},gridId,this.RenderHtml(),pagerId);
    }    DOC = NULL;
  }
  赶上(异常前)
  {
    callbackArg =的String.Format(AJAX_ERROR_FORMAT,ex.Message +\\ n \\ n+ ex.StackTrace);
  }
  最后
  {
    如果(callbackArg.Equals(的String.Empty))
    {
      callbackArg =的String.Format(AJAX_ERROR_FORMAT,Ajax错误发生!);
    }
  }
}#endregion


解决方案

这是我..的PagerRows必须设置到正确的部分。

什么工作

我得到了这个提示从
<一href=\"http://www.$c$cproject.com/Members/iguigova?msg=3153516\">http://www.$c$cproject.com/Members/iguigova?msg=3153516
(搜索MakeAccessible)

 保护覆盖无效在preRender(EventArgs的发送)
{
    base.On preRender(E);    如果(Rows.Count大于0)
    {
        UseAccessibleHeader = TRUE;
        HeaderRow.TableSection = TableRowSection.TableHeader;
        FooterRow.TableSection = TableRowSection.TableFooter;
    }    如果(TopPagerRow!= NULL)
    {
        TopPagerRow.TableSection = TableRowSection.TableHeader;
    }
    如果(BottomPagerRow!= NULL)
    {
        BottomPagerRow.TableSection = TableRowSection.TableFooter;
    }
}

I have extended the standard GridView control in ASP.NET. Basically it is just the same, but paging and sorting is done using callbacks.

I have successfully created my own PagerTemplate, but it only works when PagerSettings.Position is set to Top. I want to have the pager at both the top and the bottom.

I get an HttpException saying: The table must contain row sections in order of header, body, then footer.

Anyone know what this could be?

I already tried calling the MakeAccessible() method, but that doesn't help.

Code:

private int _IdColumnIndex = -1;

public override object DataSource
{
  get
  {
    return HttpContext.Current.Session[this.ClientID + "_DataSource"];
  }
  set
  {
    if (!value.GetType().Equals(typeof(DataView)))
      throw new ArgumentException("Only System.Data.DataView is accepted as a DataSource for DataGridView!");

    HttpContext.Current.Session[this.ClientID + "_DataSource"] = value;
  }
}

/// <summary>
/// Sets the default settings.
/// </summary>
private void SetDefaultSettings()
{
  base.AllowPaging = true;
  base.AllowSorting = true;
  base.ShowHeader = true;
  base.ShowFooter = true;
  base.PagerTemplate = new GridPagerTemplate(PAGER_ID, this);
  base.PagerSettings.Position = PagerPosition.TopAndBottom;
  base.PageSize = 10;

  AddSelectionCheckbox();
}

//private void MakeAccessible()
//{
//  if (this.Rows.Count > 0)
//  {
//    //This replaces <td> with <th> and adds the scope attribute 
//    this.UseAccessibleHeader = true;

//    //This will add the <thead> and <tbody> elements
//    this.HeaderRow.TableSection = TableRowSection.TableHeader;

//    //This adds the <tfoot> element. Remove if you don't have a footer row 
//    this.FooterRow.TableSection = TableRowSection.TableFooter;
//  }
//} 

private void AddSelectionCheckbox()
{
  TemplateField tf = new TemplateField();
  tf.HeaderText = "";
  tf.ItemTemplate = new CheckboxTemplate(ROW_CHECKBOX_ID);

  base.Columns.Add(tf);
}

protected override void OnInit(EventArgs e)
{
  SetDefaultSettings();

  base.DataBound += new EventHandler(DataGridView_DataBound);
  base.RowDataBound += new GridViewRowEventHandler(DataGridView_RowDataBound);

  base.OnInit(e);
}

protected override void OnPreRender(EventArgs e)
{
  base.OnPreRender(e);
  //this.PaintRows();
  //this.FormatCells();

  // Get JavaScript for this DataGridView, and include it on the page.
  string scriptUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Yakinix.UI.Controls.DataGridView.js");
  Page.ClientScript.RegisterClientScriptInclude("DataGridView", scriptUrl);

  string js = "function DGV_MakeCallback(argument)" +
              "{" +
                  "__theFormPostData = '';" +
                  "__theFormPostCollection = new Array();" +
                  "WebForm_InitCallback();" +
                  GetCallbackEventReference("DGV_Callback") +
              ";}";

  Page.ClientScript.RegisterClientScriptBlock(typeof(DataGridView), typeof(DataGridView).FullName + "DGV_MakeCallback", js, true);

  if (this.HeaderRow != null)
    this.HeaderRow.TableSection = TableRowSection.TableHeader;
}

private string GetCallbackEventReference(string func)
{
  string callbackEventReference = Page.ClientScript.GetCallbackEventReference(this, "argument", func, "'" + ClientID + "'", false);

  return callbackEventReference;
}

public override void RenderBeginTag(HtmlTextWriter writer)
{
  writer.RenderBeginTag(HtmlTextWriterTag.Div); // <div>
  base.RenderBeginTag(writer);
}

public override void RenderEndTag(HtmlTextWriter writer)
{
  base.RenderEndTag(writer);
  writer.RenderEndTag(); // </div>
}

/// <summary>
/// Renders the gridview control and strips whitespace. It also
/// applies the right formatting.
/// </summary>
public string RenderHtml()
{
  using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture))
  {
    //this.PaintRows();
    //this.FormatCells();

    if (this.HeaderRow != null)
      this.HeaderRow.TableSection = TableRowSection.TableHeader;

    this.UseAccessibleHeader = true;
    this.RenderControl(new HtmlTextWriter(sw));
    string html = sw.ToString();

    return html;
  }
}

// TODO: Style all rows for the column matching current SortExpression.
protected void DataGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
  //DataGridView gridView = (DataGridView)sender;

  //if (gridView.SortExpression.Length > 0)
  //{
  //  int cellIndex = -1;

  //  //  find the column index for the corresponding sort expression
  //  foreach (DataControlField field in gridView.Columns)
  //  {
  //    if (field.SortExpression == gridView.SortExpression)
  //    {
  //      cellIndex = gridView.Columns.IndexOf(field);
  //      break;
  //    }
  //  }

  //  if (cellIndex > -1)
  //  {
  //    if (e.Row.RowType == DataControlRowType.Header)
  //    {
  //      //  this is a header row,
  //      //  set the sort style
  //      e.Row.Cells[cellIndex].CssClass += (gridView.SortDirection == SortDirection.Ascending ? " sortascheader" : " sortdescheader");
  //    }
  //    else if (e.Row.RowType == DataControlRowType.DataRow)
  //    {
  //      //  this is a data row
  //      e.Row.Cells[cellIndex].CssClass += (e.Row.RowIndex % 2 == 0 ? " sortaltrow" : "sortrow");
  //    }
  //  }
  //}

  RenderDetailsRow(e);
}

/// <summary>
/// Renders the details row, which is hidden when rendered. Clicking a details link, displays the row and more detailed data is shown, and is editable.
/// </summary>
/// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
private void RenderDetailsRow(GridViewRowEventArgs e)
{
  // Only add a details row to DataRows
  if (false && e.Row.RowType == DataControlRowType.DataRow)
  {
    Table table = e.Row.Parent as Table;

    if (table != null)
    {
      GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
      row.Style[HtmlTextWriterStyle.Display] = "none";

      TableCell rowCell = new TableCell();
      rowCell.ColumnSpan = this.Columns.Count;
      rowCell.Height = new Unit(100);

      row.Cells.Add(rowCell);
      table.Rows.Add(row);

      HtmlAnchor htmlAhref = new HtmlAnchor();
      htmlAhref.HRef = "javascript:void(0);";
      htmlAhref.Attributes.Add("onclick", "ToggleElement('" + row.ClientID + "');");
      htmlAhref.InnerText = "Test";

      e.Row.Cells[e.Row.Cells.Count - 1].Controls.Add(htmlAhref);
    }
  }
}

protected void DataGridView_DataBound(object sender, EventArgs e)
{
  FillPagerDetails(base.TopPagerRow);
  FillPagerDetails(base.BottomPagerRow);
}

private void FillPagerDetails(GridViewRow pagerRow)
{
  if (pagerRow != null)
  {
    // Retrieve the DropDownList and Label controls from the row.
    DropDownList pageList = pagerRow.Cells[0].FindControl(PAGER_ID) as DropDownList;

    if (pageList != null)
    {
      pageList.Attributes.Add("onchange", "DataGridViewPage(this.value, '" + ClientID + "');");

      // Add the number of pages to the ListBox
      for (int i = 0; i < base.PageCount; i++)
      {
        int pageNumber = i + 1;

        // Create a ListItem that represents a page
        ListItem item = new ListItem(String.Format("{0}", pageNumber), i.ToString());

        // If the page is already selected, make sure the 
        // ListBox select the selected page
        if (i == base.PageIndex)
          item.Selected = true;

        // Add the ListItem object to the Items collection of the DropDownList.
        pageList.Items.Add(item);
      }
    }
  }
}

protected override void InitializeRow(GridViewRow row, DataControlField[] fields)
{
  base.InitializeRow(row, fields);

  if (row.RowType == DataControlRowType.Header)
  {
    for (int i = 0; i < row.Cells.Count; i++)
    {
      row.Cells[i].Controls.Clear();

      if (row.Cells[i].Text.Equals("CHKCOL", StringComparison.Ordinal))
      {
        CheckBox chkSelectAll = new CheckBox();
        chkSelectAll.ID = "chkSelectAllRows";
        row.Cells[i].Controls.Add(chkSelectAll);
      }
      else
      {
        row.Cells[i].Text = String.Format(CultureInfo.InvariantCulture, "<a href=\"javaScript:DataGridViewSort('{0}', '{2}');void(0)\" title=\"{1}\">{1}</a>", fields[i].SortExpression, fields[i].HeaderText, this.ClientID);
      }
    }
    //if (ViewState["SortExpression"] != null)
    //{
    //  int index;
    //  index = 0;// GetHeaderCellIndex(ViewState["SortExpression"].ToString());
    //  if (index != -1)
    //  {
    //    //Literal literal;
    //    //if ((SortDirection)ViewState["SortDirection"] == SortDirection.Ascending)
    //    //{
    //    //  if (string.IsNullOrEmpty(SortAscendingImageUrl))
    //    //  {
    //    //    literal = new Literal();
    //    //    literal.Text = "&#8593;";
    //    //    row.Cells[index].Controls.Add(literal);
    //    //  }
    //    //  else
    //    //  {
    //    //    sortImage.ImageUrl = SortAscendingImageUrl;
    //    //    row.Cells[index].Controls.Add(sortImage);
    //    //  }
    //    //}
    //    //else
    //    //{
    //    //  if (string.IsNullOrEmpty(SortDescendingImageUrl))
    //    //  {
    //    //    literal = new Literal();
    //    //    literal.Text = "&#8595;";
    //    //    row.Cells[index].Controls.Add(literal);
    //    //  }
    //    //  else
    //    //  {
    //    //    sortImage.ImageUrl = SortDescendingImageUrl;
    //    //    row.Cells[index].Controls.Add(sortImage);
    //    //  }
    //    //}
    //  }
    //}
  }
  else if (row.RowType == DataControlRowType.DataRow)
  {
    for (int i = 0; i < row.Cells.Count; i++)
    {
      if (fields[i].HeaderText.Equals("id", StringComparison.InvariantCultureIgnoreCase))
      {
        _IdColumnIndex = i;
        break;
      }
    }
  }
}

protected override void OnRowCreated(GridViewRowEventArgs e)
{
  base.OnRowCreated(e);

  if (e.Row.RowType == DataControlRowType.Header)
  {
    //HtmlImage img = new HtmlImage();
    //img.ID = "lbCheckAll";
    //img.Src = "/img/btn_slct_all.gif";
    //img.Alt = "Select all";
    //img.Attributes.Add("onclick", "SelectAllRows('" + this.ClientID + "',true);return false;");
    //e.Row.Cells[0].Controls.Add(img);
  }
  else if (e.Row.RowType == DataControlRowType.DataRow)
  {
    //AddCheckbox(e);
  }
  else if (e.Row.RowType == DataControlRowType.Footer)
  {
    //e.Row.Cells[0].Text = "sdsds";
  }
}

protected override void OnRowDataBound(GridViewRowEventArgs e)
{
  base.OnRowDataBound(e);

  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    //AddCheckbox(e);

    if (_IdColumnIndex != -1)
      e.Row.Cells[_IdColumnIndex].Text = "<a href=\"Details.aspx?id=" + e.Row.Cells[_IdColumnIndex].Text + "\">Edit</a>";
  }
}

public override void Sort(string sortExpression, SortDirection sortDirection)
{
  DataView dv = this.DataSource as DataView;

  if (dv != null)
  {
    dv.Sort = sortExpression + " " + (sortDirection == SortDirection.Ascending ? "ASC" : "DESC");
  }
}

#region ICallbackEventHandler Members

private string callbackArg = String.Empty;

string ICallbackEventHandler.GetCallbackResult()
{
  // Save state with PageStatePersister and place it to Page.ClientState
  System.Reflection.MethodInfo mi = typeof(Page).GetMethod("SaveAllState", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
  mi.Invoke(this.Page, null);

  // Get serialized viewstate from Page's ClientState
  System.Reflection.PropertyInfo stateProp = typeof(Page).GetProperty("ClientState", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
  string state = stateProp.GetValue(this.Page, null).ToString();

  return callbackArg + "¤#¤" + state;
}

void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
  try
  {
    // Load XML from client
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(eventArgument.Replace("&", "&amp;"));
    string pagerId = String.Empty;

    if (doc.DocumentElement.Name == "callback")
    {
      string gridId = doc.DocumentElement["arguments"]["gridId"].InnerText;

      switch (doc.DocumentElement["action"].InnerText)
      {
        case "sort":
          string sortExpression = doc.DocumentElement["arguments"]["sortExpression"].InnerText;
          Sort(sortExpression.Split(' ')[0].Trim(), sortExpression.Split(' ')[1].Trim().Equals("ASC") ? SortDirection.Ascending : SortDirection.Descending);
          break;

        case "page":
          string pageIndex = doc.DocumentElement["arguments"]["pageIndex"].InnerText;
          this.PageIndex = Convert.ToInt32(pageIndex);
          pagerId = base.BottomPagerRow.Cells[0].FindControl(PAGER_ID).ClientID;
          break;

        case "search":
          break;
      }

      this.DataBind();
      callbackArg = String.Format("{0}¤|¤{1}¤|¤{2}", gridId, this.RenderHtml(), pagerId);
    }

    doc = null;
  }
  catch (Exception ex)
  {
    callbackArg = String.Format(AJAX_ERROR_FORMAT, ex.Message + "\n\n" + ex.StackTrace);
  }
  finally
  {
    if (callbackArg.Equals(string.Empty))
    {
      callbackArg = String.Format(AJAX_ERROR_FORMAT, "An AJAX Error occurred!");
    }
  }
}

#endregion

解决方案

This is what works for me.. the PagerRows must be set to their proper sections.

I got this tip from http://www.codeproject.com/Members/iguigova?msg=3153516 (search for MakeAccessible)

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);

    if (Rows.Count > 0)
    {
        UseAccessibleHeader = true;
        HeaderRow.TableSection = TableRowSection.TableHeader;
        FooterRow.TableSection = TableRowSection.TableFooter;
    }

    if (TopPagerRow != null)
    {
        TopPagerRow.TableSection = TableRowSection.TableHeader;
    }
    if (BottomPagerRow != null)
    {
        BottomPagerRow.TableSection = TableRowSection.TableFooter;
    }
}

这篇关于GridView控件与寻呼机TopAndBottom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆