DataList DataBind错误 [英] DataList DataBind error

查看:97
本文介绍了DataList DataBind错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

任何帮助将不胜感激.

我创建了一个页面,它将在数据列表上显示搜索结果.在分别在页面Load和BtnSearch_Click中添加以下行之前,此页面运行良好.

Hello,

Any help will be much appreciated.

I have created a page tht will show search results on a datalist. this page was running perfectly before the addition of following lines in the page Load and BtnSearch_Click respectively.

if (Request.QueryString["query"] != null)
{
string searchParam = Request.QueryString["query"].ToString();
BindList(searchParam);
}

string qryString = Url; //Request.Url.ToString();
qryString += "?query=" + TxtSearch.Text.Trim();
Response.Redirect(qryString);
}



现在问题出在lstSearch.DataBind();.它给出了异常字符串值不能为null.参数名称:旧值".我搜索了该错误,但是大多数遇到此问题的人都是那些升级到VS较新版本的人,而我却不是这种情况..

然后我用Repeater替换了DataList.它完美地绑定了数据,但始终选择零索引(如以下几行所示),因此在ItemDataBound事件的文本替换中,我再次遇到了相同的异常.



Now the problem is at lstSearch.DataBind();. It gives exception "String Value can not be null. Parameter name:Old value". I googled the error but most of the ppl who encountered this problem were those who upgraded to newer versions of VS and thts not the case with me..

then I replaced the DataList with Repeater. It binds the data perfectly but it always picks zero index (shown in the following lines) , due to which I again the same exception at text replacement at the ItemDataBound event.

int index = _strContent.IndexOf(TxtSearch.Text, StringComparison.OrdinalIgnoreCase);



//借助该索引查找子字符串



//Find substring with the help of that Index

string sub_strContent = _strContent.Substring(index, TxtSearch.Text.Length);



//突出显示子字符串



//Highlight the substring

string _strHighlightText = "<span class=\"highlight\">" + sub_strContent + "</span>";



//用突出显示的文本替换文本



//Replace the text with highlighted text

_strContent = _strContent.Replace(sub_strContent, _strHighlightText);



以下是完整的代码.请注意,我还向我的.aspx页面添加了pagin.



Below is the complete code.Note that I have also added pagin to my .aspx page.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace abc

{

public partial class search_result : System.Web.UI.Page

{

PagedDataSource pageSource;

//''currentpage'' is used to keep track of the pages binded to the DataList

public static int currentpage = 0;

string Url ="search-result.aspx";



protected void Page_Load(object sender, EventArgs e)

{

lbtnNext1.Visible = false;

lbtnPrev1.Visible = false;

if (!Page.IsPostBack)

{

lblResults1.Visible = false;

lblResults2.Visible = false;

lblResults3.Visible = false;

if (Request.QueryString["query"] != null)
{
string searchParam = Request.QueryString["query"].ToString();
BindList(searchParam);
}

}

}

protected void BtnSearch_Click(object sender, EventArgs e)
{
string qryString = Url; //Request.Url.ToString();
qryString += "?query=" + TxtSearch.Text.Trim();
Response.Redirect(qryString);
//if (TxtSearch.Text != null && TxtSearch.Text != string.Empty)
//{ BindList(); }
//else { }

}

protected void Item_Click(object sender, DataListCommandEventArgs e)
{
Response.Redirect(e.CommandName);
}

protected void lbtnPrev_Click(object sender, EventArgs e)
{
// currentpage -= 1;
//BindList();

}

protected void lbtnNext_Click(object sender, EventArgs e)
{
//currentpage += 1;
//BindList();
}

public void BindList(string search)
{
try
{
BLL.Admin adminobj = new BLL.Admin(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
DataSet ds = adminobj.Search(search); 
int count = ds.Tables[0].Rows.Count;



//Bind data to the DataList if matching results are found
if (count != 0)
{
lblResults1.Visible = true;
lblResults2.Visible = true;
lblResults3.Visible = false;

if (count == 1)
{
lblResults1.Text = count + " Result for ";
lblResults2.Text = TxtSearch.Text;
}

else
{
lblResults1.Text = count + " Results for ";
lblResults2.Text = TxtSearch.Text;
}

pageSource = new PagedDataSource();
pageSource.AllowPaging = true;
pageSource.DataSource = ds.Tables[0].DefaultView;



// Set the number of results to be displayed below.
//e.g if you want to display 10 results per page, replace 5 by 10.
int a = 5;
pageSource.PageSize = a;
pageSource.CurrentPageIndex = currentpage;



// Disable the linkbuttons if it is first or last page.
lbtnNext1.Enabled = !pageSource.IsLastPage;
lbtnPrev1.Enabled = !pageSource.IsFirstPage;

//Set the datasource for DataList
lstSearch.DataSource = ds.Tables[0].DefaultView;
lstSearch.DataSource = pageSource;
lstSearch.DataBind(); //gives exception here.

// Keep the LinkButtons invisible if the returned rows are less than or equal to 5
if (count > a)
{
lbtnNext1.Visible = true;
lbtnPrev1.Visible = true;
}

}

else
{

//Display a message if the search doesnot match any results

lblResults1.Visible = true;
lblResults2.Visible = true;
lblResults3.Visible = true;
lblResults1.Text = "No Results found for ";
lblResults2.Text = TxtSearch.Text;
lblResults3.Text= ". Make sure all words are spelled correctly.";
lstSearch.DataSource = pageSource;
lstSearch.DataBind();

}

} 

catch (Exception ex)

{ Console.Write(ex.Message); }

}

protected void lstSearch_ItemDataBound(object sender, DataListItemEventArgs e)

{

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

{

Label lblContent = (Label)e.Item.FindControl("lblContent");

string _strContent = lblContent.Text;

//Get the index of entered word(s)

int index = _strContent.IndexOf(TxtSearch.Text, StringComparison.OrdinalIgnoreCase);

//Find substring with the help of that Index

string sub_strContent = _strContent.Substring(index, TxtSearch.Text.Length);

//Highlight the substring

string _strHighlightText = "<span class=\"highlight\">" + sub_strContent + "</span>";

//Replace the text with highlighted text

_strContent = _strContent.Replace(sub_strContent, _strHighlightText);

lblContent.Text = _strContent;

}

} 

}

}

推荐答案

调试时,
1)检查您获得的searchParam
While debugging,
1)check the searchParam that you are getting
string searchParam = Request.QueryString["query"].ToString();
BindList(searchParam);
}



2)只需检查是否在pageSource中得到了期望的结果.



2) just check if in the pageSource you are getting the desired result.

//Set the datasource for DataList

lstSearch.DataSource = ds.Tables[0].DefaultView;

lstSearch.DataSource = pageSource; // check here

lstSearch.DataBind(); //gives exception here.



并告诉结果



and tell the result


是的,我已经对其进行了多次调试,并且确实在数据集以及PageSource中获得了表.
是的,SearchParam确实包含用户在页面上输入的参数.
Yes I have debugged it many times, and I do get the table in the dataset as well as PageSource.
and yeah SearchParam does contain the parameter entered on the page by user.


您好,

替换您的代码:
Hi,

Replace you code:
if (Request.QueryString["query"] != null)
{
    string searchParam = Request.QueryString["query"].ToString();
    BindList(searchParam);
}


与:


with:

if (Request.QueryString["query"] != null)
{
    string Url ="search-result.aspx?";
    string searchParam = Request.QueryString["query"].ToString();
    BindList( Url + searchParam);
 
}


这篇关于DataList DataBind错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆