Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Product : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Retrieve ProductID from the query string
        string productId = Request.QueryString["ProductID"];
        // Retrieves product details
        ProductDetails pd = CatalogAccess.GetProductDetails(productId);
        // Does the product exist?
        if (pd.Name != null)
        {
            PopulateControls(pd);
        }
        else
        {
            Server.Transfer("~/NotFound.aspx");
        }
        // 301 redirect to the proper URL if necessary
        Link.CheckProductUrl(Request.QueryString["ProductID"]);
    }

    // Fill the control with data
    private void PopulateControls(ProductDetails pd)
    {
        // Display product details
        titleLabel.Text = pd.Name;
        descriptionLabel.Text = pd.Description;
        priceLabel.Text = String.Format("{0:c}", pd.Price);
        productImage.ImageUrl = Link.ToProductImage(pd.Image);
        // Set the title of the page
        this.Title = PoshandDanglesConfiguration.SiteName + ": " + pd.Name;

        // obtain the attributes of the product
        DataTable attrTable =
          CatalogAccess.GetProductAttributes(pd.ProductID.ToString());

        // temp variables
        string prevAttributeName = "";
        string attributeName, attributeValue, attributeValueId;

        // current DropDown for attribute values
        Label attributeNameLabel;
        DropDownList attributeValuesDropDown = new DropDownList();

        // read the list of attributes
        foreach (DataRow r in attrTable.Rows)
        {
            // get attribute data
            attributeName = r["AttributeName"].ToString();
            attributeValue = r["AttributeValue"].ToString();
            attributeValueId = r["AttributeValueID"].ToString();

            // if starting a new attribute (e.g. Color, Size)
            if (attributeName != prevAttributeName)
            {
                prevAttributeName = attributeName;
                attributeNameLabel = new Label();
                attributeNameLabel.Text = attributeName + ": ";
                attributeValuesDropDown = new DropDownList();
                attrPlaceHolder.Controls.Add(attributeNameLabel);
                attrPlaceHolder.Controls.Add(attributeValuesDropDown);
            }

            // add a new attribute value to the DropDownList
            attributeValuesDropDown.Items.Add(new ListItem(attributeValue, attributeValueId));
        }
    }
    protected void AddToCartButton_Click(object sender, EventArgs e)
    {

        // Retrieve ProductID from the query string
        string productId = Request.QueryString["ProductID"];
        // Retrieves product details
        ProductDetails pd = CatalogAccess.GetProductDetails(productId);

        // Retrieve the selected product options
        string options = "";
        foreach (Control cnt in attrPlaceHolder.Controls)
        {
            if (cnt is Label)
            {
                Label attrLabel = (Label)cnt;
                options += attrLabel.Text;
            }

            if (cnt is DropDownList)
            {
                DropDownList attrDropDown = (DropDownList)cnt;
                options += attrDropDown.Items[attrDropDown.SelectedIndex] + "; ";
            }
        }

        // The Add to Cart link
        string productUrl = Link.ToProduct(pd.ProductID.ToString());
        string destination = Link.ToPayPalAddItem(productUrl, pd.Name, pd.Price, options);
        Response.Redirect(destination);
    }
}
Posted
Updated 28-Aug-19 4:41am
v3

 
Share this answer
 
Here is my web.config file, let me know if it's ok. I'm new to ASP.NET and C#, I really need some help. May I could put up all the codes if that would help?


XML
<?xml version="1.0" encoding="UTF-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
  </configSections>
  <rewriter>
    <!-- Rewrite department pages -->
    <rewrite url="^.*-d([0-9]+)/?$" to="~/Catalog.aspx?DepartmentID=$1" processing="stop" />
    <rewrite url="^.*-d([0-9]+)/page-([0-9]+)/?$" to="~/Catalog.aspx?DepartmentID=$1&amp;Page=$2" processing="stop" />
    <!-- Rewrite category pages -->
    <rewrite url="^.*-d([0-9]+)/.*-c([0-9]+)/?$" to="~/Catalog.aspx?DepartmentId=$1&amp;CategoryId=$2" processing="stop" />
    <rewrite url="^.*-d([0-9]+)/.*-c([0-9]+)/page-([0-9]+)/?$" to="~/Catalog.aspx?DepartmentId=$1&amp;CategoryId=$2&amp;Page=$3" processing="stop" />
    <!-- Rewrite product details pages -->
    <rewrite url="^.*-p([0-9]+)/?$" to="~/Product.aspx?ProductId=$1" processing="stop" />
  </rewriter>
  <appSettings>
    <add key="PaypalUrl" value="https://www.paypal.com/xclick" />
    <add key="PaypalEmail" value="Test@example.com" />
    <add key="PaypalCurrency" value="USD" />
    <add key="PaypalReturnUrl" value="http://www.poshanddangles.com" />
    <add key="PaypalCancelUrl" value="http://www.poshanddangles.com" />
    <add key="MailServer" value="mail server address" />
    <add key="MailUsername" value="mail username " />
    <add key="MailPassword" value="mail password" />
    <add key="MailFrom" value="mail address" />
    <add key="EnableErrorLogEmail" value="false" />
    <add key="ErrorLogEmail" value="errors@example.com" />
    <add key="ProductsPerPage" value="6" />
    <add key="ProductDescriptionLength" value="60" />
    <add key="SiteName" value="PoshandDangles" />
    <add key="CartPersistDays" value="10" />
  </appSettings>
  <connectionStrings>
    <add name="PoshandDanglesConnection" connectionString="Server=(local)\SqlExpress; Database=PoshandDangles; Integrated Security=SSPI" providerName="System.Data.SqlClient" />
    <remove name="LocalSqlServer" />
    <add name="LocalSqlServer" connectionString="Server=(local)\SqlExpress;Database=PoshandDangles; Integrated Security=SSPI" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <location path="AdminDepartments.aspx">
    <system.web>
      <authorization>
        <allow roles="Administrators" />
        <deny users="*" />
      </authorization>
    </system.web>
  </location>
  <location path="AdminCategories.aspx">
    <system.web>
      <authorization>
        <allow roles="Administrators" />
        <deny users="*" />
      </authorization>
    </system.web>
  </location>
  <location path="AdminProducts.aspx">
    <system.web>
      <authorization>
        <allow roles="Administrators" />
        <deny users="*" />
      </authorization>
    </system.web>
  </location>
  <location path="AdminProductDetails.aspx">
    <system.web>
      <authorization>
        <allow roles="Administrators" />
        <deny users="*" />
      </authorization>
    </system.web>
  </location>
  <location path="AdminProductAttributes.aspx">
    <system.web>
      <authorization>
        <allow roles="Administrators" />
        <deny users="*" />
      </authorization>
    </system.web>
  </location>
  <!-- Only administrators are allowed to access ShoppingCartAdmin.aspx -->
  <location path="AdminShoppingCart.aspx">
    <system.web>
      <authorization>
        <allow roles="Administrators" />
        <deny users="*" />
      </authorization>
    </system.web>
  </location>
  <!-- Only administrators are allowed to access AdminOrders.aspx -->
  <location path="AdminOrders.aspx">
    <system.web>
      <authorization>
        <allow roles="Administrators" />
        <deny users="*" />
      </authorization>
    </system.web>
  </location>
  <!-- Only administrators are allowed to access AdminOrders.aspx -->
  <location path="AdminOrderDetails.aspx">
    <system.web>
      <authorization>
        <allow roles="Administrators" />
        <deny users="*" />
      </authorization>
    </system.web>
  </location>

  <system.web>
      <webServices>
          <protocols>
              <add name="HttpPost" />
          </protocols>
      </webServices>

      <roleManager enabled="true" />
    <globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-US" />
    <!--
            Set compilation debug="true" to insert debugging
            symbols into the compiled page. Because this
            affects performance, set this value to true only
            during development.
        -->
    <compilation debug="true" targetFramework="4.0" />
    <!--
            The <authentication> section enables configuration
            of the security authentication mode used by
            ASP.NET to identify an incoming user.
        -->
    <authentication mode="Forms">
      <forms name="PoshandDanglesLogin" loginUrl="Login.aspx" path="/" protection="All" timeout="60" />
    </authentication>
    <!--
            The <customErrors> section enables configuration
            of what to do if/when an unhandled error occurs
            during the execution of a request. Specifically,
            it enables developers to configure html error pages
            to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
    <customErrors mode="Off" defaultRedirect="~/Oops.aspx">
      <error statusCode="404" redirect="~/NotFound.aspx" />
      <error statusCode="500" redirect="~/Oops.aspx" />
    </customErrors>
    <pages theme="PoshandDanglesDefault" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />
    <httpModules>
      <add type="Intelligencia.UrlRewriter.RewriterHttpModule,Intelligencia.UrlRewriter" name="UrlRewriter" />
    </httpModules>
  </system.web>
  <!--
        The system.webServer section is required for running ASP.NET AJAX under Internet
        Information Services 7.0.  It is not necessary for previous version of IIS.
    -->
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule" />
    </modules>
        <handlers>
            <remove name="PageHandlerFactory-ISAPI-4.0_32bit" />
            <add name="PageHandlerFactory-ISAPI-4.0_32bit" path=".aspx" verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
        </handlers>
  </system.webServer>
</configuration>
 
Share this answer
 
Hi

I had the same issue I added following rewrite to my web.config
<rewrite url="^.*-p([0-9]+)/.*=([0-9]+)/?$" to="~/Product.aspx?ProductId=$1" processing="stop" />

and it's working fine.
 
Share this answer
 
v2
Add an index.htm with below re-direct lines

<html>
  <head>
    <meta http-equiv="Refresh" content="0; url=Default.aspx" />
  </head>

</html>
 
Share this answer
 
Thanks for the link, I saw this befor but I'm using IIS7 ASP.Net 4.0 and I was unable to find this mapping asp.isapi.dll.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900