简体   繁体   English

GridView中的更新行不起作用

[英]Update row in GridView does not work

I am using gridview to represent data in asp.net, but when I click on update link nothing happens.When I trace the code I found the page reload without entering update method. 我正在使用gridview来表示asp.net中的数据,但是当我单击更新链接时没有任何反应。当我跟踪代码时,发现页面重新加载而没有输入更新方法。 Also, I am trying to solve this problem in different ways, but no luck. 另外,我正在尝试以不同的方式解决此问题,但是没有运气。

<%@ 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>

and the code behind is: 而后面的代码是:

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

Could you please help me to solve this problem? 您能帮我解决这个问题吗?

You can look on following link, You can get better idea to Update. 您可以查看以下链接,您可以更好地更新。 http://www.thecollegehunt.com/topic/aspnet-gridview-row-updating-update-rows-through-gridview http://www.thecollegehunt.com/topic/aspnet-gridview-row-updating-update-rows-through-gridview

let me know if u need more help. 让我知道您是否需要更多帮助。

<asp:CommandField ButtonType="Image" EditImageUrl="images/edit.png" 
         ShowEditButton="True" CommandName="Update" />

You forgot to add 你忘了加

CommandName="Update" CommandName =“更新”

Just check if this is the right answer to your problem. 只要检查这是否是您问题的正确答案。 Adios! 阿迪奥斯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM