asp.net(c#) 页面刷新时dropdownlist的问题
页面中有一个dropdownlist控件和datalist控件,通过选定dropdownlist中的值,让datalist显示相应的数据。datalist中可分页,问题来...
页面中有一个dropdownlist控件和datalist控件,通过选定dropdownlist中的值,让datalist显示相应的数据。datalist中可分页,问题来了,就是,当datalist中的数据不止一页时,只要点击下一页,页面刷新时,原先选定的dropdownlist的值会自动清空,导致下一页时显示的数据不正确,请问该如何解决这个问题。代码如下:
前台:
<asp:DropDownList ID="DropDownList1" runat="server" Width="40px" onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True" >
</asp:DropDownList>
<asp:DataList ID="DataList1" runat="server" BackColor="White" BorderColor="#DEDFDE" BorderWidth="1px" CellPadding="4" ForeColor="Black" Width="1000px" BorderStyle="None" GridLines="Vertical">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "列1" )%>'></asp:Label>
<asp:Label ID="Label3" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "列2" )%>'></asp:Label>
</ItemTemplate>
</asp:DataList>
<br /><div style="float:right;">
<asp:HyperLink ID="HyperLink1" runat="server">上一页</asp:HyperLink>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:HyperLink ID="HyperLink2" runat="server">下一页</asp:HyperLink>
后台:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
OleDbConnection objConn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" + System.Web.HttpContext.Current.Server.MapPath("数据库"));
OleDbDataAdapter objCommand = new OleDbDataAdapter("select * from 表 Where 列 like '%" + DropDownList1.SelectedValue + "%'", objConn);
DataSet ds = new DataSet();
objCommand.Fill(ds);
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds.Tables[0].DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 10;
int CurPage;
if (Request.QueryString["Page"] != null)
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage = 1;
objPds.CurrentPageIndex = CurPage - 1;
Label1.Text = "第" + CurPage.ToString() + "页";
if (!objPds.IsFirstPage)
{
HyperLink1.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
}
if (!objPds.IsLastPage)
{
HyperLink2.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
}
DataList1.DataSource = objPds;
DataList1.DataBind();
} 展开
前台:
<asp:DropDownList ID="DropDownList1" runat="server" Width="40px" onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True" >
</asp:DropDownList>
<asp:DataList ID="DataList1" runat="server" BackColor="White" BorderColor="#DEDFDE" BorderWidth="1px" CellPadding="4" ForeColor="Black" Width="1000px" BorderStyle="None" GridLines="Vertical">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "列1" )%>'></asp:Label>
<asp:Label ID="Label3" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "列2" )%>'></asp:Label>
</ItemTemplate>
</asp:DataList>
<br /><div style="float:right;">
<asp:HyperLink ID="HyperLink1" runat="server">上一页</asp:HyperLink>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:HyperLink ID="HyperLink2" runat="server">下一页</asp:HyperLink>
后台:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
OleDbConnection objConn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" + System.Web.HttpContext.Current.Server.MapPath("数据库"));
OleDbDataAdapter objCommand = new OleDbDataAdapter("select * from 表 Where 列 like '%" + DropDownList1.SelectedValue + "%'", objConn);
DataSet ds = new DataSet();
objCommand.Fill(ds);
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds.Tables[0].DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 10;
int CurPage;
if (Request.QueryString["Page"] != null)
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage = 1;
objPds.CurrentPageIndex = CurPage - 1;
Label1.Text = "第" + CurPage.ToString() + "页";
if (!objPds.IsFirstPage)
{
HyperLink1.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
}
if (!objPds.IsLastPage)
{
HyperLink2.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
}
DataList1.DataSource = objPds;
DataList1.DataBind();
} 展开
4个回答
展开全部
试着用一下 IsPostBack属性,,这样就可以只在当前页面第一次加载的时候 绑定DropDownList控件的显示数据了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以把dropdownlist里面的值存在viewstate里 或是 存在一个hidden控件中嘛
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不要每次回发都绑定
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询