刚刚在msdn上看了一下有关RowDataBound()事件的一些文章.具体网址是:http://msdn2.microsoft.com/zh-cn/library/system.web.ui.webcontrols.templatefield.itemtemplate(VS.80).aspx . (msdn网站可能有点慢等等拉)
我运行了其中那个示例的代码但是好象没有达到预期的效果.
在这个示例中
if (radio != null)
{
switch (e.Row.Cells[3].Text.Trim())// e.Row.Cells[3].Text.Trim()的值为空
case "business":
radio.SelectedIndex = 0;
break;
之后我又在msdn上找了一些有关RowDataBound()事件的代码,网址是http://msdn2.microsoft.com/zh-cn/library/system.web.ui.webcontrols.gridview.rowdatabound(VS.80).aspx#Mtps_DropDownFilterText
但这个示例中 e.Row.Cells[1].Text 又不为空 比较了两个程序又没有什么不同的 就是在第一个示例中<asp:boundfield datafield="type" visible="false"/> 它把"type"绑定列的visible="false"了 之后我把visible="false"去掉了 又是正确的! 不知道是什么原因难道msdn出错了?
我运行了其中那个示例的代码但是好象没有达到预期的效果.
在这个示例中
if (radio != null)
{
switch (e.Row.Cells[3].Text.Trim())// e.Row.Cells[3].Text.Trim()的值为空
case "business":
radio.SelectedIndex = 0;
break;
之后我又在msdn上找了一些有关RowDataBound()事件的代码,网址是http://msdn2.microsoft.com/zh-cn/library/system.web.ui.webcontrols.gridview.rowdatabound(VS.80).aspx#Mtps_DropDownFilterText
但这个示例中 e.Row.Cells[1].Text 又不为空 比较了两个程序又没有什么不同的 就是在第一个示例中<asp:boundfield datafield="type" visible="false"/> 它把"type"绑定列的visible="false"了 之后我把visible="false"去掉了 又是正确的! 不知道是什么原因难道msdn出错了?
我可是一点没有改哦!
<%@ Page language="C#" %><script runat="server"> void TitleGridView_RowDataBound (Object sender, GridViewRowEventArgs e)
{
// Get the RadioButtonList control from the row.
RadioButtonList radio = (RadioButtonList)e.Row.FindControl("TypeList");
// Select the appropriate option button based on the value
// of the Type field for the row. In this example, the Type
// field values are stored in the hidden column in the
// GridView control.
if (radio != null)
{
switch (e.Row.Cells[3].Text.Trim())
{
case "business":
radio.SelectedIndex = 0;
break; case "mod_cook":
radio.SelectedIndex = 1;
break; case "popular_comp":
radio.SelectedIndex = 2;
break; case "psychology":
radio.SelectedIndex = 3;
break; case "trad_cook":
radio.SelectedIndex = 4;
break; default:
radio.SelectedIndex = 5;
break;
}
}
}
</script><html>
<body>
<form runat="server">
<h3>TemplateField ItemTemplate Example</h3> <!-- Populate the Columns collection declaratively. -->
<!-- Create a custom TemplateField column that uses -->
<!-- two Label controls to display an author's first and -->
<!-- last name in the same column. -->
<asp:gridview id="TitleGridView"
datasourceid="TitleSqlDataSource"
autogeneratecolumns="false"
onrowdatabound="TitleGridView_RowDataBound"
runat="server">
<columns>
<asp:boundfield datafield="title"
headertext="Title"/>
<asp:boundfield datafield="price"
dataformatstring="{0:c}"
headertext="Price"/>
<asp:templatefield headertext="Type">
<itemtemplate>
<asp:radiobuttonlist id="TypeList"
datasourceid="TypeSqlDataSource"
datatextfield="type"
enabled="false"
runat="server"/>
</itemtemplate>
</asp:templatefield>
<asp:boundfield datafield="type"
visible="false"/>
</columns>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Pubs sample database. -->
<asp:sqldatasource id="TitleSqlDataSource"
selectcommand="SELECT [title], [price], [type] FROM [titles]"
connectionstring="server=localhost;database=pubs;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
<asp:sqldatasource id="TypeSqlDataSource"
selectcommand="SELECT Distinct [type] FROM [titles]"
connectionstring="server=localhost;database=pubs;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
visible属性只是表示隐藏或是否显示的属性.不是是否输出的属性.如果是这样微软的MSDN它会不知道么,它知道还会那么做么.
关于gridview隐藏列的问题网上提问多得很,如果用普通正常方法能取到,那么多程序员就不用问这个问题了