1

我在更新 gridview 时遇到问题!一切似乎都在工作。但是当我点击完成编辑时,数据将无法保存!

但是当我单击编辑时,正确的字段提示我输入新值,但它不会保存!

这是我的asp

<asp:GridView ID="GridView1" runat="server" CssClass="report" 
    AutoGenerateColumns="False" onrowediting="GridView1_RowEditing" 
    DataKeyNames="TimeID" onrowupdating="GridView1_RowUpdating" 
    onrowcommand="GridView1_RowCommand" 
    onrowcancelingedit="GridView1_RowCancelingEdit">

    <Columns> 



        <asp:BoundField DataField="date" Visible="true" ReadOnly="true" HeaderText="Date" />
        <asp:BoundField DataField="Description" HeaderText="Stage Description" ReadOnly="True" />


       <asp:TemplateField HeaderText="Start Time">
            <ItemTemplate>
                <asp:Label ID="Label7" runat="server"  Text='<%# ConvertToShotTime(Eval("StartTime")) %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtStartTime" ValidationGroup="1" Width="90px"  class="TimeEntry" runat="server"  Text='<%# ConvertToShotTime(Eval("StartTime")) %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator6" ControlToValidate="txtStartTime" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
           </EditItemTemplate>
        </asp:TemplateField>




        <asp:TemplateField HeaderText="End Time">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server"  Text='<%# ConvertToShotTime(Eval("EndTime")) %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtEndTime" class="TimeEntry"  ValidationGroup="1" Width="90px" runat="server"  Text='<%# ConvertToShotTime(Eval("EndTime")) %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator66" ControlToValidate="txtEndTime" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
            </EditItemTemplate>
        </asp:TemplateField>



        <asp:BoundField DataField="TimeInHours" HeaderText="Time Time (Hours)" ReadOnly="True" />

        <asp:CommandField ShowEditButton="true" ShowCancelButton="true" 
            ButtonType="Image" EditImageUrl="~/images/edit_record.jpg" 
            CancelImageUrl="~/images/edit_no.jpg" 
            UpdateImageUrl="~/images/update_record.jpg" />

        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
              <asp:ImageButton ID="lnbCopy" runat="server" AlternateText="Delete" 
                    CommandName="DeleteRecord"  CommandArgument='<%# Bind("TimeID") %>'  OnClientClick="return confirm('Are you sure you want to delete this row?');"  ImageUrl="~/images/delete_record.jpg" />
            </ItemTemplate>

           <EditItemTemplate>
                <asp:Label ID="lblTimeID" runat="server" Text='<%# Eval("TimeID") %>'></asp:Label>
            </EditItemTemplate>
        </asp:TemplateField>









    </Columns>


</asp:GridView>

这是我在代码后面所做的

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {
    GridView1.EditIndex = e.NewEditIndex;
    loadTable();
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {
    GridView1.EditIndex = -1;
    loadTable();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {

    GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;


    var StartTime = row.FindControl("txtStartTime") as TextBox;
    var EndTime = row.FindControl("txtEndTime") as TextBox;
    var id = row.FindControl("lblTimeID") as Label;

    SqlConnection conn = new SqlConnection(conStr.GetConnectionString("myServer1"));

    SqlCommand comm = new SqlCommand();

    comm.CommandText = "UPDATE CDSTimeSheet SET StartTime = @StartTime, EndTime = @EndTime ,timeElapsed = datediff(minute,@startTime , @EndTime), timeInSeconds = datediff(second,@startTime , @EndTime) WHERE TimeID = @id";
    comm.Connection = conn;

    comm.Parameters.AddWithValue("@id", id.Text);
    comm.Parameters.AddWithValue("@StartTime", StartTime.Text);
    comm.Parameters.AddWithValue("@EndTime", EndTime.Text);

    conn.Open();
    comm.ExecuteNonQuery();
    conn.Close();
    GridView1.EditIndex = -1;
    loadTable();
}

我究竟做错了什么?

4

1 回答 1

0

这听起来很像您DataBind在更新基础数据后没有调用您的网格。执行更新后(可能在loadTable我看到的上面引用的方法中),调用

GridView1.DataBind();

那应该刷新网格中的数据。


在您发表评论后,我看到您没有使用参数中的NewValues集合GridViewUpdateEventArgs来获取实际传入的更新字段值。尝试这个:

String StartTime = e.NewValues["StartTime"].ToString();
String EndTime = e.NewValues["EndTime"].ToString();
String id = e.Keys[0].ToString(); 

请注意,这些变量现在是字符串,而不是TextBoxs,因此您在将它们添加为参数时不需要在它们上添加“.Text”。

于 2013-01-22T04:59:36.693 回答