gridview更新记录 [英] gridview update record

查看:54
本文介绍了gridview更新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<asp:UpdatePanel ID="up2" runat="server">
        <contenttemplate>
            <asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" 

                AutoGenerateEditButton="true" onrowediting="gv_RowEditing" 

                onrowcancelingedit="gv_RowCancelingEdit" onrowupdating="gv_RowUpdating" DataKeyNames="locationid">
                <columns>
                    <asp:TemplateField HeaderText="LocationID">
                        <itemtemplate>
                            <asp:Label ID="lbllocation" runat="server" Text='<%# Bind("locationid") %>'>
                        </itemtemplate>
                    
                    <asp:TemplateField HeaderText="RegionalGroup">
                        <itemtemplate>
                            <%# Eval("regionalgroup") %>
                        </itemtemplate>
                        <edititemtemplate>
                            <asp:TextBox ID="txtregional" runat="server" Text='<%# Eval("regionalgroup") %>'>
                        </edititemtemplate>
                    
                </columns>
            
            <asp:Label ID="lblmsg" runat="server">
        </contenttemplate>


在CS文件中


in CS file

protected void Page_Load(object sender, EventArgs e)
       {
           fillLocation();
       }

       protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
       {
           gv.EditIndex = e.NewEditIndex;
           fillLocation();
       }

       protected void fillLocation()
       {
           Employees emp = new Employees();
           gv.DataSource = emp.GetLocations();
           gv.DataBind();

       }

       protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
       {
           int locationid = Int32.Parse(gv.DataKeys[e.RowIndex].Value.ToString());
           int intresult = 0;
           GridViewRow row=gv.Rows[e.RowIndex];

           TextBox tlo = (TextBox)row.FindControl("txtregional");
           Location lo = new Location();
           Employees emp = new Employees();
           try
           {
               lo.locationid = locationid;
               lo.regionalgroup = tlo.Text;
               intresult = emp.updatelocation(lo);
               if (intresult > 0)
               {
                   lblmsg.Text = "Record updated Sucessfully";
               }
               else
               {
                   lblmsg.Text = "Record couldnt updated";
               }

           }

           catch (Exception ex)
           {
               lblmsg.Text = ex.Message;
           }

           finally
           {
               lo = null;
               emp = null;
           }
           gv.EditIndex = -1;
           fillLocation();
       }

       protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
       {
           gv.EditIndex=-1;
           fillLocation();
       }


但它没有得到更新,是否有任何问题
请帮助
谢谢.


but it is not getting update is there any problem
please help
thank you.

推荐答案

尝试页面加载中的IsPostBack()条件
try IsPostBack() condition in page load


这是一个非常有趣的问题.如果在设计器中选择GridView,然后在属性"框中选择该条目,则可以选择正确的值.结果应将您的源代码行更改为如下所示(粗体):

Itz quite interesting problem. If you select the GridView in the designer, and select that entry in the Properties box, you can select the correct values. The result should change your source line to look something like the following (in bold):

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  DataKeyNames="id" DataSourceID="SqlDataSource1">



所有教程都提到为您自动添加了DataKeyNames条目.但是,我发现,如果将SqlDataSource1控件更改为添加或删除列,则会弹出一个对话框,询问您是否要刷新字段和键...",这是问题所在,因为它清除了DataKeyNames条目.另外,您可能不希望在ItemTemplate中使用DropDownList,但可能希望将其保留为标签,并将其评估为licensee_id字段指向的字段.



All the tutorials mention that the DataKeyNames entry is automatically added for you. However, I discovered that if you change the SqlDataSource1 control to add or delete columns, a dialog pops asking if you want to "Refresh the Fields and Keys ..." This is where the problem occurs since it clears the DataKeyNames entry. Also, you may not want a DropDownList in the ItemTemplate, but may want to leave it alone as a label and Eval it to the field to which the licensee_id field points.


这篇关于gridview更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆