2013-05-29 11 views
6

Tôi đang cố gắng tạo một cơ sở dữ liệu thư viện đơn giản. Tôi liệt kê các kết quả tìm kiếm trong khung nhìn GridView, sau đó tôi có một hộp văn bản và một nút, người dùng nhập vào nút isbn và bấm nút cho vay. Sau đó, nếu có đủ số mục (itemNumber> 0), nó sẽ được người dùng mượn. Dưới đây là ảnh chụp màn hình của giao diện người dùng:Cách làm mới Gridview sau khi nhấn một nút trong asp.net

enter image description here

Câu hỏi của tôi là, khi người dùng nhấn nút vay khoản vay có thể hoặc không có thể thành công. Trong cả hai trường hợp, tôi in một thông báo cho biết liệu khoản vay có thành công hay không, và tôi cũng muốn hiển thị GridView cập nhật. Vấn đề là, sau khi nhấn nút cho vay, GridView biến mất và tôi chỉ thấy hộp văn bản, nút và thông báo trên màn hình. Làm thế nào tôi có thể hiển thị phiên bản cập nhật của GridView sau khi nhấn nút cho vay?

Đây là tập tin mã:

<%@ 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> 

         

Và đây là 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(); 


} 
} 

Và đây là đoạn code cho nút vay:

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


} 

Tôi đánh giá cao sự giúp đỡ nào. Cảm ơn

Trả lời

29

Tất cả bạn phải làm là Trong bLoanButton_Click của bạn, thêm một dòng vào rebind Lưới cho SqlDataSource:

protected void bLoanButton_Click(object sender, EventArgs e) 
{ 

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

GridView1.DataBind(); 


} 

liên quan

5

tôi đã hoàn toàn bị mất trên tại sao Gridview.Databind của tôi() sẽ không làm mới.

Vấn đề của tôi, tôi phát hiện ra, GridView của tôi nằm trong UpdatePanel. Để có được GridView của tôi để CUỐI CÙNG làm mới là thế này:

gvServerConfiguration.Databind() 
uppServerConfiguration.Update() 

"uppServerConfiguration" là id gắn liền với UpdatePanel tôi trong mã asp.net của tôi.

Hy vọng điều này sẽ giúp ai đó.

0

Trước khi liên kết dữ liệu thay đổi phương thức đặt giá thầu GridView, chỉ định GridView.EditIndex đến -1. Nó giải quyết cùng một vấn đề với tôi:

gvTypes.EditIndex = -1; 
gvTypes.DataBind(); 

gvTypes là ID GridView của tôi.

0

Thêm GridView1.DataBind() vào sự kiện nhấn nút không hoạt động đối với tôi. Tuy nhiên, thêm nó vào SqlDataSource1_Updated sự kiện đã làm mặc dù.

Protected Sub SqlDataSource1_Updated(sender As Object, e As SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Updated 
    GridView1.DataBind() 
End Sub