我在做一个查找新闻的板块,有三个单选按钮,一个按标题搜索,一个按内容搜索,一个按关键字搜索,这个是搜索的按钮事件:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if (this.rdbtnTitle.Checked)
{
string title = this.TextBox1.Text;
// Session["title"] = title;
Response.Redirect("SearchNews.aspx?title="+title);
}
else if (this.rdbtnCon.Checked)
{
Response.Redirect("SearchNews.aspx?content="+this.TextBox1.Text);
}
else
{
Response.Redirect("SearchNews.aspx?key="+this.TextBox1.Text);
}
}
在SearchNews.aspx的.CS代码:
public partial class _Default : System.Web.UI.Page
{
DataSet ds;
DataSet ds1;
DataSet ds2;
protected void Page_Load(object sender, EventArgs e)
{
//Response.Write(Request.QueryString["key"]);
if(!IsPostBack)
bind();
}
public void bind()
{
NewManager nm = new NewManager();
string title = Request.QueryString["title"];
ds = nm.getTitle(title);
//ds = nm.getKey(title);
this.DataList1.DataSource = ds;
this.DataList1.DataBind(); string content = Request.QueryString["content"];
ds1 = nm.getContent(content);
this.DataList1.DataSource = ds1;
this.DataList1.DataBind(); string key = Request.QueryString["key"];
ds2 = nm.getKey(key);
this.DataList1.DataSource = ds2;
this.DataList1.DataBind(); }
}
如果我把后边的8行注释之后,不会出错,会显示结果,注释错了,就会报错。不想用三个页面显示新闻。有没有办法解决啊?我是菜鸟,请大家帮帮忙!谢谢!@!
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if (this.rdbtnTitle.Checked)
{
string title = this.TextBox1.Text;
// Session["title"] = title;
Response.Redirect("SearchNews.aspx?title="+title);
}
else if (this.rdbtnCon.Checked)
{
Response.Redirect("SearchNews.aspx?content="+this.TextBox1.Text);
}
else
{
Response.Redirect("SearchNews.aspx?key="+this.TextBox1.Text);
}
}
在SearchNews.aspx的.CS代码:
public partial class _Default : System.Web.UI.Page
{
DataSet ds;
DataSet ds1;
DataSet ds2;
protected void Page_Load(object sender, EventArgs e)
{
//Response.Write(Request.QueryString["key"]);
if(!IsPostBack)
bind();
}
public void bind()
{
NewManager nm = new NewManager();
string title = Request.QueryString["title"];
ds = nm.getTitle(title);
//ds = nm.getKey(title);
this.DataList1.DataSource = ds;
this.DataList1.DataBind(); string content = Request.QueryString["content"];
ds1 = nm.getContent(content);
this.DataList1.DataSource = ds1;
this.DataList1.DataBind(); string key = Request.QueryString["key"];
ds2 = nm.getKey(key);
this.DataList1.DataSource = ds2;
this.DataList1.DataBind(); }
}
如果我把后边的8行注释之后,不会出错,会显示结果,注释错了,就会报错。不想用三个页面显示新闻。有没有办法解决啊?我是菜鸟,请大家帮帮忙!谢谢!@!
如果不为空,才进行DataBind
你说的这种情况是因为你只拿了第一个按钮做测试.
如果拿内容搜索,关键字搜索 一样会出错...因为此时Request.QueryString["title"]为空
repeater的,跟你的有点类似
报什么错呀?
string title = Request.QueryString["title"];
if(title!="")
{
ds = nm.getTitle(title);
//ds = nm.getKey(title);
this.DataList1.DataSource = ds;
this.DataList1.DataBind();
}