没有在网格视图行更新中获得新输入的值 [英] not getting new entered values on grid view row updating

查看:47
本文介绍了没有在网格视图行更新中获得新输入的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨所有

网格视图来源是

< asp:GridView ID =GridView1AutoGenerateColumns =falserunat = server ; AutoGenerateDeleteButton = 真 
AutoGenerateEditButton属性= 真 OnRowEditing = GridView1_RowEditing OnRowCancelingEdit = GridView1_RowCancelingEdit
OnRowDeleting = GridView1_RowDeleting OnRowUpdating = GridView1_RowUpdating >
< Columns>
< asp:TemplateField Visible =false>
< ItemTemplate>
< asp:Label ID =eidText ='<%#Eval(eid)%>'runat =server>< / asp:Label>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =EXAM NAME>
< ItemTemplate>
<%#Eval(Examname)%>
< / ItemTemplate>
< EditItemTemplate>
< asp:TextBox ID =textBox1Text ='<%#Eval(Examname)%>'runat =server>< / asp:TextBox>
< / EditItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =MAX.MARKS>
< ItemTemplate>
<%#Eval(Maxmarks)%>< / ItemTemplate>
< EditItemTemplate>
< asp:TextBox ID =textBox2Text ='<%#Eval(Maxmarks)%>'runat =server>< / asp:TextBox>
< / EditItemTemplate>
< / asp:TemplateField>
< / Columns>
< / asp:GridView>



和我的代码隐藏代码是

 < span class =code-keyword> protected   void  GridView1_RowUpdating( object  sender,GridViewUpdateEventArgs e )
{
GridViewRow row =(GridViewRow)GridView1.Rows [e.RowIndex];
Label lbl =(Label)row.FindControl( eid);
TextBox textname =(TextBox)row.FindControl( textBox1);
TextBox textmax =(TextBox)row.FindControl( textBox2);
GridView1.EditIndex = -1;
try
{
MySqlConnection con = new MySqlConnection(ConfigurationManager。 ConnectionStrings [ conn]。ConnectionString);
con.Open();
MySqlCommand cmd = new MySqlCommand( update examcreation set Examname =' + textname.Text + ',Maxmarks =' + textmax.Text + 'where eid =' + lbl.Text + ',con);
cmd.ExecuteNonQuery();
con.Close();
}
catch (例外情况)
{
Response.Write( < script type ='text / javascript'> window.alert('Please Start Wamp Server + ex.Message + ')< / script>);
}
Binding();
}



调试后如果我按下编辑按钮并在这里给出一些值我没有得到新输入的值我只得到以前的值

我怎样才能得到新输入的值,有人可以帮我吗?

提前谢谢

解决方案

最有可能原因是,您始终将数据绑定到 GridView ,而不检查 IsPostBack 属性。



当您点击更新按钮时, Page_Load()在调用 RowUpdating()事件方法之前调用事件。因此, GridView 再次与旧值绑定(因为您可能没有检查 IsPostBack 标志)因此,在 RowUpdating()事件方法中你得到的旧值。



如果我的假设是正确的,要解决这个问题,请更改 GridView 的数据绑定方法,如下所示:



  if (!IsPostBack) //  仅绑定GridView IsPostBack为false时 
{
GridView1.DataSource = // 在此处分配数据;
GridView1.DataBind();
}


只需将Gridview的Enableviewstate属性设置为false即可获得新值

什么是使用网格视图的datakey属性?


hi all
source of grid view is

<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat=server; AutoGenerateDeleteButton="True"
               AutoGenerateEditButton="True" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit"
               OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating">
                                                   <Columns>
<asp:TemplateField Visible="false">
                                                         <ItemTemplate>
                                                               <asp:Label ID="eid" Text='<%# Eval("eid") %>' runat="server"></asp:Label>
                                                           </ItemTemplate>
                                                       </asp:TemplateField>
                                                       <asp:TemplateField HeaderText="EXAM NAME">
                                                           <ItemTemplate>
                                                                <%#Eval("Examname") %>
                                                           </ItemTemplate>
                                                           <EditItemTemplate>
                                                               <asp:TextBox ID="textBox1" Text='<%#Eval("Examname") %>' runat="server"></asp:TextBox>
                                                           </EditItemTemplate>
                                                       </asp:TemplateField>
                                                       <asp:TemplateField HeaderText="MAX.MARKS">
                                                           <ItemTemplate>
                                                               <%#Eval("Maxmarks") %></ItemTemplate>
                                                           <EditItemTemplate>
                                                               <asp:TextBox ID="textBox2" Text='<%#Eval("Maxmarks") %>' runat="server"></asp:TextBox>
                                                           </EditItemTemplate>
                                                       </asp:TemplateField>
                                                   </Columns>
                                               </asp:GridView>


and my code-behind code is

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {
       GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
       Label lbl = (Label)row.FindControl("eid");
       TextBox textname = (TextBox)row.FindControl("textBox1");
       TextBox textmax = (TextBox)row.FindControl("textBox2");
       GridView1.EditIndex = -1;
       try
       {
           MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
           con.Open();
           MySqlCommand cmd = new MySqlCommand("update examcreation set Examname='" + textname.Text + "' , Maxmarks='" + textmax.Text + "' where eid='" + lbl.Text + "'", con);
           cmd.ExecuteNonQuery();
           con.Close();
       }
       catch (Exception ex)
       {
           Response.Write("<script type='text/javascript' >window.alert('Please Start Wamp Server " + ex.Message + " ')</script>");
       }
   Binding();
   }


after debugging if i press edit button and give some values here i am not getting newly entered values i am getting previous values only
how can i get newly entered values can anyone help me please?
thanks in advance

解决方案

Most likely reason is, you are always binding the data to the GridView without checking the IsPostBack property.

When you are clicking the "Update" button, the Page_Load() event gets invoked before the RowUpdating() event method is invoked. So, the GridView is getting bound again with the old value (Because you might not checked the IsPostBack flag) and hence, inside the RowUpdating() event method you are getting the old value.

If my assumption is correct, to solve this, change the Data binding method of the GridView as follows:

if (!IsPostBack) //Bind GridView only when IsPostBack is false
{
           GridView1.DataSource = //Assign data here;
           GridView1.DataBind();
}


just set Enableviewstate property to false of gridview you can get new value


what is the uses of datakey property of grid view?


这篇关于没有在网格视图行更新中获得新输入的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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