none
GridView中的删除、更新不起作用,无论是自定义模板列还是非自定义模板列 RRS feed

  • 问题

  • 首先我把GridView的前台代码部分贴出来

    <asp:GridView ID="YyInfo" AllowPaging="false" AllowSorting="true" AutoGenerateColumns="false" BackColor="White" BorderColor="#9BD1Fa" BorderWidth="1px" PageSize="15"
                        EmptyDataText="暂无数据" ShowFooter="true" runat="server" OnRowDataBound="YyInfo_RowDataBound" OnRowEditing="YyInfo_RowEditing" 
                        Width="100%" ShowHeaderWhenEmpty="true" OnRowUpdating="YyInfo_RowUpdating" OnRowCancelingEdit="YyInfo_RowCancelingEdit" OnPageIndexChanging="YyInfo_PageIndexChanging" 
                            OnRowCommand="YyInfo_RowCommand" OnRowDeleted="YyInfo_RowDeleted" OnRowDeleting="YyInfo_RowDeleting" >
                            <FooterStyle BorderColor="#9BD1FA" />
                            <HeaderStyle BorderColor="#9BD1FA" />
                            <PagerStyle BorderColor="#9BD1FA" />
                            <RowStyle BorderColor="#9BD1Fa" />
                            <FooterStyle BorderColor="#9BD1Fa" />
                            <EditRowStyle BorderStyle="Solid"  BorderColor="#9BD1Fa"/>
                            <EmptyDataRowStyle BorderColor="#9BD1FA" />
                            <Columns>
    <asp:TemplateField HeaderText="操作">
                                    <HeaderStyle Width="100px"  BorderColor="#9BD1Fa" BackColor="#9BD1Fa"/>
                                    <ItemStyle Width="10px" BorderColor="#9BD1Fa"/>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="Edit" Text="编辑" CommandName="Edit" runat="server" ></asp:LinkButton>
                                        <asp:LinkButton ID="Del" Text="删除" CommandName="Del" CommandArgument='<%#Bind("ID") %>' runat="server"></asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                    
                        </asp:GridView>

    下面是CS后台的部分代码

    #region 删除数据 protected void YyInfo_RowDeleting(object sender, GridViewDeleteEventArgs e) { //int YID = Convert.ToInt32(YyInfo.Rows[e.RowIndex].Cells[1].Controls[0].ToString()); int YID = Convert.ToInt32(this.YyInfo.DataKeys[e.RowIndex].Value.ToString()); Yyinfobll.Delete(YID); YyInBind(); } #endregion #region GirdView数据绑定 /// <summary> /// YyInBind数据绑定 /// </summary> public void YyInBind() { DataSet ds = Yyinfobll.GridDataSelect(); YyInfo.DataSource = ds; YyInfo.DataKeyNames = new string[] { "ID" }; YyInfo.DataBind(); #region 分页代码 ThisPage.Text = (YyInfo.PageIndex + 1).ToString();//当前页数 SubPage.Text = YyInfo.PageCount.ToString();//总共页数 RecordCount.Text = ds.Tables[0].Rows.Count.ToString(); if (ds.Tables[0].Rows.Count == 0) { FirstPage.Visible = false; PrePage.Visible = false; NextPage.Visible = false; LastPge.Visible = false; ThisPage.Visible = false; SubPage.Visible = false; RecordCount.Visible = false; } else if (YyInfo.PageCount == 1) { FirstPage.Visible = false; PrePage.Visible = false; NextPage.Visible = false; LastPge.Visible = false; } FirstPage.CommandName = "1"; PrePage.CommandName = (YyInfo.PageIndex == 0 ? "1" : YyInfo.PageIndex.ToString()); NextPage.CommandName = (YyInfo.PageCount == 1 ? YyInfo.PageCount.ToString() : (YyInfo.PageIndex + 2).ToString()); LastPge.CommandName = YyInfo.PageCount.ToString(); #endregion } #endregion #region 编辑数据 protected void YyInfo_RowEditing(object sender, GridViewEditEventArgs e) { YyInfo.EditIndex = e.NewEditIndex; YyInBind(); } #endregion #region 更新数据 protected void YyInfo_RowUpdating(object sender, GridViewUpdateEventArgs e) { ((Label)YyInfo.Rows[e.RowIndex].FindControl("DJR")).Text=Session["UserName"].ToString(); DateTime date1 = Convert.ToDateTime((TextBox)YyInfo.Rows[e.RowIndex].FindControl("CgDate"));//参观日期编辑 string Time1 = ((TextBox)YyInfo.Rows[e.RowIndex].FindControl("CgTime")).Text;//参观时间 int PeopNum = Convert.ToInt32((TextBox)YyInfo.Rows[e.RowIndex].FindControl("CgNumb"));//参观人数 string CGDW = ((TextBox)YyInfo.Rows[e.RowIndex].FindControl("CgDW")).Text;//参观单位 int CgAddress = Convert.ToInt32(((DropDownList)YyInfo.Rows[e.RowIndex].FindControl("CgAddress")).SelectedValue);//参观地点 string YyName = ((TextBox)YyInfo.Rows[e.RowIndex].FindControl("YyName")).Text;//参观单位预约人 string YyPhoneNum = ((TextBox)YyInfo.Rows[e.RowIndex].FindControl("YyPhoneNum")).Text;//预约人联系电话 int JJY = Convert.ToInt32(((DropDownList)YyInfo.Rows[e.RowIndex].FindControl("JJY")).SelectedValue);//讲解员 int DJR = Convert.ToInt32(userbll.GetUserID(Session["UserName"].ToString()));//登记人。从登陆信息中获取相应的信息 int IfCancel; CheckBox CBX = (CheckBox)YyInfo.Rows[e.RowIndex].FindControl("SFQX"); if (CBX.Checked == true) { IfCancel = 1; } else { IfCancel = 0; } Model.YyInfo YyInfoModel = new Model.YyInfo(); //定义更新变量数据 YyInfoModel.CgDay = date1; YyInfoModel.CgTime = Time1; YyInfoModel.PeopNum = PeopNum; YyInfoModel.UnitName = CGDW; YyInfoModel.AddressID = CgAddress; YyInfoModel.YyName = YyName; YyInfoModel.YyPhoneNum = YyPhoneNum; YyInfoModel.SPID = JJY; YyInfoModel.UserID = DJR; YyInfoModel.IfConcel = IfCancel; Yyinfobll.Update(YyInfoModel); YyInfo.EditIndex = -1; YyInBind(); } #endregion #region 取消编辑 protected void YyInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { YyInfo.EditIndex = -1; YyInBind(); } #endregio

    断点设置后发现,点击删除,没有反应,代码没有走,不知道是什么问题,求大神指点
    • 已移动 Caillen 2015年5月4日 9:14
    2015年5月3日 4:24

答案

  • 您好,

    您可以试着在GridView RowCommand事件下来执行相关操作,下面的代码供您参考。

    protected void YyInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName=="Del")
        {
            string id = e.CommandArgument.ToString();
            //delete the record accordig to id 
        }
    }
    

    2015年5月12日 2:09
    版主

全部回复

  • 你好:

    欢迎来到MSDN中文论坛。

    关于ASP.NET的问题请发布到ASP.NET论坛,我已经帮你移过来。或者也可以在ASP.NET官方英文论坛发布你的问题:

    http://forums.asp.net/


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年5月4日 9:15
  • 1,load  检查

    protected void Page_Load(object sender, EventArgs e) 
        { 
            if (!IsPostBack) 
            { 
                bind(); 
            } 
        } 

    2. RowDataBound

    你参考如下, 这个是非常常规的 控件。一般不会出什么问题。

    http://www.cnblogs.com/GnagWang/archive/2010/07/06/1772017.html

    2015年5月7日 2:14
  • 您好,

    您可以试着在GridView RowCommand事件下来执行相关操作,下面的代码供您参考。

    protected void YyInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName=="Del")
        {
            string id = e.CommandArgument.ToString();
            //delete the record accordig to id 
        }
    }
    

    2015年5月12日 2:09
    版主