【问题标题】:Adding a DataGridCheckBoxColumn to as ASP.Net page将 DataGridCheckBoxColumn 添加为 ASP.Net 页面
【发布时间】:2018-03-10 07:13:18
【问题描述】:

您可以在 ASP.Net 页面上的 DataGrid 中添加一个未绑定的复选框吗?我能找到的所有示例似乎都只适用于 WinForm,但我需要在网页中添加一个。

这是我尝试过的代码的 sn-p(来自代码隐藏页面):

// Perform the binding.
DataGrid_AuditSearch.DataSource = ds;
DataGrid_AuditSearch.DataBind();

//Hide the Plan Review Indicator, we only need it to see if the row should be highlighted
DataGrid_AuditSearch.Columns[8].Visible = false;

// do this after changing DataGrid_AuditSearch.DataSource
DataGridColumn checkBoxColumn = new DataGridCheckBoxColumn();
checkBoxColumn.HeaderText = "Selected";
DataGrid_AuditSearch.Columns.Add(checkBoxColumn);

最后一点是基于我在网上找到的东西,但显然它只适用于 WinForm。

这是 ASP 页面,如果它更容易,我很乐意将列添加到该页面:

<asp:DataGrid ID="DataGrid_AuditSearch" runat="server"
        AllowPaging="False" AllowSorting="True" CellPadding="4" ForeColor="#333333" 
        GridLines="None" AutoGenerateColumns="false" 
        OnItemDataBound="DataGrid_AuditSearch_RowDataBound" 
        OnCancelCommand="DataGrid_AuditSearch_CancelCommand" 
        OnUpdateCommand="DataGrid_AuditSearch_UpdateCommand" 
        OnEditCommand="DataGrid_AuditSearch_EditCommand">
    <AlternatingItemStyle Font-Bold="False" Font-Italic="False" 
        Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
    <EditItemStyle BackColor="#999999" Font-Bold="False" Font-Italic="False" 
        Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="False" Font-Italic="False" 
        Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="False" Font-Italic="False" 
        Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
    <PagerStyle BackColor="#5D7B9D" Font-Bold="False" Font-Italic="False" 
        Font-Overline="False" Font-Strikeout="False" Width="920px" Font-Underline="False" />
    <SelectedItemStyle BackColor="#E2DED6" Font-Bold="False" Font-Italic="False" 
        Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
    <Columns>
        <asp:EditCommandColumn ButtonType="PushButton" CancelText="Cancel" 
            EditText="Select" UpdateText="Update"></asp:EditCommandColumn>

        <asp:BoundColumn DataField="AUDIT_ID" HeaderText="Audit ID"/>  
        <asp:BoundColumn DataField="PLAN_ID" HeaderText="Plan ID"/>  
        <asp:BoundColumn DataField="PLAN_DESC" HeaderText="Plan Desc"/>  
        <asp:BoundColumn DataField="DOC_TYPE" HeaderText="Doc Type"/>
        <asp:BoundColumn DataField="PRODUCT" HeaderText="Product"/>
        <asp:BoundColumn DataField="PLAN_TYPE" HeaderText="Plan Type"/>  
        <asp:BoundColumn DataField="Auditor_ID" HeaderText="Auditor ID"/> 
        <asp:BoundColumn DataField="Plan_Review_Ind" HeaderText="Plan_Review_Ind"/> 
    </Columns>

</asp:DataGrid>

感谢任何帮助。

【问题讨论】:

标签: c# asp.net datagrid


【解决方案1】:

我使用了这个代码:Adding a CheckBox column to your DataGrid

我在CheckBoxItem 类的BindData 中注释了一些行:

private void BindData(object sender, EventArgs e)
{
    CheckBox box = (CheckBox)sender;
    DataGridItem container = (DataGridItem)box.NamingContainer;
    box.Checked = false;
}

然后在绑定数据源时,添加自定义复选框列:

// Perform the binding.
DataGrid_AuditSearch.DataSource = ds;
DataGrid_AuditSearch.DataBind();

// Create and add the custom checkbox column
CheckBoxColumn checkBoxColumn = new CheckBoxColumn(false);
checkBoxColumn.HeaderText = "Selected";
DataGrid_AuditSearch.Columns.Add(checkBoxColumn);
DataGrid_AuditSearch.DataBind();

它似乎可以解决问题。希望对你有帮助!


感谢Shaun Wilde,这是CheckBoxColumnCheckBoxItem 的完整课程。

CheckBoxColumn.cs

public class CheckBoxColumn :System.Web.UI.WebControls.TemplateColumn
{
    /// <summary>
    /// Initialise our CheckBoxColumn.
    /// </summary>
    public CheckBoxColumn()
    {
        // set the view one as readonly
        viewItem = new CheckBoxItem(false); // SAW was false
        this.ItemTemplate = viewItem as ITemplate;

        // let the edit check box be editable
        editItem = new CheckBoxItem(true);
        this.EditItemTemplate = editItem as ITemplate;
    }

    /// <summary>
    /// Initialise our CheckBoxColumn with an optional ImmediatePostback capability.
    /// </summary>
    /// <param name="ImmediatePostback">If true then each change of state of the CheckBox item 
    /// will cause an event to be fired immediately on the server.</param>
    public CheckBoxColumn(bool ImmediatePostback)
    {
        // set the view one as readonly
        viewItem = new CheckBoxItem(ImmediatePostback);
        this.ItemTemplate = viewItem as ITemplate;

        // let the edit check box be editable
        editItem = new CheckBoxItem(true);
        this.EditItemTemplate = editItem as ITemplate;

        AutoPostBack = ImmediatePostback;
    }

    /// <summary>
    /// Occurs when the value of the Checked property changes between posts to the server. 
    /// </summary>
    /// <remarks>
    /// The <b>CheckedChanged</b> event is raised when the value of the Checked property changes between posts to the server.
    ///
    /// <b>Note</b>   This event does not post the page back to the server unless the AutoPostBack property is set to true.
    /// <b>Note</b>   The control must have viewstate enabled for the <b>CheckedChanged</b> event to work correctly.
    /// </remarks>
    public event EventHandler CheckedChanged
    {
        add
        {
            viewItem.CheckedChanged += value;
            editItem.CheckedChanged += value;
        }
        remove
        {
            viewItem.CheckedChanged -= value;
            editItem.CheckedChanged -= value;
        }
    }

    /// <summary>
    /// If true then then each click on a CheckBox will cause an event to be fired on the server.
    /// </summary>
    public bool AutoPostBack
    {
        set
        {
            viewItem.AutoPostBack = value;
            editItem.AutoPostBack = value;
        }
        get
        {
            return viewItem.AutoPostBack;
        }
    }

    /// <summary>
    /// The DataField that we wish our control to bind to.
    /// </summary>
    public string DataField
    {
        get
        {
            return viewItem.DataField;
        }
        set
        {
            viewItem.DataField = value;
            editItem.DataField = value;
        }
    }

    /// <summary>
    /// Internal storage of the CheckBoxItem that is to be used for the view state.
    /// </summary>
    private CheckBoxItem viewItem;

    /// <summary>
    /// Internal storage of the CheckBoxItem that is to be used for the edit state.
    /// </summary>
    private CheckBoxItem editItem;
}

CheckBoxItem.cs

internal class CheckBoxItem : ITemplate
{
    /// <summary>
    /// The CheckBoxItem constructor
    /// </summary>
    /// <param name="editable">true if the item is to be in its editable state, false for the item to be disabled.</param>
    public CheckBoxItem(bool editable)
    {
        readOnly = (editable==true)?false:true;
    }

    /// <summary>
    /// Instantiates the CheckBox that we wish to represent in this column. 
    /// </summary>
    /// <param name="container">The container into which the control or controls are added.</param>
    void ITemplate.InstantiateIn(Control container)
    {
        CheckBox box = new CheckBox();
        box.DataBinding += new EventHandler(this.BindData);
        box.AutoPostBack = autoPostBack;
        box.CheckedChanged += new EventHandler(this.OnCheckChanged);
        container.Controls.Add(box);
    }

    /// <summary>
    /// Our CheckChanged event
    /// </summary>
    public event EventHandler CheckedChanged;

    /// <summary>
    /// This is a common handler for all the Checkboxes.
    /// </summary>
    /// <param name="sender">The raiser of this event a CheckBox.</param>
    /// <param name="e">A System.EventArgs that contains the event data.</param>
    private void OnCheckChanged(object sender, EventArgs e)
    {
        if (CheckedChanged != null)
        {
            CheckedChanged(sender, e);
        }
    }

    /// <summary>
    /// The internal storage for which DataField we are going to represent.
    /// </summary>
    private string dataField;

    /// <summary>
    /// Used to set the DataField that we wish to represent with this CheckBox.
    /// </summary>
    public string DataField
    {
        get
        {
            return dataField;
        }
        set
        {
            dataField=value;
        }
    }

    /// <summary>
    /// The internal storage for the AutoPostback flag.
    /// </summary>
    private bool autoPostBack=false;

    /// <summary>
    /// Set the AutoPostBack flag. If this is true then each time a CheckBox is clicked 
    /// in the Column that contains this item then an event is raised on the server.
    /// </summary>
    public bool AutoPostBack
    {
        set
        {
            autoPostBack = value;
        }
        get
        {
            return autoPostBack;
        }
    }

    /// <summary>
    /// Handler for the DataBinding event where we bind the data for a specific row 
    /// to the CheckBox.
    /// </summary>
    /// <param name="sender">The raiser of the event.</param>
    /// <param name="e">A System.EventArgs that contains the event data.</param>
    private void BindData(object sender, EventArgs e)
    {
        CheckBox box = (CheckBox) sender;
        DataGridItem container = (DataGridItem) box.NamingContainer;
        box.Checked = false;
        box.Enabled = (readOnly == true) ? false:true;
        string data = ((DataRowView) container.DataItem)[dataField].ToString();
        Type t = ((DataRowView)container.DataItem).DataView.Table.Columns[dataField].DataType;
        if (data.Length>0)
        {
            switch (t.ToString())
            {
                case "System.Boolean":
                    if (( data == "True") || (data == "true"))
                    {
                        box.Checked = true;
                    }
                    break;
                default:
                    break;
            }
        }
    }

    /// <summary>
    /// Internal storage for the readOnly flag.
    /// </summary>
    private bool readOnly = true;
}

【讨论】:

    猜你喜欢
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多