2014-04-07 39 views
0

我想从后面的代码作为数组访问我的所有模板字段值。如何获取gridview中的templatefield值作为数组

<asp:TemplateField HeaderText="S.No" meta:resourcekey="TemplateFieldResource1"> 
    <ItemTemplate> 
     <asp:Label ID="Label1" runat="server" Text="<%# (Container.DataItemIndex + 1) %>"></asp:Label> 
    </ItemTemplate> 
    <ItemStyle Width="50px" /> 
</asp:TemplateField> 

我想绑定默认文本与来自下面的值。

protected void grdOffice_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     Label l = (Label)e.Row.FindControl("Label1"); 
     //get list of number array from label1 
     //and replace each number with another value 
    } 
} 
+0

我不太清楚你在做什么... Label控件的Text属性是字符串而不是数组。你的问题很混乱 – Leo

+0

我想得到我的网格的S.No. templatefield 1,2,3 ..以数组形式表示为{“1”,“2”,“3”..}并将它们替换为其他值。 – user3488717

回答

0

我不知道你想实现什么,但如果你想在一个数组你S.Noβ-细胞的值,你可以尝试利用DataBound事件您GridView

protected void gv_DataBound (object sender, EventArgs e) 
{ 
    string[] ids = this.gv.Rows 
     .OfType<GridViewRow>() 
     .Where (r => r.RowType == DataControlRowType.DataRow) 
     .Select (r => ((Label)r.FindControl ("Label1")).Text) 
     .ToArray(); 
} 

请注意,你不改变你的标签的文本,如果数组中改变这些值。

相关问题