单击GridView中的超链接时如何打开另一个页面 [英] How to open another page when hyperlink in gridview is clicked

查看:653
本文介绍了单击GridView中的超链接时如何打开另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我创建了2个页面,Rental.aspx(单击超链接时将显示的页面)和BookList.aspx(显示链接按钮的页面).
对于出租"页面,我使用了带有BookTitle和Inputs的Gridview.我想到了使GridView只显示从BookList页面中选择的bookTitle(hyperlink)行.

我还创建了2个MySQL表,如下所示:
1)桌子:租金
列:书名,日期,名称,会员ID,电话,签入
2)表格:书目表
列:id,书名,可用性

代码

BookList.aspx GridView部分:

So I''ve created 2 pages, Rental.aspx (the page that will appear when hyperlink is clicked) and BookList.aspx (The page where the linkbutton is displayed).
For the Rental page, I used Gridview with BookTitle and Inputs inside. I thought of making the GridView to display only rows of bookTitle(hyperlink) selected from the BookList page.

I''ve also created 2 MySQL tables like this:
1) Table: rent
Columns: booktitle, date, name, memberID, phone, signIn
2) Table: booklist
Columns: id, booktitle, availability

Codes

BookList.aspx GridView part:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" DataKeyNames="id" OnRowDeleting="GridView1_RowDeleting"  >
    <Columns>
        <asp:TemplateField HeaderText="Id">
            <ItemTemplate>
                <asp:Label ID="Label5" runat="server" Text='<%# Eval("id") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Book Title">
            <ItemTemplate>
           <asp:LinkButton ID="bookTitle" OnClick="link_Click" Text='<%# Eval("bookTitle") %>' runat="server" ></asp:LinkButton>

            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Availability">

            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Eval("availability") %>'></asp:Label>
            </ItemTemplate>

        </asp:TemplateField>



        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="delete" CommandName="Delete" runat="server">Delete</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>



BookList.aspx.cs



BookList.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Adapters;
using System.Data;
using MySql.Data.MySqlClient;
using System.Windows;

namespace Library_System
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        DataSet myData = new DataSet();
        MySqlConnection conn = new MySqlConnection();
        protected void Page_Load(object sender, EventArgs e)
        {
           conn.ConnectionString = "server=127.0.0.1;database=test;port=3306;username=root;password=";
            if(!IsPostBack)
            {
                BindData();
       
            }
          
        }
   private void BindData()
        {
            DataTable dt = new DataTable();
            using (conn)
            {
                MySqlDataAdapter adp = new MySqlDataAdapter("select * from test.booklist", conn);
                adp.Fill(dt);

                if (dt.Rows.Count &gt; 0)
                {
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }

        protected void link_Click(object sender, EventArgs e)
        {
            Response.Redirect("Rent.aspx?bookTitle=" + ((LinkButton)sender).Text);

        }



Rent.aspx



Rent.aspx

<asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" AutoGenerateColumns="False"  OnRowUpdating="GridView1_RowUpdating">
            <Columns>
                <asp:TemplateField HeaderText="Book Title">
                    <ItemTemplate>
                        <asp:Label ID="book" runat="server" Text='<%# Eval("bookTitle") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Date">
                    <ItemTemplate>
                        <asp:TextBox ID="date" runat="server" Text='<%# Eval("date") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:TextBox ID="name" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Member ID">
                    <ItemTemplate>
                        <asp:TextBox ID="memberID" runat="server" Text='<%# Eval("memberID") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Phone">
                    <ItemTemplate>
                        <asp:TextBox ID="phone" runat="server" Text='<%# Eval("phone") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Sign In">
                    <ItemTemplate>
                        <asp:ImageButton ID="signIn" runat="server" AlternateText="On Rent" ImageUrl="~/Images/unclick.png" OnClick="SignIn_Click" />
                    </ItemTemplate>
                </asp:TemplateField>
            
                <asp:TemplateField>

                    <ItemTemplate>
                        <asp:LinkButton ID="submit" runat="server" Text="Submit" style="background-image: url(~/Images/save)" CommandName="Update"></asp:LinkButton>
                    </ItemTemplate>

                </asp:TemplateField>



Rent.aspx.cs



Rent.aspx.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;
using MySql.Data.MySqlClient;
using System.Windows;


namespace Library_System
{
    public partial class _new : System.Web.UI.Page
    {
        DataSet myData = new DataSet();
        MySqlConnection conn = new MySqlConnection();
        protected void Page_Load(object sender, EventArgs e)
        {
            conn.ConnectionString = "server=127.0.0.1;database=test;port=3306;username=root;password=";
            if (!IsPostBack)
            {
                BindData();
               
            }
        }

        private void BindData()
        {
            string bookTitle = Request.QueryString["bookTitle;"];
            conn.Open();
            DataTable dt = new DataTable();
       
                MySqlCommand cmd = new MySqlCommand("select a.bookTitle from booklist a, rent b where a.bookTitle = b.bookTitle;" + "select * from test.rent where bookTitle= @bookTitle; ", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                MySqlParameter p = new MySqlParameter("@bookTitle", bookTitle);
                cmd.Parameters.Add(p);
                MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
                
                adp.Fill(dt);

                if (dt.Rows.Count > 0)
                {
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            
        }



我认为我可以从书单中单击超链接,但是当重定向时,该错误显示在"adp.Fill(dt);"



I think I''m able to click the hyperlink from booklist but when it''s redirecting, the error was shown in rent.aspx.cs near the "adp.Fill(dt);"

推荐答案

更改
string bookTitle = Request.QueryString["bookTitle;"];




to

string bookTitle = Request.QueryString["bookTitle"];


另一个问题是您在mysql命令中有两个sql select语句,并且试图从结果中填充单个数据表.如果您有两个select sql语句,则将获得两个数据表作为结果.尝试使用数据集;可以有多个表.
更改
DataTable dt = new DataTable();

DataSet ds = new DataSet();

然后更改 adp.Fill(dt);到adp.Fill(ds);
绑定时分配所需的表,例如GridView1.DataSource = ds.Tables[0];

另一种选择是使用一个sql select语句
(select a.bookTitle from booklist a, rent b where a.bookTitle = b.bookTitle;

select * from test.rent where bookTitle= @bookTitle)


another issue is you have two sql select statements in your mysql command and you trying to fill single data table from the result. if you have two select sql statements you will get two data tables as results. try to use DataSet; which can have multiple tables.
change
DataTable dt = new DataTable();
to
DataSet ds = new DataSet();

then change adp.Fill(dt); to adp.Fill(ds);
when you bind assign the table you want, for example GridView1.DataSource = ds.Tables[0];

Another option is use one sql select statement
(select a.bookTitle from booklist a, rent b where a.bookTitle = b.bookTitle;
or
select * from test.rent where bookTitle= @bookTitle)


这篇关于单击GridView中的超链接时如何打开另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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