超高分求解,一个非常实用,但至今论坛中无人解决的问题,如果能解决肯定当之无愧的成为中国最熟悉DataList的人(在后台中自动生成DataList,
一个非常实用,但至今论坛中无人解决的问题,如果能解决肯定当之无愧的成为中国最熟悉DataList的人(在后台中自动生成DataList,并用后台进行绑定,注意不是前台绑定),已经有好多贴,可是都没解决,我这里再加100分求解
相关贴子有:
http://community.csdn.net/Expert/topic/5559/5559722.xml?temp=.7824823
http://community.csdn.net/Expert/topic/5558/5558033.xml?temp=.7784998
http://community.csdn.net/Expert/topic/5552/5552781.xml?temp=.7758142
[解决办法]
看着标题很长就进来了.
[解决办法]
一个个的看看链接,不会再悄悄地走开...
[解决办法]
回家先研究一下,完了再来告诉LZ!
[解决办法]
慢慢看再说
[解决办法]
MARK LOOK
[解决办法]
后台绑定一般通过ItemDataBound来处理
还没有见过这样绑定的
[解决办法]
你可以在后台New一个DataList,并绑定数据,但模板必须事先建好!
[解决办法]
先进来
[解决办法]
看看这个能不能满足你的基本要求
//aspx
<%@ Page Language= "C# " AutoEventWireup= "true " CodeBehind= "Default.aspx.cs " Inherits= "Default " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:Button ID= "Button1 " runat= "server " OnClick= "Button1_Click " Text= "Button " />
</div>
</form>
</body>
</html>
//aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default : System.Web.UI.Page
{
private DataView GetAuthors()
{
SqlConnection cn = new SqlConnection(@ "server=.\SQLExpress;uid=sa;pwd=password;database=pubs ");
SqlDataAdapter da = new SqlDataAdapter( "select * from authors ", cn);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();
return ds.Tables[0].DefaultView;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
DataList d = new DataList();
d.ID = "dlstTest ";
d.ItemTemplate = new MyColumn( "h1 ", 200);
d.ItemDataBound +=new DataListItemEventHandler(d_ItemDataBound);
d.DataSource = this.GetAuthors();
d.DataBind();
this.form1.Controls.Add(d);
}
private void d_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
HyperLink h;
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
h = (HyperLink)e.Item.FindControl( "h1 ");
if (h != null)
{
h.Text = ((DataRowView)e.Item.DataItem).Row[ "au_id "].ToString();
h.NavigateUrl = "Msg.aspx?id= " + h.Text;
}
}
}
}
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();
h.Width = pWidth;
h.ID = pControlName;
container.Controls.Add(h);
}
}
[解决办法]
上面3个链接中都已经这样和你讲了,
这么多高手让你用在ItemDataBound事件中去实现
lz你自己不去实现,还在问?
ls的连代码都帮你写了:
private void d_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
HyperLink h;
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
h = (HyperLink)e.Item.FindControl( "h1 ");
if (h != null)
{
h.Text = ((DataRowView)e.Item.DataItem).Row[ "au_id "].ToString();
h.NavigateUrl = "Msg.aspx?id= " + h.Text;
}
}
}
[解决办法]
你这个无非就是动态绑定DataList模板的问题,动态生成DataList并添加到页面楼主应该很容易办到,动态绑定模板需实现接口ITemplate
,你可以参考这个小例子
public class DatalistLabelColumn : ITemplate
{
public DatalistLabelColumn()
{
//Add constructor stuff here
}
public void InstantiateIn(Control container)
{
Label label1 = new Label();
label1.DataBinding += new EventHandler(this.BindLabelColumn);
container.Controls.Add(label1);
}
public void BindLabelColumn(object sender, EventArgs e)
{
Label lbl = (Label)sender;
DataListItem container = (DataListItem)lbl.NamingContainer ;
String strVals =Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem, "LastName "))
+ ", " +
Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem, "FirstName ")) ;
lbl.Text = strVals;
}
}
使用时如下:
DataSet ds = //Assign appropriate value;
DataList1.ItemTemplate = new DatalistLabelColumn();
DataList1.DataSource =ds;
DataList1.DataBind();
[解决办法]
虚荣心强的都进来了
[解决办法]
mark
[解决办法]
偶是来学习的...
[解决办法]
什么叫虚荣心强的都进来了
[解决办法]
学习来的
[解决办法]
学习
[解决办法]
来学习的
[解决办法]
MD 曾经有位牛人给我写过这样的代码,我自己也写了丁点,以为会了,就再也没弄过,楼主一问,日,发现忘记了,后悔没把代码备份下。。
[解决办法]
mark
再看
[解决办法]
动态生成datalist
Dim objDataGrid As New DataGrid
With objDataGrid
.ID = "dgComments "
.Width = System.Web.UI.WebControls.Unit.Parse( "100% ")
.ShowHeader = False
.AutoGenerateColumns = False
End With
Dim objtc As TemplateColumn
objtc = New TemplateColumn
Dim mecc As New MyEditCommentColumn
mecc.LocalResourceFile = Me.LocalResourceFile
objtc.ItemTemplate = mecc
objtc.HeaderText = " "
objDataGrid.Columns.Add(objtc)
objDataGrid.Columns(0).ItemStyle.Width = System.Web.UI.WebControls.Unit.Pixel(12)
objtc = New TemplateColumn
Dim mcc As New MyCommentColumn
mcc.LocalResourceFile = Me.LocalResourceFile
objtc.ItemTemplate = mcc
objtc.HeaderText = "Comments "
objDataGrid.Columns.Add(objtc)
AddHandler objDataGrid.ItemDataBound, AddressOf CommentGrid_ItemDataBound
objCommentsPanel.Controls.Add(objDataGrid)
objDataGrid = CType(objCommentsPanel.FindControl( "dgComments "), DataGrid)
objDataGrid.DataSource = repositoryComments.GetRepositoryComments(objRepository.ItemId, -1, -1)
objDataGrid.DataBind()
动态绑定
Private Sub CommentGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
Dim objcomment As RepositoryCommentInfo
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
objcomment = New RepositoryCommentInfo
objcomment = e.Item.DataItem
Dim objHyperLink As HyperLink = CType(e.Item.Cells(0).FindControl( "hypEdit "), HyperLink)
If Not objHyperLink Is Nothing Then
objHyperLink.NavigateUrl = EditUrl( "ItemID ", objcomment.ItemId.ToString(), "EditComment ")
If PortalSecurity.HasEditPermissions(m_RepositoryId) Or PortalSecurity.IsInRole(PortalSettings.AdministratorRoleName) Then
objHyperLink.Visible = True
Else
objHyperLink.Visible = False
End If
End If
End If
End Sub
很简单的,建议看一下dnn的代码或者community server代码,相关微软的代码都会有的。
[解决办法]
接分的
[解决办法]
我已经关注了两个星期了
[解决办法]
换行呢???
换行如何解决
根据字段自动换行
[解决办法]
换行?
上网去查,用css很容易控制
[解决办法]
只恨自己不是强人啊,不懂还是不懂.
[解决办法]
这样做的原因是?
[解决办法]
动态绑定有什么用啊?
[解决办法]
嗷嗷, 也关注吧
[解决办法]
占个座 ....
[解决办法]
可能我术水了吧,大家发的代码我看不懂啊。。。
[解决办法]
帽子太大...
不敢说话...
[解决办法]
[解决办法]
楼主还有什么疑问?这样不能满足你的要求么?
[解决办法]
我又见到了比较好的方法,一会回在后面..