将 TemplateField 字段列动态添加到 GridView 控件
2009-11-23 15:39
417 查看
<%@
Page language="C#"
%>
<script
runat="server"
>
// Create a
dynamic template column
public
class
GridViewTextTemplate :
System.Web.UI.ITemplate
{
private
DataControlRowType
templateType;
private
string
columnName;
private
string
cId;
public
GridViewTextTemplate(DataControlRowType type, string
colname, string
controlId)
{
templateType =
type;
columnName =
colname;
cId =
controlId;
}
public
void
InstantiateIn(System.Web.UI.Control
container)
{
//
Create the content for the different row types.
switch
(templateType)
{
case
DataControlRowType.Header:
// Create the controls and set id properties to put in the
header
myHeadLiteral.ID =
cId;
myHeadLiteral.Text = "<B>"
+ columnName + "</B>"
;
container.Controls.Add(myHeadLiteral);
break
;
case
DataControlRowType.DataRow:
// Create the controls and set id properties to put in a
data row
TextBox myTextBox = new
TextBox();
myTextBox.ID =
cId;
myTextBox.DataBinding +=
new
EventHandler(this
.TextBoxDataBinding);
container.Controls.Add(myTextBox);
break
;
default
:
// Insert code to handle unexpected
values.
break
;
}
}
private
void
TextBoxDataBinding(Object sender, EventArgs e)
{
TextBox myTextBox =
(TextBox)sender;
GridViewRow row =
(GridViewRow)myTextBox.NamingContainer;
myTextBox.Text =
System.Web.UI.DataBinder.Eval(row.DataItem,
columnName).ToString();
}
}
void
Page_Load(Object sender, EventArgs e)
{
// Create
the field columns when the page is first loaded.
if
(!IsPostBack)
{
TemplateField
tField;
//
Create the dynamic templates
tField = new
TemplateField();
tField.ItemTemplate = new
GridViewTextTemplate(DataControlRowType.DataRow,
"parameter_description"
, "textParameterDesc"
);
tField.HeaderTemplate = new
GridViewTextTemplate(DataControlRowType.Header,
"
说明"
,
"textParameterDescHd"
);
// Add
the dynamic templates field column to the GridView
GridView1.Columns.Add(tField);
}
}
</script>
<html>
<body>
<form id="Form1"
runat="server"
>
<asp:gridview id="GridView1"
datasourceid="TestSqlDataSource"
autogeneratecolumns="False"
runat="server"
>
</asp:gridview>
<asp:sqldatasource id="TestSqlDataSource"
selectcommand="Select * From
Parameter_Lines"
connectionstring="server=localhost;database=Test;integrated
security=SSPI"
runat="server"
>
</asp:sqldatasource>
</form>
</body>
</html>
from:http://www.cnblogs.com/Fortunelee/articles/505044.html
Page language="C#"
%>
<script
runat="server"
>
// Create a
dynamic template column
public
class
GridViewTextTemplate :
System.Web.UI.ITemplate
{
private
DataControlRowType
templateType;
private
string
columnName;
private
string
cId;
public
GridViewTextTemplate(DataControlRowType type, string
colname, string
controlId)
{
templateType =
type;
columnName =
colname;
cId =
controlId;
}
public
void
InstantiateIn(System.Web.UI.Control
container)
{
//
Create the content for the different row types.
switch
(templateType)
{
case
DataControlRowType.Header:
// Create the controls and set id properties to put in the
header
myHeadLiteral.ID =
cId;
myHeadLiteral.Text = "<B>"
+ columnName + "</B>"
;
container.Controls.Add(myHeadLiteral);
break
;
case
DataControlRowType.DataRow:
// Create the controls and set id properties to put in a
data row
TextBox myTextBox = new
TextBox();
myTextBox.ID =
cId;
myTextBox.DataBinding +=
new
EventHandler(this
.TextBoxDataBinding);
container.Controls.Add(myTextBox);
break
;
default
:
// Insert code to handle unexpected
values.
break
;
}
}
private
void
TextBoxDataBinding(Object sender, EventArgs e)
{
TextBox myTextBox =
(TextBox)sender;
GridViewRow row =
(GridViewRow)myTextBox.NamingContainer;
myTextBox.Text =
System.Web.UI.DataBinder.Eval(row.DataItem,
columnName).ToString();
}
}
void
Page_Load(Object sender, EventArgs e)
{
// Create
the field columns when the page is first loaded.
if
(!IsPostBack)
{
TemplateField
tField;
//
Create the dynamic templates
tField = new
TemplateField();
tField.ItemTemplate = new
GridViewTextTemplate(DataControlRowType.DataRow,
"parameter_description"
, "textParameterDesc"
);
tField.HeaderTemplate = new
GridViewTextTemplate(DataControlRowType.Header,
"
说明"
,
"textParameterDescHd"
);
// Add
the dynamic templates field column to the GridView
GridView1.Columns.Add(tField);
}
}
</script>
<html>
<body>
<form id="Form1"
runat="server"
>
<asp:gridview id="GridView1"
datasourceid="TestSqlDataSource"
autogeneratecolumns="False"
runat="server"
>
</asp:gridview>
<asp:sqldatasource id="TestSqlDataSource"
selectcommand="Select * From
Parameter_Lines"
connectionstring="server=localhost;database=Test;integrated
security=SSPI"
runat="server"
>
</asp:sqldatasource>
</form>
</body>
</html>
from:http://www.cnblogs.com/Fortunelee/articles/505044.html
相关文章推荐
- 将 TemplateField 字段列动态添加到 GridView 控件
- 将 TemplateField 字段列动态添加到 GridView 控件
- [C#] 动态生成GridView的模版列TemplateField,并在模版列TemplateField中添加复选框CheckBox
- GridView TemplateField 动态添加取消颜色
- GridView 动态添加模板列并绑定数据 实现从外部直接传入控件 和 绑定字段参数
- ASP.NET2.0中的GRIDVIEW控件在使用TemplateField中的LinkButton时如何在RowCommand事件中找到当前行index的方法
- 探讨为GRIDVIEW中的控件动态添加事件
- 在GridView控件FooterTemplate内添加记录 Ver3
- GridView中动态添加模板列和其子控件集合
- 关于boostrapValidator动态添加字段(addField)验证的bug
- GridView 动态列上方添加相应的Combox等控件
- 动态绑数据(GridView控件Header和ItemTemplate)
- Gridview:在进入编辑模式后动态添加控件
- 在GridView控件中的HeaderTemplate模板上添加的CheckBox控件无法引发AutoPostback事件,或无规律的引发,不正常的引发
- ASP.NET 2.0数据教程之十二:在GridView控件中使用TemplateField(转)
- .Net 后台获取GridView里<TemplateField>的控件值是否变化
- 在GridView控件FooterTemplate内添加记录 Ver2
- GridView中 对数据编辑打开一个新页面 & 动态添加控件并获取其值
- GridView动态创建模板列templateField
- GridView中动态添加模板列和其子控件集合