开发者

Gridview - Update event not fire

When I click the "Edit" button of the gridview, it will show "Update" and "Cancel" button. But when I click "Update" button, it will not fire any event "RowUpdating", RowUpdated", "RowCommand"... It reload the page and then gridview disappear.

The following is my asp code:

<asp:GridView ID="Gridview1" runat="server" EnableViewState="False"
AutoGenerateColumns = "False" Font-Names = "Arial"
Font-Size = "10pt" AlternatingRowStyle-BackColor = "#C2D69B" 
AutoGenerateEditButton="false" 
AllowPaging ="True"  
PageSize = "20"
OnRowCommand="GridView1_RowCommand" 
OnRowDataBound="GridView1_RowDataBound"
OnRowEditing="GridView1_RowEditing"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowUpdating="GridView1_RowUpdating"
OnPageIndexChanging="GridView1_PageIndexChanging">       
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<Columns>
 <asp:BoundField DataField = "Name&Post" HeaderText = "Name & Post"  ReadOnly ="true" />
<asp:TemplateField HeaderText="Working<br>Time">
<ItemTemplate>
<asp:Label ID="lb1_rosterkey"  runat="server"  Text='<%#DataBinder.Eval(Container.DataItem, "Col1_RosterKey")%>'  Visible ="false" ></asp:Label>
<asp:DropDownList ID="ddl1_shifttype" runat ="server"  Enabled ="false"  DataSourceID="SqlDataSource2" DataTextField ="en_name" DataValueField ="shift_type_key">       </asp:DropDownList>
<MKB:TimeSelector ID="Col1_StartTime" runat="server" DisplaySeconds="False" ReadOnly="true" MinuteIncrement="1" AmPm="AM" BorderColor="Silver" 
                            Date="" Hour="07" Minute="0"  SelectedTimeFormat="Twelve">   </MKB:TimeSelector>
<MKB:TimeSelector ID="Col1_EndTime" runat="server" DisplaySeconds="False"  ReadOnly="true" MinuteIncrement="1" AmPm="PM" BorderColor="Silver" 
                            Date="" Hour="07" Minute="0" SelectedTimeFormat="Twelve">   </MKB:TimeSelector>                                 
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl1_shifttype" runat ="server"  DataSourceID="SqlDataSource2" DataTextField ="en_name" DataValueField ="shift_type_key"></asp:DropDownList>
<MKB:TimeSelector ID="Col1_StartTime" runat="server" DisplaySeconds="False"  MinuteIncrement="1" AmPm="AM" BorderColor="Silver" 
                            Date="" Hour="07" Minute="0"  SelectedTimeFormat="Twelve">     </MKB:TimeSelector>
<MKB:TimeSelector ID="Col1_EndTime" runat="server" DisplaySeconds="False"  MinuteIncrement="1" AmPm="PM" BorderColor="Silver" 
                            Date="" Hour="07" Minute="0" SelectedTimeFormat="Twelve">    </MKB:TimeSelector>
</EditItemTemplate> 
</asp:TemplateField>
<asp:TemplateField HeaderText="Leave/<br>TOIL">
<ItemTemplate>
<asp:CheckBox
ID="cb1_VL" Enabled="false" Text="VL" 
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_VL")%> />
<asp:CheckBox
ID="cb1_SL" Enabled="false" Text="SL" 
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_SL开发者_运维知识库")%> />
<asp:CheckBox
ID="cb1_ML" Enabled="false" Text="ML" 
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_ML")%> />
<asp:CheckBox
ID="cb1_PH" Enabled="false" Text="PH" 
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_PH")%> />
<asp:CheckBox
ID="cb1_APH" Enabled="false" Text="APH" 
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_APH")%> />
<asp:CheckBox
ID="cb1_TOIL" Enabled="false" Text="TOIL" 
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_TOIL")%> />
<br />
<%#DataBinder.Eval(Container.DataItem, "Col1_Others")%>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox
ID="cb1_VL" Text="VL" 
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_VL")%> />
<asp:CheckBox
ID="cb1_SL"  Text="SL" 
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_SL")%> />
<asp:CheckBox
ID="cb1_ML"  Text="ML" 
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_ML")%> />
<asp:CheckBox
ID="cb1_PH"  Text="PH" 
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_PH")%> />
<asp:CheckBox
ID="cb1_APH"  Text="APH" 
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_APH")%> />
<asp:CheckBox
ID="cb1_TOIL" Text="TOIL" 
runat="server"
Checked=<%#DataBinder.Eval(Container.DataItem, "Col1_TOIL")%> />
<asp:TextBox ID="tb1_Others" runat="server" Width="50" Text='<%#DataBinder.Eval(Container.DataItem, "Col1_Others") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>

......

The following is the VB code:

Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
    'Set the edit index.
    Gridview1.EditIndex = e.NewEditIndex        
    'Bind data to the GridView control.
    BindData()
End Sub

Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
    'Reset the edit index.
    Gridview1.EditIndex = -1
    'Bind data to the GridView control.
    BindData()
End Sub

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    'Retrieve the table from the session object.
    Dim dt = CType(Session("dt"), DataTable)

    'Update the values.
    Dim row = Gridview1.Rows(e.RowIndex)



    'Reset the edit index.
    Gridview1.EditIndex = -1

    'Bind data to the GridView control.
    BindData()
End Sub

Private Sub BindData()

    Dim StartDateStr As String

    StartDateStr = Trim(Request.QueryString("sd"))
    StartDateStr = Left(StartDateStr, 10)
    'date should be best in ISO format, i.e. yyyy-mm-ddTHH:mm:ss[.mmm] as "Set Dateformat dmy" is not supported by DataSet object
    'StartDateStr = Right(StartDateStr, 4) & "-" & Mid(StartDateStr, 4, 2) & "-" & Left(StartDateStr, 2) & " T00:00:00"

    Dim StartDate As DateTime
    Dim EndDate As DateTime
    StartDate = Convert.ToDateTime(StartDateStr)
    EndDate = Format(DateAdd(DateInterval.Day, 6, StartDate), "dd/MM/yyyy")

    g_header1 = StartDate   'Monday
    g_header2 = Format(DateAdd(DateInterval.Day, 1, StartDate), "dd/MM/yyyy")
    g_header3 = Format(DateAdd(DateInterval.Day, 2, StartDate), "dd/MM/yyyy")
    g_header4 = Format(DateAdd(DateInterval.Day, 3, StartDate), "dd/MM/yyyy")
    g_header5 = Format(DateAdd(DateInterval.Day, 4, StartDate), "dd/MM/yyyy")
    g_header6 = Format(DateAdd(DateInterval.Day, 5, StartDate), "dd/MM/yyyy")
    g_header7 = EndDate     'Sunday

    Gridview1.DataSource = Session("dt")
    Gridview1.DataBind()
End Sub

Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles Gridview1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow AndAlso e.Row.RowState = DataControlRowState.Edit Then
        'If e.Row.RowType = DataControlRowType.DataRow Then
        Dim row = DirectCast(e.Row.DataItem, DataRowView).Row
        Dim Col1_StartTime = DirectCast(e.Row.FindControl("Col1_StartTime"), MKB.TimePicker.TimeSelector)
        'set the TimePicker's Value here according to the Time-Value in the DataRow'
    End If
End Sub

Protected Sub Gridview1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles Gridview1.RowCommand
    If e.CommandName = "Update" Then
        'Reset the edit index.
        Gridview1.EditIndex = -1

        'Bind data to the GridView control.
        BindData()
    End If

End Sub


Use buttons in asp:TemplateField with proper CommandName and it will fire the corresponding events.

Example

<asp:TemplateField>
    <ItemTemplate>
            <asp:Button id="btnEdit" runat="server" commandname="Edit" text="Edit" />
            <asp:Button id="btnDelete" runat="server" commandname="Delete" text="Delete" />
    </ItemTemplate>
    <EditItemTemplate>
            <asp:Button id="btnUpdate" runat="server" commandname="Update" text="Update" />
            <asp:Button id="btnCancel" runat="server" commandname="Cancel" text="Cancel" />
    </EditItemTemplate>
</asp:TemplateField>


I solved my problem.

I need to set CausesValidation="false"


make enableviewstate=true in gridview


did you write this at page load, if not add this

if me.ispostback= false then
'grid fill statement

end if


I'm not sure where your save/cancel buttons are located but you may need to add a command name to them as specified on MSDN

ie. <asp:buttonfield buttontype="Link" commandname="Update" text="Update"/>


I never used separate rowupdate/rowedit commands but i do use the following:

in my grid columns list, i'm adding the commandfield and images (optional for the buttons)

        <asp:CommandField HeaderText="Edit" ButtonType="Image" ShowCancelButton="true" ShowEditButton="True"
            EditImageUrl="../images/esig.gif" CancelImageUrl="../images/btn_close.gif"
            UpdateImageUrl="../images/icn_ok.gif" />

I don't see this in your code.

a sample of a grid-editing as below

Gridview - Update event not fire

In the code behind, you should be able to check for the RowDataBound event as per below (Grida is the name of the sample grid)

Protected Sub Grida_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Grida.RowDataBound


'check for row in edit mode
    If (e.Row.RowState = DataControlRowState.Edit) then
' create handles for the row being edited 
   Dim drv As System.Data.DataRowView = CType(e.Row.DataItem, System.Data.DataRowView)

 ' locate the dropdownbox (in my page)

  Dim dllven As DropDownList = CType(e.Row.Cells(3).FindControl("DropDownVendor"), DropDownList)

   ' check what item was selected in this DDL when in editmode

        Dim li As ListItem = dllven.Items.FindByText(drv("Vendor").ToString)

   ' set the selection  upon return

        li.Selected = True



  end if

End Sub

works perfectly for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜