1

带有:从表中选择 id、名称、Flag1

是否可以根据 Flag1 字段的位值在单个列中动态显示 Button 或 CheckBox?

换句话说:如果 Flag1 = true 则将复选框显示为选中和只读。如果 Flag1 = false 显示带有单击事件的按钮并将行的 id 传递给处理程序。

我需要挂钩哪个事件来进行更改?数据绑定?如何在 .aspx 或 .cs 中设置复选框/按钮?通常我将 ItemTemplates 与 Gridviews 一起使用...

            <asp:TemplateField HeaderText="Under Warranty" >
            <ItemTemplate>
                <asp:Button ID="WButton" runat="server" CommandName="AddWarrantyTrackerItem" CommandArgument = "<%# (Container.DataItemIndex) %>" Text="Track Warranty" Visible="false" />
                <asp:CheckBox ID="WFlag" runat="server" Visible="false" readonly="true" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Depot" >
            <ItemTemplate>
                <asp:Button ID="DButton" runat="server" CommandName="AddDepotTrackerItem" CommandArgument = "<%# (Container.DataItemIndex) %>" Text="Track Depot" Visible="false" />
                <asp:CheckBox ID="DFlag" runat="server" Visible="false"  readonly="true" />
            </ItemTemplate>
        </asp:TemplateField>

代码隐藏:

    protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        CheckBox WFlag = (CheckBox)e.Row.FindControl("WFlag");
        Button WButton = (Button)e.Row.FindControl("WButton");
        CheckBox DFlag = (CheckBox)e.Row.FindControl("DFlag");
        Button DButton = (Button)e.Row.FindControl("DButton");
        var record = (System.Data.Common.DbDataRecord)e.Row.DataItem;
        bool flagW = (bool)record.GetValue(14);
        bool flagD = (bool)record.GetValue(15);
        int reqnum = (int)record.GetValue(0);
        if (flagW == false) 
        {
            WButton.Visible = true;
            WFlag.Visible = false;
        }
        else
        {
            WButton.Visible = false;
            WFlag.Visible = true;
            WFlag.Checked = true;
        }
        if (flagD == false)   
        {
            DButton.Visible = true;
            DFlag.Visible = false;
        }
        else
        {
            DButton.Visible = false;
            DFlag.Visible = true;
            DFlag.Checked = true;
        }
    }
}
4

1 回答 1

1

是的,您可以使用TemplateFieldandRowDataBound来切换可见性:

aspx:

<Columns>
     <asp:TemplateField HeaderText="Flagged">
        <ItemTemplate>
            <asp:Button ID="ButtonFlag" runat="server" Text="flag it" Visible="true" />
            <asp:CheckBox ID="CheckFlag" runat="server" Checked="true" Visible="false" />
        </ItemTemplate>
    </asp:TemplateField>

代码隐藏:

protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        CheckBox chkFlag = (CheckBox)e.Row.FindControl("CheckFlag");
        Button buttonFlag = (Button)e.Row.FindControl("ButtonFlag");
        DataRow row = ((DataRowView)e.Row.DataItem).Row;
        bool flagged = row.Field<bool>("Flag1");
        buttonFlag.Visible = !flagged;
        chkFlag.Visible = flagged;
    }
}
于 2013-07-01T22:40:59.207 回答