单个GridView列中的多个控件类型

时间:2013-07-01 22:29:32

标签: asp.net c#-3.0

with:SELECT id,name,Flag1 FROM Table

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

换句话说: 如果Flag1 = true,则显示复选框为checked和readonly。 如果Flag1 = false,则显示带有click事件的Button,并将row的id传递给handler。

我需要挂钩哪个事件才能进行更改?数据绑定? 如何在.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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

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

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;
    }
}