<Columns>
<asp:TemplateColumn Visible="False" HeaderText="id">
<HeaderStyle Width="30%"></HeaderStyle>
<ItemTemplate>
<asp:Label id="lblID" Text="<%# Container.DataItem(0) %>" Runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="所属区" SortExpression="district">
<HeaderStyle Width="30%"></HeaderStyle>
<ItemTemplate>
<asp:Label id=lblDistrict Text="<%# Container.DataItem(1) %>" Runat="server">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=txtDistrict Text="<%# Container.DataItem(1) %>" Runat="server">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn SortExpression="name" HeaderText="名称">
<HeaderStyle Width="40%"></HeaderStyle>
<ItemTemplate>
<asp:Label ID="lblName" Runat="server" Text='<%# Container.DataItem(2) %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" Runat="server" Text='<%# Container.DataItem(2) %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="修改">
<HeaderStyle Width="15%"></HeaderStyle>
</asp:EditCommandColumn>
<asp:ButtonColumn Text="&lt;div onclick=&quot;return confirm('您真的要删除该条信息吗?')&quot;&gt;删除&lt;/div&gt;" CommandName="Delete">
<HeaderStyle Width="15%"></HeaderStyle>
</asp:ButtonColumn>
</Columns>
上面是dataGrid里的列,id列是隐藏的,所属区和名称是可以修改的列,现在修改完后点击“更新”按钮,获得的textBox值仍然是修改前所绑定的值。UpdateCommand()事件如下:
    Private Sub dbGridfwt_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dbGridfwt.UpdateCommand
        Dim s_id As String
        Dim strSql, sName, sDistrict As String
        Dim resultID As Integer        s_id = CType(e.Item.Cells(0).Controls(1), Label).Text
        sDistrict = CType(e.Item.Cells(1).Controls(1), TextBox).Text
        sName = CType(e.Item.Cells(2).Controls(1), TextBox).Text
        If Trim(sDistrict) = "" Or Trim(sName) = "" Then
            Exit Sub
        End If
        ………………
        dbGridfwt.EditItemIndex = -1
        dbGridfwt.DataBind()
    End Sub
如何才能获得输入的新值呢??

解决方案 »

  1.   

    s_id = CType(e.Item.Cells(0).Controls(1), Label).Text
    最好不要这样用
    用FindControls来查找控件~
      

  2.   

    如何才能获得输入的新值呢??
    在你的Page_Load里面
    加上条件
    if not Page.IsPostBack then     //绑定你的Grid
      end if
      

  3.   

    如果你Page_Load里面没有这个条件
    你填写完之后,页面PostBack
    重新绑定数据
    就把你新填写的冲掉了
      

  4.   

    我的Page_Load里这样写的:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If IsPostBack Then
                dbTable = Session("dbTable")
                dbView = Session("dbView")
                dbGridfwt.DataSource = dbView
                dbGridfwt.DataBind()
    end if这样不行么?
      

  5.   

    to: wangdequan1024(紫夜) 
    用FindControls怎么查找dataGrid里的控件呢?
      

  6.   

    也不行
    你从session里面获取到的值,还是你没有修改过的初始值,效果是一样的!Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If not IsPostBack Then
                dbGridfwt.DataSource = 获取你的数据源           
                dbGridfwt.DataBind()
    end if
      

  7.   

    to:brightheroes(闭关|那一剑的风情) 
    If not IsPostBack Then 不是只在页面第一次启动的时候执行么?点击更新按钮它应该走 ispostback里呀
      

  8.   

    是,重新点击按钮是走ispostback
    然后你的程序就从session里面获取数据源
    重新给grid绑定
    那么不是也一样冲掉了你新填写的值吗?
      

  9.   

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If not IsPostBack Then
                dbGridfwt.DataSource = 获取你的数据源           
                dbGridfwt.DataBind()
    end if上面不是写了吗?
      

  10.   

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '在此处放置初始化页的用户代码
            Dim strsql As String        If Not IsPostBack Then
                strsql = "select * from TB_FW_service order by s_district,s_id"
                getQuery(strsql)
                dbGridfwt.DataSource = objReader
                dbGridfwt.DataBind()
            End If
        End Sub是这样的吗?可我点修改之后dataGrid就一片空白了呀,还要加什么东西么?
      

  11.   

    getQuery(strsql)
    dbGridfwt.DataSource = objReader
    是什么意思?
    你根本没有获取到数据吧?
      

  12.   

    获得了呀,
    第一次加载是这样子的:
    If Not IsPostBack Then
                dbopen()
                strsql = "select s_id,s_district from TB_FW_service order by s_district,s_id"
                getQuery(strsql)
                dbTable.Columns.Add("id", Type.GetType("System.Int16"))
                dbTable.Columns.Add("district", Type.GetType("System.String"))
                dbTable.Columns.Add("name", Type.GetType("System.String"))            While objReader.Read
                    dbRow = dbTable.NewRow()
                    dbRow("id") = objReader("s_id")
                    dbRow("district") = objReader("s_district")
                    dbRow("name") = objReader("s_district")
                    dbTable.Rows.Add(dbRow)
                End While            dbView = New DataView(dbTable)
                dbGridfwt.DataSource = dbView 
                dbGridfwt.DataBind()
                objReader.Close()
                dbclose()
            End If
      

  13.   

    你直接把你上面的代码加到PageLoad里面就可以了
    我觉得
      

  14.   

    我总觉得要在page.IsPostBack里写点东西,象上面这样当页面postBack时,dataGrid里原有的数据都会消失了,但要是加上session里的,确实象你说的那样,把原来的数据又绑定回去了。迷茫中
      

  15.   

    你的DataGrid的EnableViewState属性要设置成true
      

  16.   

    页面postback的时候,grid里面的数据是不会丢失的