如何在按下asp.net中的按钮后刷新Gridview [英] How to refresh Gridview after pressed a button in asp.net

查看:101
本文介绍了如何在按下asp.net中的按钮后刷新Gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个简单的图书馆数据库。我在GridView中列出搜索结果,然后我有一个文本框和一个按钮,用户输入isbn并点击贷款按钮。然后,如果有足够数量的项目(itemNumber> 0),则它由用户借出。以下是用户界面的屏幕截图:





我的问题是,当用户点击贷款按钮时,贷款可能会或可能不会成功。在这两种情况下,我都会打印一条消息,指出贷款是否成功,并且我还希望显示更新后的gridview。问题是,按下贷款按钮后,gridview消失,我只能看到文本框,按钮和屏幕上的消息。如何在按下贷款按钮后显示gridview的更新版本?



以下是代码文件:

 <%@ Page Language =C#AutoEventWireup =trueCodeFile =SearchResults.aspx.csInherits =Pages_SearchResults%> 

<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://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 =form1runat =server>
< div>

< / div>
将ASP:GridView的ID = GridView1 RUNAT = 服务器 的AutoGenerateColumns = 假
的DataKeyNames = ISBN 的DataSourceID = SqlDataSource1
onselectedindexchanged = GridView1_SelectedIndexChanged
onrowcommand =GridView1_RowCommand>
<列>
< asp:BoundField DataField =TitleHeaderText =TitleSortExpression =Title/>
< asp:BoundField DataField =ISBNHeaderText =ISBNReadOnly =True
SortExpression =ISBN/>
< asp:BoundField DataField =AuthorNameHeaderText =AuthorName
SortExpression =AuthorName/>
< asp:BoundField DataField =AuthorlNameHeaderText =AuthorlName
SortExpression =AuthorlName/>
< asp:BoundField DataField =ItemTypeHeaderText =ItemType
SortExpression =ItemType/>
< asp:BoundField DataField =PublishYearHeaderText =PublishYear
SortExpression =PublishYear/>



将ASP:绑定列数据字段= numOfCopies 的HeaderText =
的SortExpression = numOfCopies/> 的副本数量;



< /专栏>
< / asp:GridView>
< asp:SqlDataSource ID =SqlDataSource1runat =server
ConnectionString =<%$ ConnectionStrings:ConnectionString%>
SelectCommand =SELECT * FROM [Items] WHERE([Title] LIKE'%'+ @Title +'%')>
< SelectParameters>
< asp:FormParameter FormField =tSearchBoxName =TitleType =String/>
< / SelectParameters>
< / asp:SqlDataSource>
< br />
< asp:Label ID =Label1runat =serverText =输入ISBN to loan:>< / asp:Label>

  

   








/ b>

这里是.cs文件:

  using System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用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 =数据源= .\\SQLEXPRESS; AttachDbFilename = d:\\Users\\SUUSER\\Documents\\Visual工作室2010\\Projects \\ Library \\\LibWebSite\\\\App_Data\\LibDatabase.mdf; Integrated Security = True; User Instance = True;

Int32验证;

串标题= GridView1.HeaderRow.Cells [0]。文本,ISBN = GridView1.HeaderRow.Cells [1]。文本,名字= GridView1.HeaderRow.Cells [2]。文本,L-NAME = GridView1.HeaderRow.Cells [3] .Text,type = GridView1.HeaderRow.Cells [4] .Text,year = GridView1.HeaderRow.Cells [5] .Text;


$ b protected void bLoanButton_Click(object sender,EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString =数据源= .\\SQLEXPRESS; AttachDbFilename = d:\\Users\\SUUSER\\Documents\\Visual工作室2010\\Projects \\ Library \\\LibWebSite\\\\App_Data\\LibDatabase.mdf; Integrated Security = True; User Instance = True;

string user =从CurrentUser中选择CurrentID;

SqlCommand cmd1 = new SqlCommand(user,con);
con.Open();
string get = cmd1.ExecuteScalar()。ToString();
$ b $ string query1 =insert into LoanTable(StudId,ISBN,onBorrow)values(
+'+ get +'+,+'+ tLoanBox。 Text +'+,
+'+1+'+);

string numQuery =从项目中选择numOfCopies其中ISBN ='+ tLoanBox.Text +';

SqlCommand cmdnumQuery = new SqlCommand(numQuery,con);

SqlCommand cmd2 = new SqlCommand(query1,con);

int结果;

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


result = cmd2.ExecuteNonQuery();
$ b $ if(num> 0)
{

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

con.Close();


}
}

这里是按钮的代码:

pre $ protected void bLoanButton_Click(object sender,EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString =数据源= .\\SQLEXPRESS; AttachDbFilename = d:\\Users\\SUUSER\\Documents\\Visual工作室2010\\Projects \\ Library \\\LibWebSite\\\\App_Data\\LibDatabase.mdf; Integrated Security = True; User Instance = True;

string user =从CurrentUser中选择CurrentID;

SqlCommand cmd1 = new SqlCommand(user,con);
con.Open();
string get = cmd1.ExecuteScalar()。ToString();
$ b $ string query1 =insert into LoanTable(StudId,ISBN,onBorrow)values(
+'+ get +'+,+'+ tLoanBox。 Text +'+,
+'+1+'+);





SqlCommand cmd2 = new SqlCommand(query1,con);

int结果;




result = cmd2.ExecuteNonQuery();



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

}





con.Close();


}

感谢您的帮助。谢谢

解决方案

在bLoanButton_Click中,添加一行以重新绑定Grid到SqlDataSource:

  protected void bLoanButton_Click(object sender,EventArgs e)
{

//您的代码
........

GridView1.DataBind();


}

问候

I am trying to make a simple library database. I list the search results in a gridview, then i have a textbox and a button, user enters the isbn and clicks loan button. Then, if there is enough number of items (itemNumber>0) it is loaned by user. Here is the screenshot of user interface:

My question is, when user clicks loan button the loan may or may not be succesful. In both cases, i print a message indicating whether loan is succesful or not, and i also want the updated gridview to be displayed. The problem is, after pressing the loan button the gridview disappears and i just see the textbox, button and the message on the screen. How can i show the updated version of gridview after pressing loan button?

Here is the code file:

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

      

And here is the .cs file:

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


}
}

And here is the code for loan button:

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


}

I appreciate any help. Thanks

解决方案

All you have to do is In your bLoanButton_Click , add a line to rebind the Grid to the SqlDataSource :

protected void bLoanButton_Click(object sender, EventArgs e)
{

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

GridView1.DataBind();


}

regards

这篇关于如何在按下asp.net中的按钮后刷新Gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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