views:

214

answers:

1

I'm trying to create a simple example of an editable gridview, and for some reason can't seem to get the basics working. Why is this example not displaying the label Bar and a textbox when I click on "edit"?

aspx:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="gv.aspx.vb" Inherits="WebRoot.gv" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
   <title></title>
</head>
<body>
   <form id="form1" runat="server">
   <asp:GridView ID="gv" runat="server" AutoGenerateEditButton="true" AutoGenerateColumns="false">
      <Columns>
         <asp:TemplateField HeaderText="Foo">
            <ItemTemplate>
               <asp:Label ID="Label1" runat="server" Text="Foo" />
               <asp:Label ID="lblQuarter" runat="server" Text='<%#  Eval("fooVal") %>' />
            </ItemTemplate>
            <EditItemTemplate>
               <asp:Label ID="lblQuarter" runat="server" Text='Bar' />
               <asp:TextBox ID="TextBox1" runat="server" Text='<%#  Eval("fooVal") %>'></asp:TextBox>
            </EditItemTemplate>
         </asp:TemplateField>
  </Columns>

code behind:

Public Class MyFoo
   Public ReadOnly Property FooVal() As String
      Get
         Return _val
      End Get
   End Property
   Private _val As String = String.Empty
   Public Sub New(ByVal val As String)
      _val = val
   End Sub
End Class
Partial Public Class gv
   Inherits System.Web.UI.Page

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      Dim l As New List(Of MyFoo)
      l.Add(New MyFoo("first"))
      l.Add(New MyFoo("second"))

      gv.DataSource = l
      gv.DataBind()
   End Sub


   Private Sub gv_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv.RowEditing
      Dim x As String
      x = "foo"
   End Sub
End Class
A: 

Try hooking up OnRowEditing to your gv_RowEditing method. I'm surprised you don't get an error page, something like "fired event RowEditing which wasn't handled".

<asp:GridView ID="gv" OnRowEditing="gv_RowEditing" ...

Update

My bad. I assumed c# and keep forgetting to check the language. Put this in your rowediting method and the edit will work. But there's more to do in the cancel and update events.

    gv.EditIndex = e.NewEditIndex
    Dim l As New List(Of MyFoo)
    l.Add(New MyFoo("first"))
    l.Add(New MyFoo("second"))
    gv.DataSource = l
    gv.DataBind()

More details here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting.aspx

Steve
In vb, the event wiring can be automatic - the sub gv_RowEditing handles the gvRowEditing event, so no error.
chris
Although in this case, the header says AutoEventWireup=false and yet it still does it - strange.
chris