我已经创建了一个带有按钮控件和gridview控件的共享点Web部件。当我单击按钮控件时,将出现一个带有两个文本框名称和城市的新窗口。当我输入一些数据并单击“确定”时,该数据将被添加到gridview中。我已经使用JavaScript来获取此窗口。
问题:-实际的操作正在执行,并且数据正在添加到gridview。但是,在手动刷新页面之前,我无法在gridview中检查此新添加的数据。由于这不是理想的应用程序行为,因此有人可以建议我如何实现这一目标。
任何帮助将不胜感激...谢谢!
需要刷新才能查看更新的数据。
如果要摆脱回发体验,则应将整个内容包装在更新面板中。
要以对asp.net友好的方式触发回发,请在页面上单击一个按钮。通过CSS隐藏它。通过js代码,您可以触发此按钮的点击。
编辑(代码):
简而言之,这就是我要完成的工作。我将“刷新”按钮置于可见状态。如果需要,可以将其包装在UpdatePanel中。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApp.PopupAdd.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>GridView Refresh Example</title>
<script>
function openPopup() {
open('popup.aspx','_blank', 'height=300,width=200')
}
function refreshPage() {
document.getElementById('btnRefresh').click();
}
</script>
</head>
<body>
<h2>GridView refresh example</h2>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnRefresh" runat="server" Text="Refresh" OnClick="btnRefresh_Click" />
<asp:Button ID="Button1" runat="server" OnClientClick="javascript:openPopup()" Text="Add" UseSubmitBehavior="False" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="StudentName" HeaderText="StudentName" SortExpression="StudentName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Students]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApp.PopupAdd
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnRefresh_Click(object sender, EventArgs e)
{
GridView1.DataBind();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="popup.aspx.cs" Inherits="WebApp.PopupAdd.popup" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
function update() {
window.opener.refreshPage();
window.close();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApp.PopupAdd
{
public partial class popup : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
var sql = "insert into students (studentname) values (@name)";
SqlConnection cxn = new SqlConnection();
cxn.ConnectionString = ConfigurationManager
.ConnectionStrings["ConnectionString"].ConnectionString;
var cmd = cxn.CreateCommand();
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("@name", TextBox1.Text);
cxn.Open();
cmd.ExecuteNonQuery();
cxn.Close();
ClientScript.RegisterStartupScript(this.GetType(),"update", "update()", true);
}
}
}
我使用的表格是一个简单的学生表格,其中包含字段id,studentname。
我以前的建议是用CSS隐藏“刷新”按钮。(style="visibility:hidden"
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句