2016-11-02 3 views
2

あるとき、私は上に充填し、このGridViewを有するPage_Load無効ButtonField BoundFieldがゼロ

protected void Page_Load(object sender, EventArgs e) { 
    if (!Page.IsPostBack) { 
    GridView1.DataSource = actBO.BuscarActividades(); 
    GridView1.DataBind(); 
    } 
} 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" > 
     <Columns> 
      <asp:BoundField DataField="Id" HeaderText="ID" Visible="False" /> 
      <asp:BoundField DataField="Class" HeaderText="Class" /> 
      <asp:BoundField DataField="Day" HeaderText="Day" /> 
      <asp:BoundField DataField="Time" HeaderText="Time" /> 
      <asp:BoundField DataField="Vacants" HeaderText="Vacants" />   

      <asp:ButtonField ButtonType="Button" HeaderText="Book" Text="Book"/> 

     </Columns> 
</asp:GridView> 

カラム「Vacantsは」int(これは空の予約スペースの量を表す示しますクラス)。

すべての行には、特定のクラスを予約するボタンがあります。フィールド「Vacants」がゼロのときに条件を設定する必要があるので、「Book」ボタンは無効になります。

これまでのところ、これは次のようになります。image

ご覧のとおり、空きがなくなったらボタンを無効にする必要があります。クリックすることはできません。

答えて

0

これを行うには、OnRowDataBoundイベントを登録する必要があります。詳しい説明はhereにあります。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"> 
     <Columns> 
      <asp:BoundField DataField="Id" HeaderText="ID" Visible="False" /> 
      <asp:BoundField DataField="Class" HeaderText="Class" /> 
      <asp:BoundField DataField="Day" HeaderText="Day" /> 
      <asp:BoundField DataField="Time" HeaderText="Time" /> 
      <asp:BoundField DataField="Vacants" HeaderText="Vacants" />   
      <asp:ButtonField ButtonType="Button" HeaderText="Book" Text="Book"/> 
     </Columns> 
</asp:GridView> 


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     // get your button via the column index; ideally you could use template field and put your own button inside 
     var button = e.Row.Cell[5].Controls[0] as Button; 
     int vacant = 0; 
     var vacantVal = int.TryParse(e.Row.Cell[4].Text, out vacant); 
     if (button != null) 
     { 
      button.Enabled = vacant > 0; 
     } 
    } 
} 

希望します。