GridView_RowUpdating未触发

时间:2016-02-29 09:10:20

标签: c# asp.net gridview

我有使用ItemTemplate和EditItemTemplate的Gridview:

但是当我点击&#34; Ok&#34;按钮,没有任何反应。我试图在RowUpdating事件上添加跟踪点,然后单击&#34;确定&#34;按钮,但它甚至没有触发。我认为,这个问题可能在同一个问题上,而且#34; Id&#34; ItemTemplate和EditItemTemplate lbl_first_fl。但早些时候这个工作正常,唯一的改变 - 我将Visual Studio更新到2015并将Windows更改为8.所以我安装了VS 2013,但问题仍然存在。有什么建议?或者也许我如何使用不同的id来处理它?<​​/ p>

aspx页面:

&#13;
&#13;
   <asp:GridView ID="GridView5" runat="server" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333" Height="430px" OnRowCancelingEdit="GridView5_RowCancelingEdit" OnRowDataBound="GridView5_RowDataBound" OnRowEditing="GridView5_RowEditing" OnRowUpdating="GridView5_RowUpdating" Width="100%" CssClass="gridview" GridLines="none">
                                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

                                <Columns>

  <asp:TemplateField ItemStyle-Width="100">
<EditItemTemplate>
                  <asp:Button ID="btn_Update_fl" runat="server" CommandName="Update" Text="Ок" />
<asp:Button ID="btn_Cancel_fl" runat="server" CommandName="Cancel" Text="Отмена" />
</EditItemTemplate>
<ItemTemplate>
 <asp:Button ID="btn_Edit_fl" runat="server" CommandName="Edit" Text="✎" Enabled='<%# Flag %>' />
</ItemTemplate>

</asp:TemplateField>

 <asp:TemplateField HeaderText="08:00 - 17:00" ItemStyle-HorizontalAlign="Center">                                                                                                        
<ItemTemplate>
 <asp:Label ID="lbl_first_fl" runat="server" Text='<%# Eval("First") %>' Visible="true"></asp:Label>                                                                                                                   
 </ItemTemplate>

 <EditItemTemplate>
 <asp:Label ID="lbl_first_fl" runat="server" Text='<%# Eval("First") %>' Visible="false"></asp:Label>        
 <asp:DropDownList ID="ddl_first_fl" runat="server">
</asp:DropDownList> 
                                                       
</EditItemTemplate>

</asp:TemplateField>

    <asp:CommandField/>
                                              

</Columns>
</asp:GridView>
&#13;
&#13;
&#13;

OnRowUpdating事件:

 protected void GridView5_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
    {


        DropDownList First_fl = GridView5.Rows[e.RowIndex].FindControl("ddl_first_fl") as DropDownList;



        con = new SqlConnection(cs);
        con.Open();
        //updating the record  
        SqlCommand cmd_fl = new SqlCommand("Update [Duty].[dbo].[Schedule_FirstLine] set First='" + First_fl.Text + "', con);
        cmd_fl.ExecuteNonQuery();

        con.Close();

        GridView5.EditIndex = -1;

        ShowDataFirstLine();

    }

绑定网格视图:

 protected void ShowDataFirstLine()
    {
        DateTime date = Convert.ToDateTime(Calendar1.VisibleDate.Date.ToString());

        var firstDayOfMonth = new DateTime(date.Year, date.Month, 1);
        var lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);

        string GridDS_FirstLine = "set dateformat dmy  Select DutyDate,WeekDay,First, Second from [Duty].[dbo].[Schedule_FirstLine] WHERE DutyDate >= '" + firstDayOfMonth + "' AND DutyDate <= '" + lastDayOfMonth + "'";

        dt_FirstLine = new DataTable();
        con_FirstLine = new SqlConnection(cs);
        con_FirstLine.Open();
        adapt_FirstLine = new SqlDataAdapter(GridDS_FirstLine, con_FirstLine);
        adapt_FirstLine.Fill(dt_FirstLine);
        if (dt_FirstLine.Rows.Count > 0)
        {
            GridView5.DataSource = dt_FirstLine;
            GridView5.DataBind();
        }

        GridView5.Columns[0].Visible = true;
    }

1 个答案:

答案 0 :(得分:0)

您可以像这样在网格上命名事件吗?

<asp:GridView ID="TaskGridView" runat="server" 
        AutoGenerateEditButton="True" 
        AllowPaging="true"
        OnRowEditing="TaskGridView_RowEditing"         
        OnRowCancelingEdit="TaskGridView_RowCancelingEdit" 
        OnRowUpdating="TaskGridView_RowUpdating"
        OnPageIndexChanging="TaskGridView_PageIndexChanging">
      </asp:GridView>