C# 将DataGridCheckBoxColumn添加到ASP.Net页面
是否可以将未绑定复选框添加到ASP.Net页面上的DataGrid?我能找到的所有示例似乎只适用于WinForm,但我需要在网页中添加一个 以下是我尝试过的代码片段(来自代码隐藏页面): 最后一点是基于我在网上找到的东西,但显然它只适用于WinForm 这是ASP页面,如果方便的话,我很乐意添加该列:C# 将DataGridCheckBoxColumn添加到ASP.Net页面,c#,asp.net,datagrid,C#,Asp.net,Datagrid,是否可以将未绑定复选框添加到ASP.Net页面上的DataGrid?我能找到的所有示例似乎只适用于WinForm,但我需要在网页中添加一个 以下是我尝试过的代码片段(来自代码隐藏页面): 最后一点是基于我在网上找到的东西,但显然它只适用于WinForm 这是ASP页面,如果方便的话,我很乐意添加该列: <asp:DataGrid ID="DataGrid_AuditSearch" runat="server" AllowPaging="False" AllowSorting
<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>
非常感谢您的帮助。我使用了以下代码:
我在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();
这似乎奏效了。我希望有帮助
感谢肖恩·王尔德,这里是完整的类
CheckBoxColumn
和CheckBoxItem
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;
}
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;
}
公共类CheckBoxColumn:System.Web.UI.WebControls.TemplateColumn
{
///
///初始化我们的CheckBoxColumn。
///
公共CheckBoxColumn()
{
//将视图设置为只读
viewItem=new CheckBoxItem(false);//SAW为false
this.ItemTemplate=将项目视为ITemplate;
//使“编辑”复选框可编辑
editItem=新的CheckBoxItem(true);
this.EditItemTemplate=editItem作为ITemplate;
}
///
///使用可选的即时回写功能初始化CheckBoxColumn。
///
///如果为true,则复选框项的每个状态更改
///将导致在服务器上立即触发事件。
公共复选框列(bool ImmediatePostback)
{
//将视图设置为只读
viewItem=新的复选框项(立即返回);
this.ItemTemplate=将项目视为ITemplate;
//使“编辑”复选框可编辑
editItem=新的CheckBoxItem(true);
this.EditItemTemplate=editItem作为ITemplate;
自动回发=立即回发;
}
///
///在向服务器发送的邮件之间更改Checked属性的值时发生。
///
///
///CheckedChanged事件是在向服务器发送的邮件之间Checked属性的值发生更改时引发的。
///
///注意:除非AutoPostBack属性设置为true,否则此事件不会将页面发回服务器。
///注意:控件必须启用viewstate,CheckedChanged事件才能正常工作。
///
公共事件事件处理程序CheckedChanged
{
添加
{
viewItem.CheckedChanged+=值;
editItem.CheckedChanged+=值;
}
去除
{
viewItem.CheckedChanged-=值;
editItem.CheckedChanged-=值;
}
}
///
///如果为true,则每次单击复选框都会导致在服务器上触发事件。
///
公共bool自动回发
{
设置
{
viewItem.AutoPostBack=值;
editItem.AutoPostBack=值;
}
得到
{
返回viewItem.AutoPostBack;
}
}
///
///希望控件绑定到的数据字段。
///
公共字符串数据字段
{
得到
{
返回viewItem.DataField;
}
设置
{
viewItem.DataField=值;
editItem.DataField=值;
}
}
///
///用于视图状态的CheckBoxItem的内部存储。
///
私有CheckBoxItem视图项;
///
///用于编辑状态的CheckBoxItem的内部存储。
///
私有CheckBoxItem编辑项;
}
CheckBoxItem.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;
}
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;
}
内部类CheckBoxItem:ITemplate
{
///
///CheckBoxItem构造函数
///
///如果项目处于可编辑状态,则为true;如果项目处于禁用状态,则为false。
公共复选框项目(bool可编辑)
{
只读=(可编辑==真)?假:真;
}
///
///实例化我们希望在此列中表示的复选框。
///
///将控件添加到其中的容器。
void ITemplate.ein(控制容器)
{
复选框=新复选框();
box.DataBinding+=新的EventHandler(this.BindData);
box.AutoPostBack=AutoPostBack;
box.CheckedChanged+=新的EventHandler(this.OnCheckChanged);
container.Controls.Add(box);
}
///
///我们的支票更改了事件
///
公共事件事件处理程序CheckedChanged;
///
///这是所有复选框的通用处理程序。
///
///此事件的引发者是一个复选框。
///包含事件数据的System.EventArgs。
私有void OnCheckChanged(对象发送方,事件参数e)
{
if(CheckedChanged!=null)
{
CheckedChanged(发送方,e);
}
}
///
///我们将为其表示数据字段的内部存储。
///
私有字符串数据字段;
///
///用于设置我们希望用此复选框表示的数据字段。
///
公共字符串数据字段
{
得到
{
返回数据字段;
}
设置
{
数据字段=值;
}
}
///
///自动回发标志的内部存储器。
///
私有bool autoPostBack=false;
///
///设置自动回发标志。如果为真,则每次单击复选框时
///在包含此项的列中,将在服务器上引发一个事件。
///
公共bool自动回发
{
设置
{
自动回写=值;
}
得到
{
返回自动回邮;
}
}