如何在ASP.NET中按下按钮后刷新GridView

10
我正在尝试制作一个简单的图书馆数据库。我在网格视图中列出搜索结果,然后有一个文本框和一个按钮,用户输入ISBN并点击借贷按钮。如果有足够数量的项目(itemNumber> 0),则由用户借出。这是用户界面的截图:

enter image description here

我的问题是,当用户点击借贷按钮时,借款可能成功也可能不成功。在两种情况下,我都会打印一条消息,指示是否成功或失败,并且我还希望显示更新后的网格视图。问题是,在按下借贷按钮后,网格视图消失了,我只看到屏幕上的文本框,按钮和消息。如何在按下借贷按钮后显示更新版本的网格视图?
这是代码文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SearchResults.aspx.cs" Inherits="Pages_SearchResults" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="ISBN" DataSourceID="SqlDataSource1" 
    onselectedindexchanged="GridView1_SelectedIndexChanged" 
    onrowcommand="GridView1_RowCommand">
    <Columns>
        <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
        <asp:BoundField DataField="ISBN" HeaderText="ISBN" ReadOnly="True" 
            SortExpression="ISBN" />
        <asp:BoundField DataField="AuthorName" HeaderText="AuthorName" 
            SortExpression="AuthorName" />
        <asp:BoundField DataField="AuthorlName" HeaderText="AuthorlName" 
            SortExpression="AuthorlName" />
        <asp:BoundField DataField="ItemType" HeaderText="ItemType" 
            SortExpression="ItemType" />
        <asp:BoundField DataField="PublishYear" HeaderText="PublishYear" 
            SortExpression="PublishYear" />



        <asp:BoundField DataField="numOfCopies" HeaderText="Number of Copies" 
            SortExpression="numOfCopies" />



    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT * FROM [Items] WHERE ([Title] LIKE '%' + @Title + '%')">
    <SelectParameters>
        <asp:FormParameter FormField="tSearchBox" Name="Title" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>
<br />
<asp:Label ID="Label1" runat="server" Text="Type ISBN to loan:"></asp:Label>

  

   

这是 .cs 文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Pages_SearchResults : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    Response.Redirect("Default.aspx");
}


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Users\\SUUSER\\Documents\\Visual Studio 2010\\Projects\\Library\\LibWebSite\\App_Data\\LibDatabase.mdf;Integrated Security=True;User Instance=True";

    Int32 verify;

    string title = GridView1.HeaderRow.Cells[0].Text, isbn = GridView1.HeaderRow.Cells[1].Text, name = GridView1.HeaderRow.Cells[2].Text, lname = GridView1.HeaderRow.Cells[3].Text, type = GridView1.HeaderRow.Cells[4].Text, year = GridView1.HeaderRow.Cells[5].Text;


}
protected void bLoanButton_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Users\\SUUSER\\Documents\\Visual Studio 2010\\Projects\\Library\\LibWebSite\\App_Data\\LibDatabase.mdf;Integrated Security=True;User Instance=True";

    string user = "select CurrentID from CurrentUser";

    SqlCommand cmd1 = new SqlCommand(user, con);
    con.Open();
    string get = cmd1.ExecuteScalar().ToString();

    string query1 = "insert into LoanTable(StudId,ISBN,onBorrow) values ("
        + "'" + get + "'" + "," + "'" + tLoanBox.Text + "'" + ","
        + "'" + "1" + "'" + ")";

    string numQuery = "select numOfCopies from Items where ISBN='" + tLoanBox.Text + "'";

    SqlCommand cmdnumQuery = new SqlCommand(numQuery, con);

    SqlCommand cmd2 = new SqlCommand(query1, con);

    int result;

    int num=Convert.ToInt32(cmdnumQuery.ExecuteScalar());


    result = cmd2.ExecuteNonQuery();

    if (num > 0)
    {

        if (result > 0)
            Response.Redirect("LoanSuccesfull.aspx");
    }
    else
        notAvailable.Visible = true;

    con.Close();


}
}

这里是“贷款”按钮的代码:

 protected void bLoanButton_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Users\\SUUSER\\Documents\\Visual Studio 2010\\Projects\\Library\\LibWebSite\\App_Data\\LibDatabase.mdf;Integrated Security=True;User Instance=True";

    string user = "select CurrentID from CurrentUser";

    SqlCommand cmd1 = new SqlCommand(user, con);
    con.Open();
    string get = cmd1.ExecuteScalar().ToString();

    string query1 = "insert into LoanTable(StudId,ISBN,onBorrow) values ("
        + "'" + get + "'" + "," + "'" + tLoanBox.Text + "'" + ","
        + "'" + "1" + "'" + ")";





    SqlCommand cmd2 = new SqlCommand(query1, con);

    int result;




    result = cmd2.ExecuteNonQuery();



        if (result > 0)
        {
            loanSuccesful.Visible = true;
            Response.Redirect("LoanSuccesfull.aspx");

        }





    con.Close();


}

我感激任何帮助。谢谢。

4个回答

41

您需要做的就是在bLoanButton_Click事件中,添加一行代码以将Grid重新绑定到SqlDataSource:

protected void bLoanButton_Click(object sender, EventArgs e)
{

//your same code
........

GridView1.DataBind();


}

问候


9

我完全不知道为什么我的Gridview.Databind()无法刷新。

后来我发现,问题是因为我的GridView在UpdatePanel里面。要让我的GridView最终刷新,需要这样做:

gvServerConfiguration.Databind()
uppServerConfiguration.Update()

uppServerConfiguration 是我在 asp.net 代码中与我的 UpdatePanel 相关联的 id。

希望这能对某些人有所帮助。


1
在数据绑定更改GridView数据绑定方法之前,将GridView.EditIndex分配为-1。这为我解决了相同的问题:
 gvTypes.EditIndex = -1;
 gvTypes.DataBind();

gvTypes 是我的 GridView ID。


0
将GridView1.DataBind()添加到按钮单击事件中对我没有起作用。但是,将其添加到SqlDataSource1_Updated事件中确实起作用了。
Protected Sub SqlDataSource1_Updated(sender As Object, e As SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Updated
    GridView1.DataBind()
End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接