gridup在触发rowupdating事件时不显示新的更新值

时间:2012-06-02 13:42:14

标签: asp.net

我正在使用gridview。当我单击编辑按钮时,会出现更新和取消按钮。在修改来自EditItemTemplate的文本框中的值时,新值不会显示在事件处理程序rowupdating()中,而是获取在呈现页面时显示的值。如何从这些文本框中获取新值并继续进行?这是代码。

aspx页面

    <%@ Page Language="C#" MasterPageFile="~/Master Pages/FacultyMasterpage.master" AutoEventWireup="true" CodeFile="FacultyQuestion.aspx.cs" Inherits="Faculty_FacultyQuestion" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Content10" Runat="Server">
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <br />
    <br />
    <asp:GridView ID="GridView1" runat="server" BackColor="White" 
        BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
        AllowPaging="True" AutoGenerateEditButton="True" 
        onrowcancelingedit="GridView1_RowCancelingEdit" 
        onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
        <RowStyle BackColor="White" ForeColor="#330099" />
        <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
    </asp:GridView>
    <br />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Content20" Runat="Server">
</asp:Content>

cs文件coading

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Faculty_FacultyQuestion : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();
    }
    private void BindData()
    {
        DataTable dt = Class3.QuestionForFaculty(Convert.ToInt32(Session["rid"]));
        GridView1.DataSource = dt;
        GridView1.DataBind();

    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        e.Cancel = true;
        GridView1.EditIndex = -1;
        BindData();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        {

                //Retrieve the table from the session object.

            DataTable dt = Class3.QuestionForFaculty(Convert.ToInt32(Session["rid"]));
            GridView1.DataSource = dt;
            GridView1.DataBind();

                //Update the values.
                GridViewRow row = GridView1.Rows[e.RowIndex];

                dt.Rows[row.DataItemIndex]["ans"] = ((TextBox)(row.Cells[2].Controls[0])).Text;

                GridView1.EditIndex = -1;
               BindData();
        }
    }
}

3级coading

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.ComponentModel;

[DataObject]
/// <summary>
/// Summary description for Class3
/// </summary>
public class Class3
{

     private static string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
    [DataObjectMethod(DataObjectMethodType.Select)]
    public static DataTable getall()
    {
        SqlDataAdapter dp = new SqlDataAdapter("Select * from Question", conn);
        DataSet ds = new DataSet();
        dp.Fill(ds, "Question");
        return ds.Tables["Question"];
    }
    [DataObjectMethod(DataObjectMethodType.Delete)]
    public static int delete(int qid)
    {
        SqlConnection con = new SqlConnection(conn);
        SqlCommand cmd = new SqlCommand("Delete into Question qid = @a", con);
        cmd.Parameters.Add("@a", SqlDbType.VarChar).Value = qid;

        con.Open();
        int i = cmd.ExecuteNonQuery();
        con.Close();
        return(i);

    }
     [DataObjectMethod(DataObjectMethodType.Select)]
    public static DataTable QuestionForFaculty(int rid)
    {
        SqlDataAdapter dp = new SqlDataAdapter("SELECT Question.question, Question.ans,Question.qid, Subject.subjectname, Question.posteddate, Registration.name, Registration.role, Registration.rid FROM Question INNER JOIN Registration ON Question.rid = Registration.rid INNER JOIN Subject ON Question.sid = Subject.subjectid WHERE (Question.fid = '"+rid+"')", conn);
        DataSet ds = new DataSet();
        dp.Fill(ds, "Question");
        return ds.Tables["Question"];
    }


}

1 个答案:

答案 0 :(得分:2)

为什么要在GridView事件开始时对OnRowUpdating进行数据绑定?那就是你的问题!只需删除它:

GridView1.DataSource = dt;
GridView1.DataBind();

更新:首先将此添加到您的Class3(完全基于您的示例代码 - 未经测试):

[DataObjectMethod(DataObjectMethodType.Update)]
public static int UpdateAnswer(int qid, string ans)
{
    var con = new SqlConnection(conn);
    var cmd = new SqlCommand("UPDATE Question SET ans = @ans WHERE qid = @qid", con);
    cmd.Parameters.Add("@qid", SqlDbType.VarChar).Value = qid;
    cmd.Parameters.Add("@ans", SqlDbType.VarChar).Value = ans;
    con.Open();
    int i = cmd.ExecuteNonQuery();
    con.Close();
    return(i);
}

然后将OnRowUpdating事件更改为:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    Class3.UpdateAnswer(row.DataItemIndex, ((TextBox)row.Cells[2].Controls[0]).Text);
    GridView1.EditIndex = -1;
    BindData();
}