Gridview没有更新 [英] Gridview not updating
本文介绍了Gridview没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HTML: -
< asp: GridView runat = server ID = gv2 AutoGenerateColumns = false AutoGenerateEditButton = true < span class =code-attribute>
< span class =code-attribute> < span class =code-attribute> AutoGenerateDeleteButton = true OnRowEditing = 编辑 OnRowUpdating = update OnRowDeleting = delete
OnRowCancelingEdit = 取消 >
< 列 >
< asp:TemplateField HeaderText = ID >
< ItemTemplate >
< asp:标签 runat = 服务器 ID = lblId 文字 =' <% #Eval( s_id)%> ' > < / asp:标签 >
< / ItemTemplate >
< EditItemTemplate >
< asp:Label runat = 服务器 < span class =code-attribute> ID = lblId1 文字 =' <% #Eval( s_id)%> ; ' > < / asp:标签 >
< / EditItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 名称 >
< ItemTemplate >
< < span class =code-leadattribute> asp:Label runat = server ID = lblName 文本 =' <% #Eval( s_name)%> ' > < < span class =code-leadattribute> / asp:Label >
< / ItemTemplate >
< EditItemTemplate >
< asp:TextBox runat = server ID = txtName 文字 =' <% #Eval( S_NAME跨度>) %> ' > < / asp:TextBox >
< / EditItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 电话 >
< ItemTemplate >
< asp:Label runat = server ID = lblPhone 文字 =' <% #Eval( s_phone)%> ' > < / asp:Label >
< / ItemTemplate >
< EditItemTemplate >
< asp:TextBox runat = server ID = txtPhone < span class =code-attribute>文字 =' <% #Eval( s_phone)%> ' > < / asp:TextBox >
< / EditItemTemplate >
< / asp:TemplateField < span class =code-keyword>>
< asp:TemplateField HeaderText = 度 >
< < span class =code-leadattribute> ItemTemplate >
< asp:标签 runat = server ID = lblDegree 文本 =' <% #Eval( s_degree)%> ' > < / asp:标签 >
< / ItemTemplate >
< EditItemTemplate >
< asp:TextBox runat = server ID = txtDegree 文字 =' <% #Eval( s_degree)%> ' > < / asp:TextBox >
< / EditItemTemplate >
< < span class =code-leadattribute> / asp:TemplateField >
< /列 >
< ; / asp:GridView >
代码(.cs): -
public void update(object sender,GridViewUpdateEventArgs e)
{
con.Open();
Label lbl = gv2.Rows [e.RowIndex] .FindControl(lblId1)as Label;
TextBox txtName = gv2.Rows [e.RowIndex] .FindControl(txtName)as TextBox;
TextBox txtPhone = gv2.Rows [e.RowIndex] .FindControl(txtPhone)as TextBox;
TextBox txtDegree = gv2.Rows [e.RowIndex] .FindControl(txtDegree)as TextBox;
SqlCommand cmd2 = new SqlCommand();
cmd2.Connection = con;
cmd2.CommandText =UpdateInfo;
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.Add(@ s_name,SqlDbType.VarChar).Value = txtName.Text.ToString();
cmd2.Parameters.Add(@ s_phone,SqlDbType.VarChar).Value = txtPhone.Text.ToString();
cmd2.Parameters.Add(@ s_degree,SqlDbType.VarChar).Value = txtDegree.Text.ToString();
cmd2.Parameters.Add(@ s_id,SqlDbType.Int).Value = Convert.ToInt32(lbl.Text);
cmd2.ExecuteNonQuery();
lblTest.Text = txtName.Text; //正在检查值,但此前打印的值为
cmd2.Dispose();
gv2.EditIndex = -1;
con.Close();
BindData();
}
程序: -
设置ANSI_NULLS ON
设置QUOTED_IDENTIFIER ON
GO
ALTER程序[dbo]。[UpdateInfo]
(
@s_name varchar(300),
@s_phone varchar(50),
@s_degree varchar( 300),
@s_id int
)
as
begin
update info set s_name = @ s_name,s_phone = @s_phone,s_degree = @ s_degree其中s_id = @ s_id
结束
Quote:大家好,这里的问题是我需要删除旧行并在编辑模式下在Gridview中插入一个新行。所以请帮我一些代码,这些代码可以插入/更新在编辑模式下给出的新值,也可以清除以前存储/绑定的值。
当我更新时,Gridview保存绑定值,因此不允许更新操作以产生效果。
解决方案
在页面加载时我只是调用BindData函数,但技术是在它不是Postback时调用它。 />
if(!Page.IsPostBack)
BindData();
您好,
如果您的数据在数据库表上成功更新,那么您将在更新功能之后调用绑定gridview上数据的函数。 / blockquote>
我认为您的表正在使用此查询进行更新,因此您的数据不会在网格中更新。请尝试:
< span class =code-keyword> update info set s_name = ' ''+ @ s_name +''',s_phone = ' ''+ @ s_phone +''',s_degree = ' ''+ @ s_degree +'' ' 其中 s_id = @ s_id
希望它'请帮助你。
如果有帮助,不要忘记标记为答案。 :)
HTML:-
<asp:GridView runat="server" ID="gv2" AutoGenerateColumns="false" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" OnRowEditing="edit" OnRowUpdating="update" OnRowDeleting="delete" OnRowCancelingEdit="cancel"> <Columns> <asp:TemplateField HeaderText="ID"> <ItemTemplate> <asp:Label runat="server" ID="lblId" Text='<%#Eval("s_id") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:Label runat=server ID="lblId1" Text='<%#Eval("s_id") %>'></asp:Label> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:Label runat="server" ID="lblName" Text='<%#Eval("s_name") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox runat="server" ID="txtName" Text='<%#Eval("s_name") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Phone"> <ItemTemplate> <asp:Label runat="server" ID="lblPhone" Text='<%#Eval("s_phone") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox runat="server" ID="txtPhone" Text='<%#Eval("s_phone") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Degree"> <ItemTemplate> <asp:Label runat="server" ID="lblDegree" Text='<%#Eval("s_degree") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox runat="server" ID="txtDegree" Text='<%#Eval("s_degree") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Code(.cs):-
public void update(object sender, GridViewUpdateEventArgs e) { con.Open(); Label lbl = gv2.Rows[e.RowIndex].FindControl("lblId1") as Label; TextBox txtName = gv2.Rows[e.RowIndex].FindControl("txtName") as TextBox; TextBox txtPhone = gv2.Rows[e.RowIndex].FindControl("txtPhone") as TextBox; TextBox txtDegree = gv2.Rows[e.RowIndex].FindControl("txtDegree") as TextBox; SqlCommand cmd2 = new SqlCommand(); cmd2.Connection = con; cmd2.CommandText = "UpdateInfo"; cmd2.CommandType = CommandType.StoredProcedure; cmd2.Parameters.Add("@s_name", SqlDbType.VarChar).Value = txtName.Text.ToString(); cmd2.Parameters.Add("@s_phone", SqlDbType.VarChar).Value = txtPhone.Text.ToString(); cmd2.Parameters.Add("@s_degree", SqlDbType.VarChar).Value = txtDegree.Text.ToString(); cmd2.Parameters.Add("@s_id", SqlDbType.Int).Value = Convert.ToInt32(lbl.Text); cmd2.ExecuteNonQuery(); lblTest.Text = txtName.Text;//was checking value but previous value printing here cmd2.Dispose(); gv2.EditIndex = -1; con.Close(); BindData(); }
Procedure:-
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO ALTER procedure [dbo].[UpdateInfo] ( @s_name varchar(300), @s_phone varchar(50), @s_degree varchar(300), @s_id int ) as begin update info set s_name=@s_name, s_phone=@s_phone, s_degree=@s_degree where s_id=@s_id end
Quote:Guys, the problem here is that I need to delete the old row and insert a new row in Gridview in EDIT mode. So help me with some code which would Insert/Update new values given in Edit mode and also clearing previous stored/bind value.
When I am updating, the Gridview holds the bind value in it thus not letting Update operation to cause effects.解决方案At Page Load I was simply calling BindData function but the technique was to call it when its not Postback!
if(!Page.IsPostBack) BindData();
Hi,
If your data updated successfully on database table then you call the function which is bind the data on gridview, after the update function.
I dnt think your table is updating with this query so your data is not updating in the grid.Try this :
update info set s_name='''+@s_name+''', s_phone='''+@s_phone+''', s_degree='''+@s_degree+''' where s_id=@s_id
Hope it'll help you.
Don't forget to mark as answer if it helps. :)
这篇关于Gridview没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文