使用buttonfield分页的gridview命令参数

时间:2009-11-03 10:43:46

标签: c# button gridview pagination

我正在使用c#.net

我有一个gridview,需要包含一个'Use'按钮(将appointmentId设置为命令参数)。

源代码

<asp:GridView ID="resultsReturned" runat="server" AllowPaging="True"  
    AutoGenerateColumns="False"
    EnableSortingAndPagingCallbacks="True" 
    OnPageIndexChanged="resultsReturned_PageIndexChanged" 
    onrowcommand="resultsReturned_RowCommand">
    <Columns>    
      <asp:BoundField DataField="UserAppointmentId" HeaderText="App ID" />
      <asp:BoundField DataField="UserBookingName" HeaderText="Booking Name" />
      <asp:TemplateField>
        <ItemTemplate>                
          <asp:Button runat="server" 
            ID="UseButton"
            Text="Use"
            CommandName="Use"
            CommandArgument="UserAppointmentId" />
        </ItemTemplate>
      </asp:TemplateField>
    </Columns>
  </asp:GridView>  

代码隐藏

protected void resultsReturned_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Use")
    {
        correctAppointmentID.Value = (e.CommandArgument.ToString());
    }
}

这用于分页:

private void BindAppointments()
{
    var results = appointmentRepos.GetBookingIdBySearchCriteria(catgoryid, resultsReturned.PageIndex * resultsReturned.PageSize, -1);

    resultsReturned.DataSource = results;            
    resultsReturned.DataBind();
}

我将约会绑定到PageLoad / search_Click

中的gridview

这是我收到的错误:

  

TemplateField不支持回调,因为某些控件无法在回调中正确更新。在'resultsReturned'上关闭回调。

1 个答案:

答案 0 :(得分:2)

尝试以下方法:

 <asp:Button runat="server" 
ID="UseButton" Text="Use" CommandName="Use"
CommandArgument='<%# DataBinder.Eval(Container,"DataItem.UserAppointmentId") %>' />

并设置EnableSortingAndPagingCallbacks =“False”