关于DropDownList1_SelectedIndexChanged一个难以理解的问题。。。。

fanglibang 2005-09-16 04:39:00
以下是代码:问题是不执行DropDownList1_SelectedIndexChanged方法。。请大家帮帮看看为什么???

<%@ Register TagPrefix="eshop" TagName=HeaderMenu Src="UserControl/HeaderMenu.ascx" %>
<%@ Register TagPrefix="eshop" TagName=CategoryList Src="UserControl/CategoryList.ascx" %>
<%@ Page language="c#" Codebehind="Productlist.aspx.cs" AutoEventWireup="false" Inherits="eshop_test.Productlist" enableViewState="True"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Productlist</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<TABLE height="120" cellSpacing="0" cellPadding="0" width="512" border="0" runat="server">
<TR bgColor="lightsteelblue">
<TD style="WIDTH: 633px; HEIGHT: 25px" colSpan="2" height="25"><eshop:headermenu id="Header" runat="server"></eshop:headermenu></TD>
</TR>
<TR vAlign="top">
<TD width="25%" height="10"><eshop:categorylist id="Category" runat="server"></eshop:categorylist></TD>
<TD style="WIDTH: 555px" height="10">
<h3>查询结果:</h3>
<asp:datagrid id="DataGridSearch" runat="server" AutoGenerateColumns="False" Width="528px">
<HeaderStyle BackColor="DeepSkyBlue"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="ProductId" HeaderText="商品编号"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="商品名称">
<ItemTemplate>
<asp:HyperLink ID=shangbin Runat=server text='<%# DataBinder.Eval(Container.DataItem,"ProductName")%>' NavigateUrl='<%# "Productlist.aspx?productID="+ DataBinder.Eval(Container.DataItem,"ProductId")%> '>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Intro" HeaderText="商品简单描述"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="购买">
<ItemTemplate>
<asp:HyperLink ID="Hyperlink2" Runat=server NavigateUrl='<%# "AddToCart.aspx?productID="+ DataBinder.Eval(Container.DataItem,"ProductId")%> '>
<img src="Images\next_down.gif">
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
<table width="100%" runat="server">
<tr>
<td style="WIDTH: 361px">总共<asp:label id="PageCount" Runat="server"></asp:label>样商品     
共有
<asp:label id="Label1" runat="server" Width="32px">Label</asp:label>页</td>
<td align="right">转到了第
<asp:dropdownlist id="DropDownList1" runat="server" AutoPostBack="True" ></asp:dropdownlist>页</td>
</tr>
</table>
</TD>
</TR>
</TD></TR></TABLE>
</FONT>
</form>
</body>
</HTML>

这以下是脚体:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace eshop_test
{
/// <summary>
/// Productlist 的摘要说明。
/// </summary>
public class Productlist : System.Web.UI.Page
{
protected System.Web.UI.WebControls.HyperLink shangbin;
protected System.Web.UI.WebControls.HyperLink Hyperlink2;
protected System.Web.UI.WebControls.DataGrid DataGridSearch;
protected System.Web.UI.WebControls.Label PageCount;
protected System.Web.UI.WebControls.DataGrid ProductDataGrid1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
private static int PageSize=5;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//如果是第一次加载,则加载第一页。


if(!Page.IsPostBack)
{
ShowPage(1,PageSize);

//得到商品的总数。。。
PageCount.Text=eshop_test.BLL.Product.GetProductCountByCategory(int.Parse(Request.Params["categoryID"])).ToString();

}


}
public void ShowPage( int PageIndex,int pageSize)

{
//加载选定项目的所有商品。
// ProductDataGrid1.DataSource=eshop_test.BLL.Product.GetProductsByCategory(int.Parse(Request.Params["categoryID"]), PageIndex, pageSize);
// ProductDataGrid1.DataBind();

string connstring="server=fang;database=eshop_test;uid=sa;pwd=2200437";

SqlConnection objcon=new SqlConnection(connstring);
objcon.Open();
SqlCommand objcom=new SqlCommand();
objcom.Connection=objcon;
objcom.CommandType=CommandType.StoredProcedure ;
objcom.CommandText="GetProductByCategory";
objcom.Parameters.Add("@categoryId",int.Parse(Request.Params["categoryID"]));
objcom.Parameters.Add("@pageSize",pageSize);
objcom.Parameters.Add("@pageIndex",PageIndex);

DataGridSearch.DataSource=objcom.ExecuteReader();

DataGridSearch.DataBind();
objcon.Close();
//得到商品的总页数后绑定到 DropDownList1控件。。。
int tatolcount=eshop_test.BLL.Product.GetProductCountByCategory(int.Parse(Request.Params["categoryID"]));
int count;
if(tatolcount%PageSize==0)
{
count=tatolcount/PageSize;
Label1.Text=count.ToString();
}
else
{
count=tatolcount/PageSize+1;
Label1.Text=count.ToString();


}
formfell.Items.Clear();

for(int i=1;i<=count;i++)
{
ListItem page1=new ListItem(i.ToString());
DropDownList1.Items.Add(page1);

}
DropDownList1.SelectedItem.Text=PageIndex.ToString();



}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{

this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion





public void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{

int selectV= Convert.ToInt32(DropDownList1.SelectedItem.Text );

ShowPage(selectV,PageSize);




}


}
}

...全文
303 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangsuipeng 2005-09-17
  • 打赏
  • 举报
回复
那可不是绑定多次的问题,你的DropDownList1如果VIEWSTATE 在ENABLE情况下,每次POSTBACK都重新加多了count个LISTITEM
adandelion 2005-09-17
  • 打赏
  • 举报
回复
public void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{

int selectV= Convert.ToInt32(DropDownList1.SelectedItem.Text );

ShowPage(selectV,PageSize);

}

里面设置断点调试,没用运行?不会吧,是运行结果有问题吧.
我觉得DropDownList1的绑定一次就可以了,为什么没每次页码变化是后都绑定呢?

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧