开发者

how to access templatefield from code behind

the reason why i am looking to update dynamic 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 t开发者_JAVA百科o access so for an example:

+Student
  ......
  ......
  ......
  -Courses
    .........
    .........
    Name

Update end

how do i bind templatefield 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


Here are some code samples from MSDN:

http://msdn.microsoft.com/en-us/library/aa479353.aspx

These are in VB but you should be able to locate C# also :-)

If you follow this link and scroll down you will find a code sample:

http://bytes.com/topic/asp-net/answers/624380-gridview-generated-programmatically

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜