Comment ajouter une balise colgroup au contrôle DataGrid pour que je puisse styliser chaque colonne en utilisant css?Comment ajouter une balise Colgroup à ASP: DataGrid Control?
Répondre
Je pensais que cela a été traité dans .NET 3.5, mais je ne trouve aucune référence. Quoi qu'il en soit, voici un contrôle serveur roulées à la main qui vous permet de spécifier 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>");
}
}
Utilisez comme ça ...
<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>
Source: Jeff Prosise's Blog
Comment ajouter un col par programmation? Par exemple, je voudrais faire quelque chose comme ColGroupGridView.ColGroup.Add (col) ou quelque chose de similaire – Arizona1911
Bien sûr. Faire un get/set public pour la variable _ColGroup. Ensuite, vous devriez être en mesure d'ajouter comme ceci ... 'Dim Col comme nouveau HtmlGenericControl (" COL ") Col.Attributes.Add (" classe "," myCssClass ") ColGroupGridView.ColGroup.Add (Col)' –
Pouvez-vous expliquer Qu'est-ce que ColGroupTemplate est et comment je suis supposé l'initialiser? Jusqu'à présent, j'ai ce ADDCOL public void (string colClass) { HtmlGenericControl col = new HtmlGenericControl ("COL"); col.Attributes.Add ("class", colClass); _ColGroup = new ColGroup(); _ColGroup.Controls.Add (col); ColGroupTemplate.InstantiateIn (_ColGroup); } La dernière ligne me donne une référence d'objet non définie sur une instance d'un objet car ColGroupTemplate est null. – Arizona1911
Que diriez-vous changement mineur afin d'ajouter des cols dynamiquement?
/// <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();
}
}
J'ai cherché depuis quelques jours une solution pour ajouter un template ColGroup à un GridView. En utilisant la réponse donnée par Josh Uphere, et quelques jours de lecture sur les objets dérivés, en particulier GridView, j'ai pensé qu'une solution mise à jour et efficace aiderait les gens.
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
Cela vous offre vraiment la possibilité de l'utiliser comme Josh sais:
<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>
si vous enroulez la classe dérivée dans un espace de noms et l'enregistrer dans le web.config comme suit:
<configuration>
<system.web>
<pages>
<controls>
<add tagPrefix="aspcust" namespace="CustomControls" />
</controls >
</pages >
</system.web>
</configuration>
Programatically, vous pouvez ajouter une nouvelle colonne dans colgroup à, disons, PreRender:
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
Cheers.
- 1. Colgroup dans ffox/chrome
- 2. jQuery - Comment ajouter une classe à une balise td
- 3. Silverlight DataGrid Control - Comment arrêter le tri sur une colonne?
- 4. Comment ajouter "popup edit control"
- 5. éditer une cellule dans un asp: datagrid
- 6. Comment ajouter une balise <td> à ListViewItem?
- 7. Comment ajouter une nouvelle ligne à wpfToolkit: DataGrid (en code)
- 8. Comment ajouter une zone de liste déroulante à un DataGrid?
- 9. export Datagrid pour exceler asp
- 10. Comment ajouter objet System.Drawing.Image à Asp: Contrôle de l'image
- 11. Comment ajouter une balise d'image à l'intérieur de l'attribut title d'une balise d'ancrage?
- 12. Ajouter une ligne dans Dojo DataGrid
- 13. Ajouter une balise personnalisée à la validation Visual Studio
- 14. ajouter dynamiquement des données à balise span
- 15. Ajouter TemplateColumn de CheckBoxes à DataGrid programme
- 16. Obtention d'une balise active dans un DataGrid
- 17. Ajouter une nouvelle ligne: ListView vs DataGrid
- 18. Comment ajouter une balise d'ancrage à une URL à partir de la saisie de texte
- 19. Ajouter une balise non-standard à DOM avec jQuery
- 20. ajouter une balise fermante avec jquery
- 21. comment ajouter une ligne DataGrid sur l'événement Clavier?
- 22. Comment ajouter une balise après un lien avec BeautifulSoup
- 23. Comment ajouter un bouton à un DataGrid de structure compacte?
- 24. Comment ajouter un attribut à la balise de titre
- 25. Silverlight DataGrid - Ajouter des colonnes à l'exécution
- 26. Comment ajouter un menu contextuel à wpf datagrid?
- 27. Comment ajouter manuellement des données à un DataGrid dans Silverlight
- 28. ajouter querystring à une propriété postbackurl de asp: bouton
- 29. ASP classique Ajouter des événements à Outlook
- 30. Comment transmettre une variable asp à une commande select?
Quelle version d'ASP.NET? –
J'utilise la version 4.0 – Arizona1911