我使用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
你能帮我解决这个问题吗?
答案 0 :(得分:0)
您可以查看以下链接,您可以更好地了解更新。 http://www.thecollegehunt.com/topic/aspnet-gridview-row-updating-update-rows-through-gridview
如果您需要更多帮助,请告诉我。
答案 1 :(得分:0)
<asp:CommandField ButtonType="Image" EditImageUrl="images/edit.png"
ShowEditButton="True" CommandName="Update" />
您忘了添加
的CommandName = “更新”
只需检查这是否是您问题的正确答案。阿迪奥斯!