N
Nu2ASP.NET
I have the following code in my code behind page:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace CheckBoxTest
{
/// <summary>
/// Summary description for WebForm1.
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
SqlConnection myConnection;
private void Page_Load(object sender, System.EventArgs e)
{
myConnection = new SqlConnection
("Server=localhost;uid=sa;pwd=preview;database=pubs");
if(!IsPostBack)
BindGrid();
}
private void MyDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
CheckBox cb = e.Item.FindControl("contract") as CheckBox;
if(cb != null)
{
if(DataBinder.Eval(e.Item.DataItem,
"contract")!=DBNull.Value)
{
if(Convert.ToBoolean(DataBinder.Eval(e.Item.DataItem,
"contract")) )
cb.Checked = true;
else
cb.Checked = false;
}
}
}
public void BindGrid()
{
SqlDataAdapter dAdapter = new SqlDataAdapter ("SELECT *
FROM authors ",
myConnection);
DataSet dSet= new DataSet();
dAdapter.Fill(dSet, "authors");
MyDataGrid.DataSource =
dSet.Tables["authors"].DefaultView;
MyDataGrid.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.MyDataGrid.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler
(this.MyDataGrid_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
The problem I'm having is that the checkbox is not properly reflecting
the state of the underlying data field, which, in this case, is an SQL
2000 bit type. All of the checkboxes appear unchecked. (I want the
check box to appear checked if the underlying data field contains a "1"
and unchecked if it contains a "0".)
I added the event handler for the ItemDataBound event to
InitializeComponent, since that's where it seemed to make the most
sense.
Am I missing something *really* obviously wrong here?
Thanks!!!
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace CheckBoxTest
{
/// <summary>
/// Summary description for WebForm1.
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
SqlConnection myConnection;
private void Page_Load(object sender, System.EventArgs e)
{
myConnection = new SqlConnection
("Server=localhost;uid=sa;pwd=preview;database=pubs");
if(!IsPostBack)
BindGrid();
}
private void MyDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
CheckBox cb = e.Item.FindControl("contract") as CheckBox;
if(cb != null)
{
if(DataBinder.Eval(e.Item.DataItem,
"contract")!=DBNull.Value)
{
if(Convert.ToBoolean(DataBinder.Eval(e.Item.DataItem,
"contract")) )
cb.Checked = true;
else
cb.Checked = false;
}
}
}
public void BindGrid()
{
SqlDataAdapter dAdapter = new SqlDataAdapter ("SELECT *
FROM authors ",
myConnection);
DataSet dSet= new DataSet();
dAdapter.Fill(dSet, "authors");
MyDataGrid.DataSource =
dSet.Tables["authors"].DefaultView;
MyDataGrid.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.MyDataGrid.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler
(this.MyDataGrid_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
The problem I'm having is that the checkbox is not properly reflecting
the state of the underlying data field, which, in this case, is an SQL
2000 bit type. All of the checkboxes appear unchecked. (I want the
check box to appear checked if the underlying data field contains a "1"
and unchecked if it contains a "0".)
I added the event handler for the ItemDataBound event to
InitializeComponent, since that's where it seemed to make the most
sense.
Am I missing something *really* obviously wrong here?
Thanks!!!