Salve. Mi sto cimentando coi primi rudimenti di ASP.NET
Per ora scopiazzo dai libri e modifico a piacimento (da qualche parte devo pur cominciare!).
Mi dite come mai all'apertura della pagina la questa GridView non mostra nulla? Vi posto il codice strettamente necessario al controllo... tutto il resto non c'entra nulla e finirebbe per occupare spazio inutile nella pagina:
default.aspx
codice:
<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.master" CodeFile="Default.aspx.cs" Inherits="_Default" Title="eCommerce"%>
<asp:Content id="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:gridview id="GridView1" runat="server"
autogeneratecolumns="False"
datakeynames="productid"
datasourceid="SqlDataSource3"
onselectedindexchanged="GridView1_SelectedIndexChanged"
allowpaging="True">
<HeaderStyle CssClass="header" />
<RowStyle CssClass="rigaA" />
<AlternatingRowStyle CssClass="rigaB" />
<columns>
<asp:imagefield dataimageurlfield="thumbnail" dataimageurlformatstring="~\Images\{0}"> </asp:imagefield>
<asp:BoundField DataField="name" HeaderText="Prodotto"/>
<asp:BoundField DataField="shorttext" HeaderText="Descrizione"/>
<asp:BoundField DataField="price" DataFormatString="{0:c}" HeaderText="Price" />
<asp:CommandField SelectText="View" ShowSelectButton="true" />
<asp:BoundField DataField="SalePrice" DataFormatString="In vendita {0:c}!" SortExpression="SalePrice">
<HeaderStyle BorderStyle="None" />
<ItemStyle BorderStyle="None" Font-Bold="true" />
</asp:BoundField>
</columns>
</asp:gridview>
Selezionare una categoria:
<asp:DropDownList id="ddlCategory" runat="server"
autopostback="true" datasourceid="SqlDataSource2" datatextfield="name" datavaluefield="catid" width="127px">
</asp:DropDownList>
<asp:sqldatasource id="SqlDataSource2" runat="server"
connectionstring="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT catid, name, [desc] FROM Categories ORDER BY name">
</asp:sqldatasource>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT P.productid, P.catid, P.name, P.shorttext, P.longtext, P.price, P.image, P.thumbnail,
FP.saleprice FROM Products AS P LEFT OUTER JOIN FeaturedProducts AS FP ON P.productid = FP.productid
WHERE (P.catid = @catid)">
<SelectParameters>
<asp:ControlParameter Name="catid" ControlID="ddlCategory" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Content>
default.aspx.cs
codice:
using System;
using System.Configuration;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string CatID = Request.QueryString["cat"];
if (CatID != null)
ddlCategory.SelectedValue = CatID;
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string ProductID = GridView1.SelectedValue.ToString().Trim();
string CatID = ddlCategory.SelectedValue;
Response.Redirect("product.aspx?prod="+ProductID+"&cat="+CatID);
}
}
ora, ammesso che io sia buono a poco... mi pare che le istruzioni siano chiare.
Se c'è PostBack il parametro della SqlDataSource3 lo prende automaticamente dalla dropdownlist, ce invece non c'è postback il valore della ddl ce lo infilo io forzatamente.
Come mai però appare qualcosa solo se io clicco sulla dropdownlist, scatenando un postback?
Grazie