在GridView中更新行不起作用
本文关键字:不起作用 更新 GridView | 更新日期: 2023-09-27 18:14:59
我使用gridview来表示asp.net中的数据,但是当我点击更新链接时,什么都没有发生。当我跟踪代码时,我发现页面重载没有输入更新方法。同时,我也在尝试用不同的方法来解决这个问题,但是都不成功。
<%@ Page Title="" Language="C#" MasterPageFile="~/ins.Master"
AutoEventWireup="true" CodeBehind="ins_topic2.aspx.cs"
Inherits="RFID_SAMS.ins_topic2"%>
<asp:GridView ID="GridView1" runat="server"
OnRowDataBound="GridView1_RowDataBound"
onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
OnRowCancelingEdit="GridView1_RowCancelingEdit" AllowPaging="true"
DataKeyNames="Week#/Day">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnedit" runat="server" CommandName="Edit"
Text="Edit"/>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnupdate" runat="server" CommandName="Update"
Text="Update" />
<asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel"
Text="Cancel"/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
,后面的代码是:
protected void Page_Init(object sender, EventArgs e)
{
try
{
connetionString = "Server=petunia.arvixe.com;Database=rfid-sams;User
Id=rfid-sams-admin;Password=10100;MultipleActiveResultSets=true";
cnn = new SqlConnection(connetionString);
cnn.Open();
}//end try connection
catch (Exception ex)
{
Response.Write(ex);
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
View_lectures_topic();
}
}// end page_load
protected void View_lectures_topic()
{
week_num = 1;
DataTable table = new DataTable();
DataRow dr;
table.Columns.Add("Week#/Day");
table.Columns.Add("Saturday");
table.Columns.Add("Sunday");
table.Columns.Add("Monday");
table.Columns.Add("Tuesday");
table.Columns.Add("Wednesday");
for (int i = 1; i < 17; i++)
{
dr = table.NewRow();
dr[0] = "Week " + i;
table.Rows.Add(dr);
}
for (int i = 0; i < 16; i++)
{
query1 = "select * from [M].[Lecture] where [sec_no] = '" +
section + "'AND [week_no]='" + week_num +
"' Order by [date];";
checkuser1 = new SqlCommand(query1, cnn);
rdr1 = checkuser1.ExecuteReader();
while (rdr1.Read())
{
date = Convert.ToDateTime(rdr1["date"].ToString());
day = (int)date.DayOfWeek;
day = day + 2;
if (day == 1 || day == 8)
{
sat = true;
table.Rows[i][1] = rdr1["topic"].ToString();
}
}//end while
week_num++;
}//end for loop
GridView1.DataSource = table;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
View_lectures_topic();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
string week;
//week = row.FindControl("Week#/Day").ToString();
week = row.Cells[0].Controls[0].ToString();
query1 = "select * from [M].[Lecture] where [sec_no] = '" + section +
"' AND [week_no]='" + week + "' Order by [date];";
checkuser1 = new SqlCommand(query1, cnn);
rdr1 = checkuser1.ExecuteReader();
SqlCommand cmd;
while (rdr1.Read())
{
date = Convert.ToDateTime(rdr1["date"].ToString());
day = (int)date.DayOfWeek;
day = day + 2;
if (sat == true && (day == 1 || day == 8))
{
cmd = new SqlCommand("UPDATE [M].[Lecture] set [topic]='" +
row.Cells[1].Controls[0].ToString() +
"' where [date]='" + rdr1["date"].ToString() +
"' AND [sec_no]='" + section + "';", cnn);
cmd.ExecuteNonQuery();
}// end if
}// end while
GridView1.EditIndex = -1;
//call binding method
View_lectures_topic();
}// end method
你能帮我解决这个问题吗?
你可以看看下面的链接,你可以得到更好的想法更新。http://www.thecollegehunt.com/topic/aspnet-gridview-row-updating-update-rows-through-gridview
如果你需要更多的帮助,请告诉我。<asp:CommandField ButtonType="Image" EditImageUrl="images/edit.png"
ShowEditButton="True" CommandName="Update" />
你忘了加
CommandName = "更新"
检查一下这是否是你问题的正确答案。再见!