【问题标题】:Which control for this scenario?这种情况下的控制是什么?
【发布时间】:2013-02-27 19:53:34
【问题描述】:

我有一个包含 2 个下拉菜单、2 个文本框和一个按钮的页面。用户将从下拉列表中选择项目,然后在文本框中键入数据。这样做之后,他们将单击一个按钮以从这些控件中获取信息并填充“订单容器”。他们将能够输入多个“订单”。

  • Gridview 控件是否会成为此“订单容器”的路径?
  • Gridview 控件是否允许我插入多条记录?
  • Gridview 控件是否允许删除记录?

感谢您的帮助! 迈克

更新: 以下是我更新 gridview 的方式:

Protected Sub imgAddOrderItemClick(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgAddOrderItem.Click
    Dim qty As String 'Integer
    Dim type As String
    Dim product As String
    Dim price As Integer
    Dim count As Integer

    count = GridView1.Rows.Count
    type = ddlProductTypes.SelectedItem.ToString
    product = ddlProductFamilies.SelectedItem.ToString
    price = 11
    qty = TextBox10.Text


    ' Populate the datatable with your data (put this in appropriate loop)        
    dr = dt.NewRow        
    dr("Type") = type
    dr("Product") = product
    dr("Qty") = qty
    dr("Price") = price

    ' Add the row
    dt.Rows.Add(dr)

    dt.AcceptChanges()

    GridView1.DataSource = dt 'GetData()
    GridView1.DataBind()

End Sub

【问题讨论】:

  • 我不认为GridView 允许您插入记录。您最好查看ListView 控件。
  • 我已经能够向 Gridview 添加一行,但只能添加一行。如果我有第二行,它将覆盖现有行。
  • 我正在添加这样的新行:
  • 您可以使用 GridView 插入和删除多行。只需在代码隐藏中将行添加/删除到您的数据源。
  • 谢谢。我很感激。

标签: asp.net


【解决方案1】:

Gridview 可以正常工作。您最有可能看到您的行被覆盖,因为您的代码中的数据表 (dt) 在每次请求时都会被重新实例化。您需要做的是将该表保存在内存中(例如,将其放在 Session 中),从那里获取它,添加新行并重新绑定 GridView。像这样的:

Protected Sub imgAddOrderItemClick(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgAddOrderItem.Click
dt = Session("Data")

  If dt is Nothing Then
    ' Create your DT columns here
      Session.Add("Data",dt)
  End If

  'Add rows here and rebind
  dr = dt.NewRow        
  dr("Type") = type
  dr("Product") = product
  dr("Qty") = qty
  dr("Price") = price

  ' Add the row
  dt.Rows.Add(dr)

  dt.AcceptChanges()

  GridView1.DataSource = dt 'GetData()
  GridView1.DataBind()
End Sub

【讨论】:

  • 谢谢。我需要阅读 Session 和 Session 变量。请参阅上面的更新。
【解决方案2】:

GridView 很好!

你的 dt 来自哪里?

我认为问题在于,当您回帖时,您的 dt 会被初始化,因此它是空的。这就是为什么您每次只能获得一个(新)记录。有两种排序方式,

(1)。您必须在会话中保留 dt(或数据源)并且您的代码很好。

(2)。如果 dt 不在会话中,则需要先循环遍历 gridview 行和列以填充已添加的数据(如果有),然后添加新订单,最后将其绑定到 gridview。

希望有帮助!

【讨论】:

    猜你喜欢
    • 2013-04-23
    • 2017-02-15
    • 2023-03-28
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 2020-04-04
    • 2017-03-28
    • 2016-08-26
    相关资源
    最近更新 更多