100分!在线急等: DataGrid更新中CType(e.Item.Cells(2).Controls(0), TextBox).Text获值问题

luckin 2004-09-19 04:01:16
在页面只有一个DataGrid的时候,可以很好的更新。在一个页面里有4,5个DataGrid的时候,用CType(e.Item.Cells(2).Controls(0), TextBox).Text获得的值却是原来的值,不是TextBox里面的新值。
请高手指教,附上aspx.vb


Imports System.Data.SqlClient
Public Class xgService
Inherits System.Web.UI.UserControl

#Region " Web 窗体设计器生成的代码 "

'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents SelectedCarService As System.Web.UI.WebControls.DataGrid
Protected WithEvents CarServiceDg As System.Web.UI.WebControls.DataGrid
Protected WithEvents SaveBtn As System.Web.UI.WebControls.Button
Protected WithEvents YeWuIDTxt As System.Web.UI.WebControls.Label

'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub

#End Region
Dim conn As New SqlConnection(ConfigurationSettings.AppSettings("connectionstring"))
Dim ds As New DataSet
Dim carDa As New SqlDataAdapter
Dim carSelect As DataTable
Dim carserV As DataView

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
Dim idstr As String = Request.Params("id")
YeWuIDTxt.Text = "业务编号" & idstr & "的服务"
'运输类服务
Dim CarServiceDa As New SqlDataAdapter("SELECT Resource_Name, Price FROM Resource_Table WHERE (Resource_Type = '运输类')", conn)
CarServiceDa.Fill(ds, "carservice")
CarServiceDg.DataSource = ds.Tables("carservice")
CarServiceDg.DataBind()
'客户选择的运输类服务
getsource()
binddata()

End Sub

Sub CarServiceDg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles CarServiceDg.ItemCommand
Dim dr As DataRow = carSelect.NewRow
dr("resource_name") = e.Item.Cells(0).Text
dr("price") = e.Item.Cells(1).Text
dr("quantity") = "1"
carSelect.Rows.Add(dr)
binddata()
End Sub


Sub SelectedCarService_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles SelectedCarService.ItemCommand
Dim ResourceName As String = e.Item.Cells(0).Text
If CType(e.CommandSource, LinkButton).CommandName = "deleteservice" Then
carserV.RowFilter = "resource_name='" & ResourceName & "'"
If carserV.Count > 0 Then
carserV.Delete(0)
Else
carserV.RowFilter = ""
End If
End If
getsource()
binddata()
End Sub


Sub getsource()
Dim idstr As String = Request.Params("id")
carDa.SelectCommand = New SqlCommand("SELECT Resource_Name, Price, Quantity FROM Service_Table WHERE (Ye_Wu_ID = '" & idstr & "') AND (Resourse_Type = '运输类服务')", conn)
If Session("carservice") Is Nothing Then
carDa.Fill(ds, "carselect")
carSelect = ds.Tables("carselect")
Session("carservice") = carSelect
Else
carSelect = CType(Session("carservice"), DataTable)
End If
carserV = New DataView(carSelect)
End Sub


Sub binddata()
SelectedCarService.DataSource = carserV
SelectedCarService.DataBind()
End Sub

Sub SelectedCarService_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles SelectedCarService.EditCommand
SelectedCarService.EditItemIndex = e.Item.ItemIndex
binddata()
End Sub

Sub SelectedCarService_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles SelectedCarService.CancelCommand
SelectedCarService.EditItemIndex = -1
binddata()
End Sub


Sub SelectedCarService_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles SelectedCarService.UpdateCommand
Dim resourceName As String = e.Item.Cells(0).Text
Dim price As String = e.Item.Cells(1).Text
Dim quantity As String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
'这里无法获得新值
carserV.RowFilter = "resource_name='" & resourceName & "'"
If carserV.Count > 0 Then
carserV.Delete(0)
Else
carserV.RowFilter = ""
End If

Dim dr As DataRow = carSelect.NewRow
dr("resource_name") = resourceName
dr("price") = price
dr("quantity") = quantity
carSelect.Rows.Add(dr)
SelectedCarService.EditItemIndex = -1
getsource()
binddata()
End Sub
End Class
...全文
180 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
luckin 2004-09-19
  • 打赏
  • 举报
回复
这个方法我试了
这段代码本身没有问题
但是我把几个这样的DataGrid放在一起,就不行了
要更新的DataGrid是在getsource() binddata()里面帮定的数据


swzlxm 2004-09-19
  • 打赏
  • 举报
回复
ctype(e.item.findcontrol("controlname"),textbox)
savagewang1978 2004-09-19
  • 打赏
  • 举报
回复
If Not IsPostBack Then

'在此处放置初始化页的用户代码
Dim idstr As String = Request.Params("id")
YeWuIDTxt.Text = "业务编号" & idstr & "的服务"
'运输类服务
Dim CarServiceDa As New SqlDataAdapter("SELECT Resource_Name, Price FROM Resource_Table WHERE (Resource_Type = '运输类')", conn)
CarServiceDa.Fill(ds, "carservice")
CarServiceDg.DataSource = ds.Tables("carservice")
CarServiceDg.DataBind()
'客户选择的运输类服务
getsource()
binddata()

End If
exboy 2004-09-19
  • 打赏
  • 举报
回复
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

-- 下面的代码一定要放在 IsPostBack 里面

If Not IsPostBack Then

'在此处放置初始化页的用户代码
Dim idstr As String = Request.Params("id")
YeWuIDTxt.Text = "业务编号" & idstr & "的服务"
'运输类服务
Dim CarServiceDa As New SqlDataAdapter("SELECT Resource_Name, Price FROM Resource_Table WHERE (Resource_Type = '运输类')", conn)
CarServiceDa.Fill(ds, "carservice")
CarServiceDg.DataSource = ds.Tables("carservice")
CarServiceDg.DataBind()
'客户选择的运输类服务
getsource()
binddata()

End If

End Sub
luckin 2004-09-19
  • 打赏
  • 举报
回复
他的功能是点按钮后,把一个DataGrid的内容,放入第二个DataGrid,并可以在第二个DataGrid里面修改值

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧