如何刷新网格...? [英] How to refresh Grid....?

查看:138
本文介绍了如何刷新网格...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASPX页面,其中一个用户控件和一个用户控件包含一个网格来显示数据,该数据将使用DatasourceID和一个sqldatasource控件加载到网格视图中.

现在我的问题是,当我在该表中添加一条记录时,该记录未显示在网格中,但是当我刷新页面或重新加载页面时,该记录显示在网格中.

因此,当我们使用sqldatasource控件时,如何刷新网格.


I have one ASPX page and inside that one user control and that user control contains one grid to display the data, the data will load into grid view using DatasourceID and one sqldatasource control.

now my question is that when i add one record in that table that record is not shown in my grid, but when i refresh the page or reload the page at that time that record is displayed in grid.

so how to refresh the grid while we use a sqldatasource control.


<asp:TextBox ID="txtName" runat="server">
    
    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />



后面的代码就是这样.



code behind is like this.

protected void btnSave_Click(object sender, EventArgs e)
{   
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString.ToString());
    SqlDataAdapter da = new SqlDataAdapter();
    con.Open();
    da.InsertCommand = new SqlCommand();
    da.InsertCommand.Connection = con;
    da.InsertCommand.CommandType = CommandType.Text;
    da.InsertCommand.CommandText = "INSERT INTO [User] ([NAME]) VALUES (@NAME)";
    da.InsertCommand.Parameters.AddWithValue("@NAME", txtName.Text);            
    da.InsertCommand.ExecuteNonQuery();
    con.Close();
}



我已经像这样配置了我的sqldata源.



i have configured my sqldata source like this.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConStr %>"
        SelectCommand="SELECT * FROM [User]">



我的网格视图就是这样.



and my grid view is like this.

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">



简而言之,当我将记录插入到网格中时,我想刷新网格.
我正在使用Oracle作为数据库.



In short... I want to refresh my grid when i insert the record to my grid.
I am using Oracle as my database.

推荐答案

ConnectionStrings:ConStr%>" SelectCommand = " >
ConnectionStrings:ConStr %>" SelectCommand="SELECT * FROM [User]">



我的网格视图就是这样.



and my grid view is like this.

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">



简而言之,当我将记录插入到网格中时,我想刷新网格.
我正在使用Oracle作为数据库.



In short... I want to refresh my grid when i insert the record to my grid.
I am using Oracle as my database.


问题是SqlDataSource不了解更改,因此它们不会传播到网格.

一种选择是通过SqlDataSource执行插入 [ ^ ]可以在某些[有限]情况下不带任何代码地完成这项工作.

一个更简单的方法是在您提供的插入代码之后调用GridView1.DataBind().
The problem is that the SqlDataSource is unaware of the changes so they do not propagate through to the grid.

One option is to perform the insert via the SqlDataSource[^] which will do the job with no code-behind in some [limited] circumstances.

A simpler method is to call GridView1.DataBind() after the insert code you have supplied.


在save方法之后重新绑定数据源.

Rebind the datasource after the save method.

private void SaveRecord()
{ 
   SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString.ToString());
    SqlDataAdapter da = new SqlDataAdapter();
    con.Open();
    da.InsertCommand = new SqlCommand();
    da.InsertCommand.Connection = con;
    da.InsertCommand.CommandType = CommandType.Text;
    da.InsertCommand.CommandText = "INSERT INTO [User] ([NAME]) VALUES (@NAME)";
    da.InsertCommand.Parameters.AddWithValue("@NAME", txtName.Text);            
    da.InsertCommand.ExecuteNonQuery();
    con.Close();
}

protected void btnSave_Click(object sender, EventArgs e)
{   
   SaveRecord();
   GridView1.DataSource = SqlDataSource1;
   GridView1.DataBind();
}



问候,
爱德华



Regards,
Eduard


这篇关于如何刷新网格...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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