【问题标题】:ASP.NET Details View. How to bind a value not in the Object Data SourceASP.NET 详细信息视图。如何绑定不在对象数据源中的值
【发布时间】:2015-05-09 05:27:17
【问题描述】:

我有一个绑定到对象数据源 (ODS) 的 ASP.NET DetailsView。我有用于 ODS OnSelecting 事件的方法,在此方法中,我们根据从对象数据源返回的字段进行另一个数据库查找以获得记录的“风险”值。

我的问题是如何绑定/评估并显示这个额外的“风险”字段,我一直觉得很困难,因为它没有将数据源绑定到详细信息视图。

我尝试将 Label 控件添加到 DetailsView ItemTemplate,但您无法向控件添加 ID 以在后面的代码中引用它。有任何想法吗?

更新这里的一些代码

这里有一些我正在尝试做的简单示例代码

 <asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="ObjectDataSource1">
        <Fields>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <div style="float: right">
                        <asp:Label  runat="server" Text="NEED RISK HERE"></asp:Label>
                    </div>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Button ID="ButtonUpdate" runat="server" CausesValidation="True" CommandName="Update"
                        Text="Update" />
                    &nbsp;
                    <asp:Button ID="ButtonCancelUpdates" runat="server" CausesValidation="False" CommandName="Cancel"
                        Text="Cancel" />
                </EditItemTemplate>
            </asp:TemplateField>
        </Fields>
    </asp:DetailsView>
    <br />
    <br />
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OnSelecting="ods1_Selecting"
        SelectMethod="GetMemoryDataTable" TypeName="AspMemoryDatatableDemo.MemoryDataTableProvider">
    </asp:ObjectDataSource>

代码背后

  public String GetRisk()
    {
        return "HIGH";
    }


    protected void ods1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
      // Get Risk based on row data
      // Need some way of showing this in the DetailsView

        GetRisk();
    }

数据集实际上来自数据实体,但在此示例中,我只使用内存数据表。

【问题讨论】:

    标签: asp.net binding detailview


    【解决方案1】:

    我会尽力为您提供帮助,但如果您向我们展示一些此类问题的代码,那将真的很有帮助。您是否尝试过向对象数据源添加列?我假设您有某种数据表(如果没有,请将其转换为数据表)。所以让我们添加一列并重新绑定它...


    HTML

    <div>
    
    <asp:GridView id="myGridView" AutoGenerateColumns="true" runat="server">
    
    </asp:GridView>
        <br />
        <asp:TextBox ID="column_name" runat="server"></asp:TextBox>
        <asp:Button ID="addColumn" Text="Add Column" runat="server" />
    </div>
    

    VB.NET

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    
        ' ===========================================================================
        '  This section is just to create a datatable. I assume you are already have one 
        '      that gets retrieved from a database somewhere.
        Dim dt As New DataTable ' create new test datatable
    
        With dt.Columns ' add columns to datatable
            .Add("id").AutoIncrement = True
            .Add("value")
        End With
    
        For i = 1 To 4 ' add some rows and values to the datatable.
            Dim msg = String.Empty
            Select Case i
                Case 1
                    msg = "this"
                Case 2
                    msg = "is"
                Case 3
                    msg = "a test"
                Case 4
                    msg = "table"
            End Select
            Dim r As DataRow = dt.NewRow()
            r("value") = msg
            dt.Rows.Add(r)
        Next i
        ' ===========================================================================
    
        myGridView.DataSource = dt ' set our datasource
        myGridView.DataBind() ' bind our data source
    
    End Sub
    
    
    Protected Sub addColumn_Click(sender As Object, e As EventArgs) Handles addColumn.Click
        Dim dt As DataTable = DirectCast(myGridView.DataSource, DataTable) ' get the current datatable used by your gridview.
        If Not String.IsNullOrWhiteSpace(column_name.Text) Then ' check if the new column name is null or empty.
            dt.Columns.Add(column_name.Text).DefaultValue = "new value" ' just here to add a default value. yours presumably will already have a value.
            myGridView.DataSource = dt ' update the datasource.
            myGridView.DataBind() ' bind the new datasource.
    
        End If
    End Sub
    

    添加列前的页面


    添加列后的页面

    【讨论】:

    • 谢谢。我已经用一些示例代码更新了我的问题,可能会有所帮助。我将 ODS 绑定到的数据集来自实体框架,只是一个数据库视图。我注意到在您的示例中,您向数据表添加了一个新列。我是否需要向我的实体添加一个新字段,或者在我的示例中是数据表
    • @Hevski 数据库“视图”仍然是正在返回的数据表。无论如何,我很确定在 dataview 或 gridview 中显示的任何内容都可以格式化为 DataTable,因此可以添加行和/或列。如果您想更好地控制所显示的内容,请创建一个 ASP Web API 控制器,在其中建立您的数据库连接并获取/填充数据表,然后使用 Newtonsoft 的 json.net 库对其进行序列化,将其返回给 API 调用者,然后然后通过javascript渲染表格。我很久以前就开始这样做而不是使用 ASP,而且效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 2023-04-07
    • 2015-12-06
    • 2010-09-28
    相关资源
    最近更新 更多