How can i to navigate to the book_detail.aspx page with one attributes which will stored into querystring variable

StackOverflow https://stackoverflow.com/questions/23380710

문제

I create a book details page which show the book cover pics,book title,author,isbn no etc.

book_all.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/book.master" AutoEventWireup="true" CodeFile="book_all.aspx.cs" Inherits="book_all" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div>
        <asp:TextBox ID="res" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" 
            BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" 
            DataKeyNames="isbn" DataSourceID="SqlDataSourceAll" PageSize="4">
            <Columns>
                <asp:TemplateField HeaderText="cover_pic" SortExpression="cover_pic">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("cover_pic") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Image ID="Image1" runat="server" ImageUrl='<%# Bind("cover_pic") %>' Height="125px" Width="100px" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="isbn" HeaderText="isbn" ReadOnly="True" 
                    SortExpression="isbn" />
                <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
                <asp:BoundField DataField="author" HeaderText="author" 
                    SortExpression="author" />
                <asp:BoundField DataField="edition" HeaderText="edition" 
                    SortExpression="edition" />
                <asp:BoundField DataField="publisher" HeaderText="publisher" 
                    SortExpression="publisher" />
                <asp:BoundField DataField="type" HeaderText="type" SortExpression="type" />
                <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
                <asp:BoundField DataField="qty" HeaderText="qty" SortExpression="qty" />
                <asp:BoundField DataField="available" HeaderText="available" 
                    SortExpression="available" />
                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("cover_pic") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="detail" runat="server" Text="Details" OnClick="detail_click"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSourceAll" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT * FROM [book_info]"></asp:SqlDataSource>        
    </div>
</asp:Content>

I want to navigate book_detail.aspx page with only one tuple whose detail button will be click(Link Button name detail)

This is the cs code of the previous aspx page.

book_all.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.IO;
using System.Data.SqlClient;

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

    }
    protected void detail_click(object sender, EventArgs e)
    {
        Response.Redirect("book_detail.aspx?IsbnNo="+GridView1.FindControl("isbn").ToString());

    }
}
도움이 되었습니까?

해결책

I hope the below example will help you. This is an abstract from one of my projects which do the same thing as you need.

As an outline, you have to perform the following steps:

  • In the GridView, create a TemplateField which contain the link to navigate to other page (linkButton or imageButton or Button)
  • Set CommandArgument and CommandName properties for that link control
  • In your C# code, handle gridView's RowCommand event
  • Based on the commandName and commandArgument navigate to the other page.

.ASPX

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" OnRowCommand="gv_RowCommand"
    EmptyDataText="No record found" Width="1000px">
    <Columns>
        <asp:BoundField DataField="ISBN" />
        <asp:BoundField DataField="Title" HeaderText="Title"/>
        .
        .
        .
        .
        .
        <asp:TemplateField HeaderText="Actions">
            <ItemTemplate>
                <asp:LinkButton ID="lnkEdit" runat="server" CommandArgument='<%# Eval("ISBN") %>'
                    CommandName="viewDetails">View Details</asp:LinkButton>             </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

In the above code, the main thinks to see are setting CommandArgument and CommandName properties of the LinkButton. These will be used in the .cs code

.ASPX.CS

protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
    // ensure that commandArgument is not null, which may happen when someone tries to hack the page
    string isbn = e.CommandArgument.ToString();
    if (string.IsNullOrEmpty(uid))
    {
        // error message, someone has tried to hack the web page
        return;
    }

    if (e.CommandName == "viewDetails")
    {
        Response.Redirect("book-details.aspx?isbn=" + isbn);
        return;
    }

    // handle any other command that may be there (like delete, edit, etc)
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top