<ASP:RadioButton id="chkVisible" GroupName="Col2Visible" runat="server"
AutoPostback="True" /> Visible
<ASP:RadioButton id="chkNotVisible" GroupName="Col2Visible" runat="server"
AutoPostback="True" /> Hidden<p />
<ASP:DataGrid id="MyDataGrid" runat="server"
AutoGenerateColumns="False" //注意这儿
CellPadding="5"
GridLines="None"
HeaderStyle-BackColor="silver"
HeaderStyle-HorizontalAlign="center"
FooterStyle-BackColor="silver"
ShowFooter="True"
OnItemCommand="ShowInfo">
<!--此处声明了itemCommand事件处理函数,用于处理button控件的点击-->
<Columns>//用此元素指定要显示的栏
<ASP:TemplateColumn HeaderText="" ItemStyle-BackColor="silver">
<ItemTemplate> </ItemTemplate>
</ASP:TemplateColumn>
//第一栏为空
<ASP:BoundColumn HeaderText="<b>Code</b>" DataField="ISBN" ItemStyle-BackColor="lightblue" />
<ASP:BoundColumn HeaderText="<b>Book Title</b>" DataField="Title"/>
<ASP:BoundColumn HeaderText="<b>Released</b>" DataField="PublicationDate" DataFormatString="{0:D}" ItemStyle-HorizontalAlign="right" ItemStyle-BackColor="yellow" />
//使用datafield属性指定数据绑定源中的某个字段
<ASP:TemplateColumn HeaderText="" ItemStyle-BackColor="lightblue">
<ItemTemplate>
<ASP:Button id="cmdInfo" Text="More Info" CommandName="Info" runat="server" />
</ItemTemplate>
</ASP:TemplateColumn>
//上面在第五栏放置asp:button,设置commmandnamw为info
<ASP:TemplateColumn HeaderText="<b>Buy Now</b>" ItemStyle-BackColor="silver" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<ASP:CheckBox id="chkBuy" runat="server" />
</ItemTemplate>
</ASP:TemplateColumn>
//此栏放置了checkbox。使用TemplateColumn和ItemTemplate可以添加不属于原来数据集部分的附加栏
</Columns>
以下是脚本:
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
if (Page.IsPostBack)//判断是否显示第4栏(从1开始数)
{
// display or hide the "Release Date" column
MyDataGrid.Columns[3].Visible = (chkVisible.Checked == true);
}
else
{
chkVisible.Checked = true; // set default value
BindDataGrid(); // create data set and bind grid
}
}
//响应itemcommand事件
//Object objSender是包含对触发该事件的控件的引用,DataGridCommandEventArgs objArgs对象包含该事件以及对该控件中当前行(即包含被激活的控件的行)的引用的详细资料。
void ShowInfo(Object objSender, DataGridCommandEventArgs objArgs )
{
// runs when any command button in the grid is clicked
// see if the CommandName of the clicked button was "Info"
if (objArgs.CommandName == "Info")//使用commandname属性来判断是button
{
// get values of ISBN and Title from Text property of the table cells
// for the current row returned in the objArgs parameter values
//得到被激发控件所在行的第二,三栏的值
string strISBN = objArgs.Item.Cells[1].Text;
string strTitle = objArgs.Item.Cells[2].Text;
// display the informaion in the page - possibly extract from database?
lblInfo.Text = "More information about the book:<br /><b>" + strTitle
+ "</b><br />(ISBN " + strISBN + ") goes here...";
}
}
void BindDataGrid()
{
// get connection string from ../global/connect-strings.ascx user control
string strConnect = ctlConnectStrings.OLEDBConnectionString;
// create a SQL statement to select some rows from the database
string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '%18610023%'";
// create a variable to hold an instance of a DataReader object
OleDbDataReader objDataReader;
try
{
// create a new Connection object using the connection string
OleDbConnection objConnect = new OleDbConnection(strConnect);
// open the connection to the database
objConnect.Open();
// create a new Command using the connection object and select statement
OleDbCommand objCommand = new OleDbCommand(strSelect, objConnect);
// execute the SQL statement against the command to get the DataReader
objDataReader = objCommand.ExecuteReader();
}
catch (Exception objError)
{
// display error details
outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
+ objError.Message + "<br />" + objError.Source + "<p />";
return; // and stop execution
}
// set the DataSource property of the DataList
MyDataGrid.DataSource = objDataReader;
// and bind the control to the data
MyDataGrid.DataBind();
}
</script>
我的理解:当页面首次加载以及每次用户点击单选按钮时就会触发page_load,
但若是用户单击datagrid中的button时则仅仅是调用showinfo事件,并不触发page_load
MyDataGrid.Columns[3].Visible
objArgs.CommandName == "Info"
string strISBN = objArgs.Item.Cells[1].Text;