开发者

Visible Checkbox only on Gridview's last row?

Is it possible to have a Checkbox that only shows up when Editing the last row of a GridView?

I have tried something like this in the EditItemTemplate:

<asp:CheckBox ID="chk开发者_StackOverflowNextDay" runat="server" 
              ToolTip="Is it a next-day departure?"
              Enabled="true" 
              Checked='<%# DateTime.Parse(Eval("OutHour","{0:d}")).Date >
                           DateTime.Parse(Eval("InHour","{0:d}")).Date  %>'/>

Then on code-behind I tried hiding it for rows other than the last one like this:

protected void grvOutHour_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView grvOutHour = (GridView)this.grvReport.Rows[grvReport.EditIndex].FindControl("grvOutHour");
            TextBox txtBox = (TextBox)grvOutHour.Rows[e.NewEditIndex].FindControl("txtEditOutHour");
            CheckBox nextDay = (CheckBox)grvOutHour.Rows[e.NewEditIndex].FindControl("chkNextDay");
            if (grvOutHour.Rows.Count-1 != e.NewEditIndex)
                nextDay.Visible = false;
        }

This ALMOST worked, but the checkbox kept showing for all fields, I think because the RowDataBound is called AFTER RowEditing again so it renders the whole thing again :(

Any suggestions?

Thanks, EtonB.


Use RowDataBound instead...

protected void grvOutHour_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowState == DataControlRowState.Edit)
    {
        GridView grid = (GridView)sender;
        CheckBox nextDay = (CheckBox)e.Row.FindControl("chkNextDay");
        nextDay.Visible = (e.Row.RowIndex == (grid.Rows.Count - 1));
    }
}


You will need to handle hiding the checkbox in the RowDataBound event.

You'll need to determine what the last row is, and set the checkboxes visible property to true when that condition is true, obviously.


I guess it's more of a hack than an elegant solution, but I would probably just hide the other checkboxes via JavaScript if the condition is true.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜