【问题标题】:Accessing SqlDataSource result in code behind for conversion访问 SqlDataSource 会导致代码落后以进行转换
【发布时间】:2014-06-30 17:21:10
【问题描述】:

我的 ASP.NET 页面上有 SqlDataSource,并将其加载到 Telerik 的 RadGrid 控件中。

<asp:SqlDataSource 
    ID="DataSource" 
    runat="server"    
    ConnectionString="<%$ ConnectionStrings:ServerConnectionString %>" 
    SelectCommand="SELECT [Timestamp], [Label], [Project], [Product], [Value], [Reference] FROM [Operation]">
</asp:SqlDataSource

我的字段 [Value] 是一个表示枚举的字节值。当我将数据加载到网格中时,我想将这些字节值转换为它们的枚举等价物,换句话说,我想要明文形式。

我尝试在 SqlDataSource 组件的 OnSelected 和 OnLoad() 事件中做一些事情,但没有成功。有没有办法在渲染之前访问来自 SqlDataSource 的代码中的数据?

【问题讨论】:

    标签: asp.net telerik-grid sqldatasource


    【解决方案1】:

    您可以查看RadGrid 的ItemDataBound 事件,它类似于GridView 中的RowDataBound 事件。例如:

    ASPX:

    <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="DataSource"
                     OnItemDataBound="RadGrid1_ItemDataBound" >  
    

    C# 代码隐藏:

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)     
    {  
        if (e.Item is GridDataItem) // get the row collection of cells/items
        {  
            GridDataItem dataItem = (GridDataItem)e.Item; 
    
            // here we are converting the TEXT value to INT of the "VALUE" column
    
            if(int.Parse(dataItem["Value"].Text) == 1)
                dataItem["Value"].Text = "A"; // here you can set cell value as you like
        }
    }  
    

    【讨论】:

      猜你喜欢
      • 2017-08-22
      • 2019-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      相关资源
      最近更新 更多