超链接配置中的问题

超链接配置中的问题

本文介绍了超链接配置中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<asp:HyperLinkField DataNavigateUrlFields="CategoryID"
                    DataNavigateUrlFormatString="~/ProductCatalog.aspx?CategoryID={0}"
                    DataTextField="CategoryName" HeaderText="Categories" />



我在超链接显示的gridview中有一些类别.

当我单击一些类别时.它显示数据库中的所有产品.不要根据我点击的类别过滤产品.问题出在哪儿.请帮助我.


这是productcatalog.aspx页面代码C#



i have some catagory in the gridview shown by the hyperlinks.

when i click on some catagory. it showing all the products in database. not filtering the products on the bases of catagory which i clicked. where is the problem. pls help me.


here is the productcatalog.aspx page code C#

SqlConnection conn = new SqlConnection("Data Source=FAROOQPC\\SQLEXPRESS; Initial Catalog=ecommerce; Integrated Security=True");
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {

           LoadGridView();

       }

   }
   private void LoadGridView()
   {
       //conn.Open();
       SqlDataAdapter da = new SqlDataAdapter("Select * from ESK_Products", conn);
       DataSet ds = new DataSet();
       da.Fill(ds, "mydb");
       GridView1.DataSource = ds.Tables[0];
       GridView1.DataBind(); ;
       //conn.Close();
   }



productcatalog.aspx页的asp.net代码



productcatalog.aspx page asp.net code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProductCatalog.aspx.cs" Inherits="ProductCatalog" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>


        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

            onselectedindexchanged="GridView1_SelectedIndexChanged" Width="385px">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>

                        <table style="width: 100%">
                        <tr>
                            <td rowspan="4" valign="top" width="5%">
                                <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ProductImage") %>' /></td>
                            <td align="left">
                                <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("ProductID", "~/displayproduct.aspx?productid={0}") %>'

                                    Text='<%# Eval("ProductName") %>'></asp:HyperLink></td>
                        </tr>
                        <tr>
                            <td align="left">
                                <asp:Label ID="Label3" runat="server" Text='<%# GetShortDescription(Eval("Description").ToString()) %>'></asp:Label></td>
                        </tr>
                        <tr>
                            <td align="left">
                                <asp:Label ID="Label5" runat="server" SkinID="FormLabel" Text="Price :"></asp:Label>
                                <asp:Label ID="Label4" runat="server" Text='<%# Eval("UnitCost", "{0:C}") %>'></asp:Label></td>
                        </tr>
                        <tr>
                            <td align="right">
                                &nbsp;</td>
                        </tr>
                    </table>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

    </div>
    </form>
</body>
</html>




catagories.aspx页面C#code




catagories.aspx page C#code

SqlConnection conn = new SqlConnection("Data Source=FAROOQPC\\SQLEXPRESS; Initial Catalog=ecommerce; Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            LoadGridView();

        }
    }


    private void LoadGridView()
    {
        //conn.Open();
        SqlDataAdapter da = new SqlDataAdapter("Select CategoryID,CategoryName from ESK_Categories", conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "mydb");
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind(); ;
        //conn.Close();
    }





catogories.aspx页的asp.net代码





catogories.aspx page asp.net code

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>


        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

            Height="216px" Width="269px">
            <Columns>

                <asp:HyperLinkField DataNavigateUrlFields="CategoryID"

                    DataNavigateUrlFormatString="~/ProductCatalog.aspx?CategoryID={0}"

                    DataTextField="CategoryName" HeaderText="Categories" />
            </Columns>
        </asp:GridView>

    </div>
    </form>
</body>

please help me y is it not filtering the data on the bases of catagory id
</html>

推荐答案

<asp:HyperLinkField DataNavigateUrlFields="CategoryID"
                    DataNavigateUrlFormatString="~/ProductCatalog.aspx?CategoryID={0}"
                    DataTextField="CategoryName" HeaderText="Categories" />



这意味着CategoryId将作为QueryString参数传递到ProductCatalog页面.

在此页面中,您有



Which means the CategoryId will be passed to the ProductCatalog page as a QueryString parameter.

In this page you have

SqlDataAdapter da = new SqlDataAdapter("Select * from ESK_Products", conn);



它不使用传递给它的QueryString参数.

如果要按CategoryId过滤结果,则应该是这样的



which makes no use of the QueryString parameter that was passed to it.

If you want to filter the results by the CategoryId then it should be something like this

string query = string.Format("Select * from ESK_Products WHERE CategoryId={0}", Request["CategoryId"]);
SqlDataAdapter da = new SqlDataAdapter(query, conn);



[/UPDATE]



[/UPDATE]



这篇关于超链接配置中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:31