就是我创建DataList的时候,我在前台是使用
<asp:Repeater id="Repeater1" runat="server"> <ItemTemplate> <table border="0" cellpadding="0" cellspacing="3"> <tr><td><%#RepalceString(DataBinder.Eval(Container.DataItem, "ST_newscontent").ToString())%></td>
</tr></table> </ItemTemplate> </asp:Repeater>
来绑定数据的
而<%#RepalceString(DataBinder.Eval(Container.DataItem, "ST_newscontent").ToString())%>的意思是绑定的内容来自st_newscontent这个字段.
然后RepalceString()这个函数是我自己在后台里写的,其它就有动态创建
DataList的语句.然而我创建出来DataList,他不能在我想要的地方显示,而在最下面显示出来
如何我的本意是:aaa
bbb
CCC
<动态创建DataList,长100px 高80px>
DDD
EEE
FFF
他出来的效果却是:
aaa
bbb
CCC
DDD
EEE
FFF
<动态创建DataList,长100px 高80px>
也就是说他把所有要加载的东东先加载,然后再来创建我的DataList,有什么办法解决没?
昨天跟我上司说了一下,他说可以在后台保存的时候先创建一个容器控件来保存(例如Table)
要创建DataList的地方,然后再在前台动态加载的时候把DataList加载到容器里面去.
可是当我在测试使用Table tab1 = (Table)this.Repeater1.FindControl("table1");
时发现tab1为空(注:table1为id值,这里我在后台保存时建立的)
所以也没能解决问题,能否告诉我一下怎么解决啊.
<asp:Repeater id="Repeater1" runat="server"> <ItemTemplate> <table border="0" cellpadding="0" cellspacing="3"> <tr><td><%#RepalceString(DataBinder.Eval(Container.DataItem, "ST_newscontent").ToString())%></td>
</tr></table> </ItemTemplate> </asp:Repeater>
来绑定数据的
而<%#RepalceString(DataBinder.Eval(Container.DataItem, "ST_newscontent").ToString())%>的意思是绑定的内容来自st_newscontent这个字段.
然后RepalceString()这个函数是我自己在后台里写的,其它就有动态创建
DataList的语句.然而我创建出来DataList,他不能在我想要的地方显示,而在最下面显示出来
如何我的本意是:aaa
bbb
CCC
<动态创建DataList,长100px 高80px>
DDD
EEE
FFF
他出来的效果却是:
aaa
bbb
CCC
DDD
EEE
FFF
<动态创建DataList,长100px 高80px>
也就是说他把所有要加载的东东先加载,然后再来创建我的DataList,有什么办法解决没?
昨天跟我上司说了一下,他说可以在后台保存的时候先创建一个容器控件来保存(例如Table)
要创建DataList的地方,然后再在前台动态加载的时候把DataList加载到容器里面去.
可是当我在测试使用Table tab1 = (Table)this.Repeater1.FindControl("table1");
时发现tab1为空(注:table1为id值,这里我在后台保存时建立的)
所以也没能解决问题,能否告诉我一下怎么解决啊.
然后 PlaceHolder.controls.add(repeater)
B.在后台先模拟DataList的地方放个Table或PlaceHolder或其它容器,然后在前台去找到Table或PlaceHolder,然后再用PlaceHolder.controls.add(DataList),然而就卡在这里,我不知道怎么去找到Table或PlaceHolder,我用Table tab1 = (Table)this.Repeater1.FindControl("table1");
居然报错,tab1为空,有哪位高手知道怎么在前台去找到这个像Table 或PlaceHolder的容器吗?
而且它是在某个ItemTemplate里,所以不能直接使用Table tab1 = (Table)this.Repeater1.FindControl("table1");之类
应该在DataBound的事件里写
{
ViewState["Count"] = 0;
string myStr = str.ToString();
MatchCollection mc = Regex.Matches(myStr, @"<table\s[^>]*(?<type>type=[^=]*=type)[^>]*>\s*(?<tbody><tbody[^>]*>[\s\S]*?</tbody>)?[\s\S]*?</table>", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
int count = (int)ViewState["Count"];
count++;
ViewState["Count"] = count;
string mTable;
mTable = m.Value.ToString(); CreateDataList(mTable, m.Groups["tbody"].Value, m.Groups["type"].Value, ViewState["Count"].ToString());
}
string strR = myStr;
return strR;
} public void CreateDataList(string strTable, string strBody, string strType, string num)
{
MyDataList d = new MyDataList();
d.ID = "dlstTest";
d.RepeatColumns = 1;
d.RepeatDirection = RepeatDirection.Horizontal;
d.ItemTemplate = new MyColumn("h1", 0);
d.ItemDataBound += new MyDataList.MyItemDataBoundEventHandler(d_ItemDataBound);
d.DataSource = db.GetDataView("select * from o_classContent where class_name='业界新闻'", "o_classContent");
d.strBindClass = "业界新闻";
d.strBindField = "[标题],[新闻日期]";
d.strHtml = strTable;
d.DataBind();
string[] strT = strType.Split('|');
this.Repeater1.Controls.Add(d); }
public class MyColumn : ITemplate
{
private string pControlName; //控件名称
private int pWidth; //控件宽度 public MyColumn(string ControlName, int Width)
{
pControlName = ControlName;
pWidth = Width;
} public void InstantiateIn(Control container)
{
HyperLink h = new HyperLink();
// Table h = new Table();
h.Width = pWidth;
h.ID = pControlName; container.Controls.Add(h);
}
}public class MyDataList : DataList
{
public string strHtml;
public string strBindClass;
public string strBindField;
public delegate void MyItemDataBoundEventHandler(object sender, DataListItemEventArgs e, string strHtml, string strBindClass, string strBindField); new public event MyItemDataBoundEventHandler ItemDataBound;
protected override void OnItemDataBound(DataListItemEventArgs e)
{
if (ItemDataBound != null)
{
ItemDataBound(this, e, strHtml, strBindClass, strBindField);
}
}
}
对了. private void d_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e, string strHtml, string strBindClass, string strBindField)
{
// Table h;
HyperLink h;
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
h = (HyperLink)e.Item.FindControl("h1");
// h = (Table)e.Item.FindControl("h1");
if (h != null)
{
// h.Caption = strHtml;
h.Text = strHtml;
string[] strB = strBindField.Split(',');
for (int i = 0; i < strB.Length; i++)
{
string Y = strB[i].Replace("[", "");
Y = Y.Replace("]", "");
// h.Caption = h.Caption.Replace(strB[i], ((DataRowView)e.Item.DataItem).Row["" + getToField(strBindClass,Y) + ""].ToString()); h.Text = h.Text.Replace(strB[i], ((DataRowView)e.Item.DataItem).Row["" + getToField(strBindClass, Y) + ""].ToString());
}
}
}
}
d.DataSource = db.GetDataView("select * from o_classContent where class_name='业界新闻'", "o_classContent");
d.strBindClass = "业界新闻";
d.strBindField = "[标题],[新闻日期]";
我就可以通过这里来绑定数据,即动态的创建好一个MyDataList即可.
{
MyDataList d = new MyDataList();
d.ID = "dlstTest";
d.RepeatColumns = 1;
d.RepeatDirection = RepeatDirection.Horizontal;
d.ItemTemplate = new MyColumn("h1", 0);
d.ItemDataBound += new MyDataList.MyItemDataBoundEventHandler(d_ItemDataBound);
d.DataSource = db.GetDataView("select * from o_classContent where class_name='业界新闻'", "o_classContent");
d.strBindClass = "业界新闻";
d.strBindField = "[标题],[新闻日期]";
d.strHtml = strTable;
d.DataBind();
string[] strT = strType.Split('|');
this.Repeater1.Controls.Add(d); }中有一个定位置的代码,没写上来
d.Style["POSITIN"] = "absolute;margin-top:" + strT[2] + ";margin-left:" + strT[3].Replace("=type", "");
看是不是可以在这里作文章?
时发现tab1为空(注:table1为id值,这里我在后台保存时建立的)
---------------------------------------------------------------
table1是不是在repeater中的重复项中?那么你需要先定位到某个重复项再根据服务端的id来findcontrol,相关的代码可能类似于
Table tab1 = (Table)this.Repeater1.Items[0].FindControl("table1");
</tr></table> 中的DataBinder.Eval(Container.DataItem, "ST_newscontent").ToString()
即数据库内的ST_newscontent这个字段里.
</tr></table>
-------------------------
如果服务端需要使用table作为控件容器
<table id="table1" runat=server>......
参数名: index 错误。这个我以前也用this.Repeater1.Controls.AddAt(1,d);试过,不过这样肯定是不行的,因为我根本无法知道他的index是几。
bbb
CCC
<动态创建DataList,长100px 高80px>
DDD
EEE
FFF你的aaa,bbb,ccc是什么?是数据库纪录的内容?是代表3条纪录还是代表一段文字?重要的是,你要理解HTML的显示机制,内容行数和精确位置是矛盾的。
用web服务器控件试试
<asp:Repeater id="Repeater1" runat="server"><ItemTemplate><table border="0" cellpadding="0" cellspacing="3"><tr><td><%#RepalceString(DataBinder.Eval(Container.DataItem, "ST_newscontent").ToString())%>
<asp:Literal id=x runat=server/>
</td>
</tr></table></ItemTemplate></asp:Repeater>
在绑定事件里进行判断是否加入内容不就行吗?也可以换GridView控件都可以嘛