グリッドビューを使用しています。編集ボタンをクリックすると、更新とキャンセルのボタンが表示されます。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">
<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ファイルのコーディング
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 コーディング
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"];
}
}