DetailsView故障检索值

时间:2014-07-22 19:56:15

标签: asp.net

我的详细信息视图的目的是收集用户在运行报表时输入的报表参数。因此,我动态构建详细信息视图,因为报表参数是数据驱动的,根据报表存储在数据库中。 绑定的对象是在运行时从报表参数动态反射创建的,每个参数都有一个属性,因此它包含简单的日期或集合属性,我通过动态创建模板动态转换为组合框或日期框

在item_updating事件中,我只想获取用户输入的值,因此我知道如何运行报告。 我查看了e的所有属性,并尝试使用findcontrol来获取用户输入的值。 e和findcontrol都是空的。这就是我过去获取数据的方式所以我不知道他们为什么不在那里。这几乎就像当时控件本身没有在详细信息视图的对象模型中表示一样。

  <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="false" DefaultMode="Edit" >
    <Fields>
      <asp:CommandField ButtonType="Button" UpdateText="Run" ShowCancelButton="false" ShowEditButton="true" />
    </Fields>
  </asp:DetailsView>

背后的代码

  Private Sub BuildDetailView(DataSource As Object)


    Dim Properties() As System.Reflection.PropertyInfo = DataSource.GetType.GetProperties
    Dim Template As System.Web.UI.ITemplate
    For Each PropertyInfo As System.Reflection.PropertyInfo In Properties
      Template = Nothing
      If PropertyInfo.PropertyType Is GetType(System.DateTime) Then
        Template = New DateTemplate(DataSource, PropertyInfo)
      ElseIf GetType(ICollection).IsAssignableFrom(PropertyInfo.PropertyType) Then
        Template = New ListTemplate(DataSource, PropertyInfo, Report.InputReportParameters(Array.IndexOf(Properties, PropertyInfo)).Enumeration.MultiSelect)
      End If
      If Template IsNot Nothing Then
        Dim TemplateField As New TemplateField
        TemplateField.HeaderText = PropertyInfo.Name
        TemplateField.ItemTemplate = Template
        DetailsView1.Fields.Add(TemplateField)
      End If
    Next
  End Sub

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    CheckAuthentication()

    BuildDetailView(Report.ReportParametersDataObject)

    If Report.ReportParameters.Any AndAlso Not Report.InputReportParameters.Any Then
      Run() 'no parameters are for data entry

    ElseIf Not IsPostBack Then
      lblReportType.Text = String.Format("Report {0}", Report.Type)
      DetailsView1.DataSource = New List(Of Object) From {Report.ReportParametersDataObject}
      DetailsView1.DataBind()
    End If
  End Sub

  Protected Sub DetailsView1_ItemUpdating(sender As Object, e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
    Run(e.NewValues)
  End Sub

列表模板助手:

Public Class ListTemplate
  Implements System.Web.UI.ITemplate

  Public Sub New(DataSource As Object, PropertyInfo As System.Reflection.PropertyInfo, Multiselect As Boolean)
    Me.DataSource = DataSource
    Me.PropertyInfo = PropertyInfo
    Me.Multiselect = Multiselect
  End Sub

  Private DataSource As Object
  Private Multiselect As Boolean
  Private PropertyInfo As System.Reflection.PropertyInfo

  Public Sub InstantiateIn(container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
    Dim ListBox As New System.Web.UI.WebControls.ListBox
    ListBox.DataSource = PropertyInfo.GetValue(DataSource, Nothing)
    ListBox.DataTextField = "Text"
    ListBox.DataValueField = "Value"
    ListBox.SelectionMode = IIf(Multiselect, ListSelectionMode.Multiple, ListSelectionMode.Single)
    ListBox.Rows = Math.Min(25, ListBox.DataSource.count)
    ListBox.EnableViewState = True
    container.Controls.Add(ListBox)
  End Sub
End Class

DateTemplate Helper:

Public Class DateTemplate
  Implements System.Web.UI.ITemplate

  Public Sub New(DataSource As Object, PropertyInfo As System.Reflection.PropertyInfo)
    Me.DataSource = DataSource
    Me.PropertyInfo = PropertyInfo
  End Sub

  Private DataSource As Object
  Private PropertyInfo As System.Reflection.PropertyInfo

  Public Sub InstantiateIn(container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
    Dim Textbox As New System.Web.UI.WebControls.TextBox
    Textbox.ID = PropertyInfo.Name.Replace(" ", String.Empty)
    Textbox.EnableViewState = True
    Dim CalendarExtender As New AjaxControlToolkit.CalendarExtender
    CalendarExtender.TargetControlID = Textbox.ID
    CalendarExtender.SelectedDate = PropertyInfo.GetValue(DataSource, Nothing)
    CalendarExtender.DefaultView = AjaxControlToolkit.CalendarDefaultView.Months
    CalendarExtender.Format = "MMMM yyyy"
    container.Controls.Add(Textbox)
    container.Controls.Add(CalendarExtender)
  End Sub
End Class

1 个答案:

答案 0 :(得分:0)

由于没有任何答案甚至评论,我通过创建自己的asp.net自定义控件解决了这个问题。

它的工作方式非常简单。它有一个DataSource属性,它创建UI为每个属性添加一个编辑小部件。根据propertyinfo.propertytype,它为该类型创建一个编辑器。

一旦用户提交更改,它就会引发一个服务器端事件,回复包含更改值的数据源对象。

它很简单,即使我能理解如何使用它。