我有一个绑定到对象数据源(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" />
<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();
}
数据集实际上来自数据实体,但在本例中我只使用内存数据表。
答案 0 :(得分:0)
我会尽力帮助你,但如果你向我们展示一些像这样的问题的代码,那将会非常有帮助。您是否尝试过向对象数据源添加列?我假设您有某种数据表(如果没有,请将其转换为数据表)。所以,让我们只添加一个列并重新绑定它......
<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>
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