Gridview - 更新事件不触发

发布于 2024-11-17 03:13:40 字数 7659 浏览 2 评论 0 原文

当我单击gridview的“编辑”按钮时,它将显示“更新”和“取消”按钮。但是当我点击“Update”按钮时,它不会触发任何事件“RowUpdating”,RowUpdated”,“RowCommand”......它重新加载页面,然后gridview消失。

以下是我的asp代码:

<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>

......

下面是VB代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

七秒鱼° 2024-11-24 03:13:40

在 asp:TemplateField 中使用具有正确 CommandName 的按钮,它将触发相应的事件。

例子

<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>

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>
方觉久 2024-11-24 03:13:40

我解决了我的问题。

我需要设置 CausesValidation="false"

I solved my problem.

I need to set CausesValidation="false"

池木 2024-11-24 03:13:40

在 gridview 中设置 enableviewstate=true

make enableviewstate=true in gridview

烛影斜 2024-11-24 03:13:40

你是在页面加载时写的吗,如果没有添加这个

if me.ispostback= false then
'grid fill statement

end if

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

if me.ispostback= false then
'grid fill statement

end if
故事未完 2024-11-24 03:13:40

我不确定您的保存/取消按钮位于何处,但您可能需要按照 MSDN

即。

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"/>

素罗衫 2024-11-24 03:13:40

我从未使用过单独的 rowupdate/rowedit 命令,但我确实使用了以下命令:

在我的网格列列表中,我添加了命令字段和图像(按钮可选)

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

我在代码中没有看到这一点。

网格编辑的示例如下
在此处输入图像描述

在后面的代码中,您应该能够检查 RowDataBound 事件,如下所示(Grida 是示例网格的名称)

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

非常适合我。

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
enter image description here

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文