如何从code访问模板列的背后 [英] how to access templatefield from code behind
问题描述
为什么我期待UDPATE dynaic的原因是因为我使用的ObjectDataSource和ObjectDataSource控件我有对象的集合,该对象中我还有一个目标,我想访问所以一个例子:
+学生
......
......
......
-培训班
.........
.........
名称
更新结束
如何填写从code-背后templatefiled?
< ASP:GridView的ID =gridview1=服务器>
<列>
< ASP:的TemplateField的HeaderText =名称SORTEX pression =名称>
<&ItemTemplate中GT;
< / ItemTemplate中>
< / ASP:的TemplateField>< /列>
< / ASP:GridView的>
首先定义键字段在GridView控件,只需添加网络属性的GridView的标记:的DataKeyNames =StudentID
。
您可以使用两种GridView的事件处理程序:的RowDataBound或RowCreated。只需添加此事件处理程序之一,并发现有控制被放置在你的ItemTemplate。这里一样,例如:
无效ProductsGridView_RowCreated(对象发件人,GridViewRowEventArgs E)
{ 如果(e.Row.RowType == DataControlRowType.DataRow)
{
//检索从第一塔LinkButton控件。
标签someLabel =(标签)e.Row.FindControl(someLabel);
如果(someLabel!= NULL)
{
//获取学生的索引
INT StudentId =(int)的GridView.DataKeys [e.Row.RowIndex] .Values [0];
//设置标签文本
//这里定义关于当前学生证所有课程
someLabel.Text = //
}
} }
这个例子来自得到MSDN
the reason why i am looking to udpate dynaic is because i am using objectdatasource and my objectdatasource have a collection of object and within that object i have another object that i wanted to access so for an example:
+Student
......
......
......
-Courses
.........
.........
Name
Update end
how do i fill templatefiled from code-behind?
<asp:Gridview ID="gridview1" runat="Server">
<columns>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:Gridview>
First of all define your key field in GridView control, just add net attribute to GridView markup: datakeynames="StudentID"
.
You can use both event handler for GridView: RowDataBound or RowCreated. Just add one of this event handler and find there control that is placed in your ItemTemplate. Like here, for instance:
void ProductsGridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the LinkButton control from the first column.
Label someLabel = (Label)e.Row.FindControl("someLabel");
if (someLabel != null)
{
// Get Student index
int StudentId = (int)GridView.DataKeys[e.Row.RowIndex].Values[0];
// Set the Label Text
// Define here all the courses regarding to current student id
someLabel.Text = //
}
}
}
This example was gotten from MSDN
这篇关于如何从code访问模板列的背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!