2013-05-29 60 views
6

我想製作一個簡單的庫數據庫。我在GridView中列出搜索結果,然後我有一個文本框和一個按鈕,用戶輸入isbn並點擊貸款按鈕。然後,如果有足夠數量的項目(itemNumber> 0),則它由用戶借出。下面是用戶界面的截圖:如何在asp.net中按下按鈕後刷新Gridview

enter image description here

我的問題是,當用戶點擊按鈕貸款,貸款可能會或可能不會成功的。在這兩種情況下,我都會打印一條消息,指出貸款是否成功,並且我還希望顯示更新後的gridview。問題是,按下貸款按鈕後,gridview消失,我只能看到文本框,按鈕和屏幕上的消息。如何在按下貸款按鈕後顯示gridview的更新版本?

下面是代碼文件:

<%@ 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˚F ILE:

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(); 


} 

我感謝所有幫助。由於

回答

29

所有你需要做的就是在你bLoanButton_Click,添加一行重新綁定網格到的SqlDataSource:

protected void bLoanButton_Click(object sender, EventArgs e) 
{ 

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

GridView1.DataBind(); 


} 

問候

5

我完全失去了爲什麼我的Gridview.Databind()不會刷新。

我發現我的問題是我的gridview是在UpdatePanel中。爲了讓我的GridView控件FINALLY刷新是這樣的:

gvServerConfiguration.Databind() 
uppServerConfiguration.Update() 

「uppServerConfiguration」是我的UpdatePanel在我的asp.net代碼相關的標識。

希望這可以幫助別人。

0

在數據綁定更改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