往DropDownList 里面 插入一个处于0位置的item,并且他的值是 0
插入后显示如下图所示。
我们在新闻管理页面,有用到 ajax 来实现 删除的 无动态刷新,那么对于分页和查询,能ajax么? 答案是可以的
只需要我们把所有的方法,全部都放到
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate>
中间即可
也就是 控件中间即可实现 ajax 的无刷新 查询/分页(但是如果分页已经设置为 url分页,则这个ajax 无效,分页的时候,url还是会加入page=2这类的)
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; 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; using System.Xml.Linq; using BLL; using DAL; using Model; using System.Data.SqlClient; public partial class admin_newsmanager : System.Web.UI.Page { NewsManager nm = new NewsManager(); protected void Page_Load(object sender, EventArgs e) { if (Session["admin"] != null && Session["admin"].ToString() == "joey") { if (!Page.IsPostBack) { ddlCategory.DataTextField = "name"; ddlCategory.DataValueField = "id"; ddlCategory.DataSource = new CategoryDAO().SelectAll(); ddlCategory.DataBind(); //ddlCategory.Items.Insert(0, "所有分类"); ddlCategory.Items.Insert(0, new ListItem("所有分类", "0")); } BindNews(); } else { Response.Redirect("login.aspx"); } } /// <summary>绑定新闻列表 /// /// </summary> private void BindNews() { string key = txtKey.Text.Trim(); int pagesize = anp.PageSize; int pageindex = anp.CurrentPageIndex; string cond = ""; cond = " title like '%" + key + "%' "; string cateid = ddlCategory.SelectedValue;//默认为0的是新闻类别是 选择全部 if (cateid != "0") //如果不等于0 说明选择的类别不为空 { cond += " and caid = " + cateid; } anp.RecordCount = nm.CalcCount(cond); repNews.DataSource = nm.Select(pagesize, pageindex, cond); //repNews.DataSource = new NewsManager().SelectAll(); repNews.DataBind(); } protected void lbtnDelNews_Click(object sender, EventArgs e) { LinkButton lb = (LinkButton)sender; string newsid = lb.CommandArgument; bool b = new NewsManager().Delete(newsid); if (b) //如果删除成功 { BindNews(); } else { Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('删除失败!');</script>"); } } protected void anp_PageChanged(object sender, EventArgs e) { BindNews(); } protected void btnSearch_Click(object sender, EventArgs e) { BindNews(); } }