如何将colgroup标签添加到datagrid控件中,以便我可以使用css设置每个列的样式?如何将Colgroup标签添加到ASP:Datagrid控件?
回答
我认为这是在.NET 3.5中解决,但我找不到任何参考。不管怎么说,这里是一个手卷服务器控件允许您指定colgroup
...
public class ColGroupGridView : GridView
{
private ColGroup _ColGroup = null;
private ITemplate _ColGroupTemplate = null;
[TemplateContainer(typeof(ColGroup))]
public virtual ITemplate ColGroupTemplate
{
get { return _ColGroupTemplate; }
set { _ColGroupTemplate = value; }
}
protected override void CreateChildControls()
{
base.CreateChildControls();
_ColGroup = new ColGroup();
ColGroupTemplate.InstantiateIn(_ColGroup);
}
protected override void Render(HtmlTextWriter writer)
{
// Get the base class's output
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
base.Render(htw);
string output = sw.ToString();
htw.Close();
sw.Close();
// Insert a <COLGROUP> element into the output
int pos = output.IndexOf("<tr");
if (pos != -1 && _ColGroup != null)
{
sw = new StringWriter();
htw = new HtmlTextWriter(sw);
_ColGroup.RenderPrivate(htw);
output = output.Insert(pos, sw.ToString());
htw.Close();
sw.Close();
}
// Output the modified markup
writer.Write(output);
}
}
internal class ColGroup : WebControl, INamingContainer
{
internal void RenderPrivate(HtmlTextWriter writer)
{
writer.Write("<colgroup>");
base.RenderContents(writer);
writer.Write("</colgroup>");
}
}
使用它像这样...
<custom:ColGroupGridView ... runat="server">
<ColGroupTemplate>
<col class="itemid" />
<col class="cover-image" />
<col class="title" />
<col class="number" />
<col class="year" />
<col class="rating" />
<col class="cgc-rating" />
<col class="description" />
</ColGroupTemplate>
<!-- Rest of stuff here... -->
</custom:ColGroupGridView>
如何以编程方式添加col? 例如我想要做类似 ColGroupGridView.ColGroup.Add(col)或类似的东西 – Arizona1911 2010-06-29 20:30:19
当然。为_ColGroup变量进行公开获取/设置。那么你应该可以像这样添加......'Dim Col As New HtmlGenericControl(“COL”)Col.Attributes.Add(“class”,“myCssClass”)ColGroupGridView.ColGroup.Add(Col)' – 2010-06-29 20:45:36
你能解释一下吗? ColGroupTemplate是什么以及我应该如何初始化它? 到目前为止,我有这个 public void AddCol(string colClass) HtmlGenericControl col = new HtmlGenericControl(“COL”); col.Attributes.Add(“class”,colClass); _ColGroup = new ColGroup(); _ColGroup.Controls.Add(col); ColGroupTemplate.InstantiateIn(_ColGroup); } 最后一行给我一个对象引用未设置为对象的实例 ,因为ColGroupTemplate为null。 – Arizona1911 2010-06-29 21:22:00
怎么样为了动态添加col而做小的改动?
/// <summary>
/// Represents a Col element.
/// </summary>
public class HtmlCol : HtmlGenericControl
{
/// <summary>
/// Default constructor.
/// </summary>
public HtmlCol()
: base("col")
{
}
}
/// <summary>
/// Collection of HtmlCols
/// </summary>
public class HtmlCols : List<HtmlCol>
{
/// <summary>
/// Default constructor.
/// </summary>
public HtmlCols()
{
}
/// <summary>
/// Adds a col to the collection.
/// </summary>
/// <returns></returns>
public HtmlCol Add()
{
var c = new HtmlCol();
base.Add(c);
return c;
}
}
/// <summary>
/// Represents a colgrpup tag element.
/// </summary>
internal class ColGroup : WebControl, INamingContainer
{
internal void RenderPrivate(HtmlTextWriter writer)
{
writer.Write("<colgroup>");
base.RenderContents(writer);
writer.Write("</colgroup>");
}
internal void RenderPrivate(HtmlTextWriter writer, HtmlCols cols)
{
writer.Write("<colgroup>");
base.RenderContents(writer);
foreach (var c in cols)
{
c.RenderControl(writer);
}
writer.Write("</colgroup>");
}
}
[ToolboxData("<{0}:PlainGrid runat=server></{0}:Table>")]
public class PlainGrid : GridView
{
private ColGroup _colGroup = null;
private ITemplate _colGroupTemplate = null;
private HtmlCols _cols = null;
[TemplateContainer(typeof(ColGroup))]
public virtual ITemplate ColGroupTemplate
{
get { return _colGroupTemplate; }
set { _colGroupTemplate = value; }
}
protected override void CreateChildControls()
{
base.CreateChildControls();
if (ColGroupTemplate != null)
{
ColGroupTemplate.InstantiateIn(_colGroup);
}
}
protected override void Render(HtmlTextWriter writer)
{
// Get the base class's output
var sw = new StringWriter();
var htw = new HtmlTextWriter(sw);
base.Render(htw);
string output = sw.ToString();
htw.Close();
sw.Close();
// Insert a <COLGROUP> element into the output
int pos = output.IndexOf("<tr");
if (pos != -1 && _colGroup != null)
{
sw = new StringWriter();
htw = new HtmlTextWriter(sw);
_colGroup.RenderPrivate(htw, _cols);
output = output.Insert(pos, sw.ToString());
htw.Close();
sw.Close();
}
// Output the modified markup
writer.Write(output);
}
/// <summary>
/// Gets/Sets col items.
/// </summary>
public HtmlCols Cols
{
get { return _cols; }
set { _cols = value; }
}
/// <summary>
/// Default constructor
/// </summary>
public PlainGrid()
{
base.AutoGenerateColumns = false;
_colGroup = new ColGroup();
_cols = new HtmlCols();
}
}
我已经搜索了一些关于如何将ColGroup模板添加到GridView的解决方案。使用Josh提供的答案以及几天的关于派生对象(特别是GridView)的阅读,我认为一个更新的工作解决方案可以帮助周围的人。
Imports System.ComponentModel
Imports System.Web.UI
Imports Microsoft.VisualBasic
Imports System.IO
Namespace CustomControls
<ToolboxData("<{0}:GridViewColGroup runat=server></{0}:GridViewColGroup>")> _
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust"), _
ParseChildren(True)> _
Public Class GridViewColGroup
Inherits GridView
Implements INamingContainer
Private _ColGroup As ColGroup = Nothing
Private _ColGroupTemplate As ITemplate = Nothing
<Browsable(False), _
Description("The ColGroup template."), _
TemplateContainer(GetType(ColGroup)), _
PersistenceMode(PersistenceMode.InnerProperty)> _
Public Overridable Property ColGroupTemplate() As ITemplate
Get
Return _ColGroupTemplate
End Get
Set(ByVal value As ITemplate)
_ColGroupTemplate = value
End Set
End Property
<Browsable(False), _
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public Property ColGroup() As ColGroup
Get
Me.EnsureChildControls()
Return _ColGroup
End Get
Set(ByVal value As ColGroup)
_ColGroup = value
End Set
End Property
Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
_ColGroup = New ColGroup()
If Not ColGroupTemplate Is Nothing Then
ColGroupTemplate.InstantiateIn(_ColGroup)
End If
Me.Controls.Add(_ColGroup)
End Sub
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
EnsureChildControls()
' Get the base class's output
Dim sw As New StringWriter()
Dim htw As New HtmlTextWriter(sw)
MyBase.Render(htw)
Dim output As String = sw.ToString()
htw.Close()
sw.Close()
' Insert a <COLGROUP> element into the output
Dim pos As Integer = output.IndexOf("<tr")
If pos <> -1 AndAlso _ColGroup IsNot Nothing Then
sw = New StringWriter()
htw = New HtmlTextWriter(sw)
_ColGroup.RenderPrivate(htw)
output = output.Insert(pos, sw.ToString())
htw.Close()
sw.Close()
End If
' Output the modified markup
writer.Write(output)
End Sub
End Class
<ToolboxItem(False)> _
Public Class ColGroup
Inherits WebControl
Implements INamingContainer
Friend Sub RenderPrivate(ByVal writer As HtmlTextWriter)
writer.Write("<colgroup>")
MyBase.RenderContents(writer)
writer.Write("</colgroup>")
End Sub
End Class
End Namespace
这确实为您提供了使用它作为乔希最高审计机关的机会:
<aspcust:GridViewColGroup runat="server" ID="gridName">
<ColGroupTemplate>
<col class="some_class_1" />
<col class="some_class_2" />
...
<col class="some_class_n" />
</ColGroupTemplate>
<Columns>
...
</Columns>
</aspcust:GridViewColGroup>
如果您缠绕派生类到一个命名空间,并且在web.config中注册,如下所示:
<configuration>
<system.web>
<pages>
<controls>
<add tagPrefix="aspcust" namespace="CustomControls" />
</controls >
</pages >
</system.web>
</configuration>
编程方式,您可以添加新列到COLGROUP,让我们说,预渲染:
Protected Sub gridName_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
Dim grd As CustomControls.GridViewColGroup = sender
Dim wctrl As New WebControl(HtmlTextWriterTag.Col)
wctrl.CssClass = "some_class_m"
grd.ColGroup.Controls.Add(wctrl)
End Sub
干杯。
- 1. 将文字添加到标签控件
- 2. 将“标签”控件添加到“RichTextBox”
- 3. 将鼠标事件添加到colgroup或col
- 4. 如何以编程方式将PictureBox控件添加到标签页控件
- 5. 的标签添加到WPF ComboBox控件
- 6. 如何将标签添加到绘图
- 7. 如何将标签添加到THREE.MESH?
- 8. 如何将图像添加到标签?
- 9. 如何将标签添加到SlidingTabLayout?
- 10. 如何将新标签添加到JTidy?
- 11. 如何将标签添加到情节
- 12. 如何将actionlistener添加到JRadioButton标签?
- 13. 将图标添加到标签控件中的文本字段
- 14. 将asp.net控件标签添加到XML模式文件
- 15. 如何添加标签到MP4文件?
- 16. OnClick将标签添加到标签
- 17. 如何将标签添加到小部件屏幕图标?
- 18. 将标签添加到treenode
- 19. 将标签添加到filled.contour
- 20. 将标签添加到menustrip
- 21. 将标签添加到小部件
- 22. 如何将标签添加到没有标签的解析树?
- 23. knp菜单包 - 如何将标签添加到标签?
- 24. 如何将span标签添加到Drupal webform元素标签中?
- 25. 如何将填充添加到标签标签?
- 26. 将用户控件添加到vb.net标签页后,Tabpage为空
- 27. 将用户控件添加到标签页
- 28. 将控件添加到TabControl中的特定标签页Win32
- 29. 将标题添加到现有控件
- 30. 如何以编程方式将标签控件添加到Web表单中?
什么版本的ASP.NET? – 2010-06-29 19:31:29
我正在使用4.0版 – Arizona1911 2010-06-29 19:33:55