【问题标题】:Multiple control types in a single GridView column单个 GridView 列中的多个控件类型
【发布时间】:2013-07-11 06:34:41
【问题描述】:

with: SELECT id, name, Flag1 FROM Table

是否可以根据 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;
        }
    }
}

【问题讨论】:

    标签: asp.net c#-3.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;
        }
    }
    

    【讨论】:

    • DataRow 行 = ((DataRowView)e.Row.DataItem).Row;错误:无法将“System.Data.Common.DataRecordInternal”类型的对象转换为“System.Data.DataRowView”类型。
    • @user1431356:您现在使用DataReader 作为数据源吗?试试var rd = (System.Data.Common.DbDataRecord)e.Row.DataItem;。如果可行,您可以使用DbDataRecord 的方法获取所有字段。
    猜你喜欢
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 2016-05-08
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多