1

嗨,我有以下网格视图

  <asp:GridView ID="grdSettings" runat="server" Height="400px" Width="100%"
                        AutoGenerateColumns="false" >
                        <Columns>
                            <asp:TemplateField HeaderText="XYZ"  BoundFieldName="XYZTypeName">
                                <ItemTemplate>
                                    <asp:Label ID="lblCustomerName" runat="server" Text='<%# Bind("CustomerName") %>'>
                                    </asp:Label>

                                        <asp:Label ID="lblCostumerId" runat="server" Text='<%#Bind("CustomerId") %>'></asp:Label>

                            </asp:TemplateField>
                        </Columns>
                     </asp:GridView>   

受客户列表约束,类如下

Class Customer
{
  public string CustomerName { get; set; }
  public int CustomerId { get; set; }
} 

现在在名为 GetGridStuff() 的方法上,我需要在每一列中迭代并获取绑定到模板字段中控件的类型。例如,在模板字段中的第一个控件的情况下

 <asp:Label ID="lblCustomerName" runat="server" Text='<%# Bind("CustomerName") %>' >
                                    </asp:Label>

我需要知道它包含什么类型的属性数据,在本例中是 CustomerName。我需要在运行时动态获取它,以便在不知道该网格结构的程序的一部分中编写代码。我有网格对象,我可以访问所有属性。

4

1 回答 1

1

让我试试这个,现在已经 5 年多没有编码了,如果我正确理解了这个问题,你可能想通过服务器端代码来处理它。

查找一个名为ItemBound(或类似的事件,请原谅我的记忆),这将为您提供当前行(属性)中所有项目的值。您还需要声明临时控件类型(标签、文本框等)并使用e.FindControl适当的类型转换为这些控件分配相应的值,例如Label l = (Label)e.Findcontrol("Name")

该方法的缺点是您应该避免过多的过程,因为它将在创建网格的每一行上执行。

如果您仍然需要精确的代码来处理问题,请告诉我,否则此描述至少应该可以帮助您寻找行级事件来破解问题,并鼓励您坚持使用技术论坛 :)

于 2013-08-12T11:39:35.483 回答