Come posso aggiungere una classe css a un pannello di aggiornamento nel codice C# dietro il file di asp.netCome posso aggiungere una classe css a un updatepanel in ASP.Net?
risposta
Come hai visto il pannello di aggiornamento non ha una proprietà di classe css. Quindi dal momento che non può essere fatto direttamente, è necessario un lavoro in giro; ce ne sono due (afferrati da UpdatePanel and CSS) che possono ottenere il comportamento desiderato.
Uno è quello di circondare il pannello di aggiornamento con un div:
<div id="foo" style="visibility: hidden; position: absolute">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
</asp:UpdatePanel>
</div>
L'altro è di applicare un selettore css basato su id del pannello di aggiornamento:
<style type="text/css">
#<%=UpdatePanel1.ClientID%> {
visibility: hidden;
position: absolute;
}
</style>
Un altro modo non menzionato l'articolo è circondato dal pannello in un div e stile il pannello di aggiornamento basato su di esso rendering come div:
<style type="text/css">
#foo div {
visibility: hidden;
position: absolute;
}
</style>
<div id="foo">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
</asp:UpdatePanel>
</div>
Un pannello di aggiornamento può eseguire il rendering come div o span (in base alla modalità). Il modo più semplice per ottenere ciò che si desidera è di avvolgere l'UpdatePanel in un pannello standard:
<asp:Panel ID="Panel1" runat="Server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
</asp:UpdatePanel>
</asp:Panel>
La si può solo fare questo in codebehind:
Panel1.CssClass = "myCssClass";
Si potrebbe anche usare un div, come LFSR Consulting detto e aggiungere runat="server"
e quindi modificare l'attributo di classe. Ma Panel è un po 'più facile da lavorare (un pannello si presenta come un div).
è possibile utilizzare un'unica classe attributo HTML
<asp:UpdatePanel ID="UpdatePanel1" runat="server" class="MyCssClass">
</asp:UpdatePanel>
Questo funziona in .NET 4, ma il parser .NET precedente esplode quando si tenta di farlo con 'System.Web.HttpParseException: Digitare 'System.Web.UI.UpdatePanel' non ha una proprietà pubblica chiamata 'class'' – bdukes
Questo perché 'UpdatePanel' non implementa [' IAttributeAccessor'] (http://msdn.microsoft.com/it us/library/system.web.ui.iattributeaccessor.aspx) in .NET 3.5, ma fa in .NET 4 (quindi non puoi evento programmaticamente aggiungere il 'class' tramite la proprietà' Attributes', perché è stato aggiunto in .NET 4, pure). – bdukes
Si potrebbe anche fare come ho e basta creare una nuova classe che eredita l'UpdatePanel. Ho avuto la base per questo da qualche altra parte ma non ricordo dove, quindi non posso accreditarmi completamente ma l'ho solo perfezionato per questa idea. Sto per fare lo stesso per gli attributi HTML (poiché la collezione .attributes() è per css su UpdatePanel invece degli attributi HTML grezzi come con la maggior parte degli altri web.ui.controls).
AGGIORNAMENTO: Ho aggiornato per includere qualche altra personalizzazione Ho fatto che consentono di qualsiasi attributo da aggiungere. In realtà questo duplica la prima personalizzazione, tranne per il fatto che il primo crea un approccio abbastanza standard in cui questo è completamente flessibile (quindi non standard).
Imports System.ComponentModel
Imports System.Collections
Imports System.Web.UI
Namespace Controls
Public Class UpdatePanelCss
Inherits UpdatePanel
Private _cssClass As String
Private _tag As HtmlTextWriterTag = HtmlTextWriterTag.Div
Public HtmlAttributes As New HtmlAttributes
<DefaultValue("")> _
<Description("Applies a CSS style to the panel.")> _
Public Property CssClass() As String
Get
Return If(_cssClass, [String].Empty)
End Get
Set(ByVal value As String)
_cssClass = value
End Set
End Property
' Hide the base class's RenderMode property since we don't use it
<Browsable(False)> _
<EditorBrowsable(EditorBrowsableState.Never)> _
<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public Shadows Property RenderMode() As UpdatePanelRenderMode
Get
Return MyBase.RenderMode
End Get
Set(ByVal value As UpdatePanelRenderMode)
MyBase.RenderMode = value
End Set
End Property
<DefaultValue(HtmlTextWriterTag.Div)> _
<Description("The tag to render for the panel.")> _
Public Property Tag() As HtmlTextWriterTag
Get
Return _tag
End Get
Set(ByVal value As HtmlTextWriterTag)
_tag = value
End Set
End Property
Protected Overrides Sub RenderChildren(ByVal writer As HtmlTextWriter)
If IsInPartialRendering Then
' If the UpdatePanel is rendering in "partial" mode that means
' it's the top-level UpdatePanel in this part of the page, so
' it doesn't render its outer tag. We just delegate to the base
' class to do all the work.
MyBase.RenderChildren(writer)
Else
' If we're rendering in normal HTML mode we do all the new custom
' rendering. We then go render our children, which is what the
' normal control's behavior is.
writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID)
If CssClass.Length > 0 Then
writer.AddAttribute(HtmlTextWriterAttribute.[Class], CssClass)
End If
If HtmlAttributes.Count > 0 Then
For Each ha As HtmlAttribute In HtmlAttributes
writer.AddAttribute(ha.AttrName, ha.AttrVal)
Next
End If
writer.RenderBeginTag(Tag)
For Each child As Control In Controls
child.RenderControl(writer)
Next
writer.RenderEndTag()
End If
End Sub
End Class
Public Class HtmlAttribute
Private PAttrName As String
Private PAttrVal As String
Public Sub New(AttrName As String, AttrVal As String)
PAttrName = AttrName
PAttrVal = AttrVal
End Sub
Public Property AttrName() As String
Get
Return PAttrName
End Get
Set(value As String)
PAttrName = value
End Set
End Property
Public Property AttrVal() As String
Get
Return PAttrVal
End Get
Set(value As String)
PAttrVal = value
End Set
End Property
End Class
Public Class HtmlAttributes
Inherits CollectionBase
Public ReadOnly Property Count() As Integer
Get
Return List.Count
End Get
End Property
Default Public Property Item(ByVal index As Integer) As HtmlAttribute
Get
Return CType(List.Item(index), HtmlAttribute)
End Get
Set(ByVal Value As HtmlAttribute)
List.Item(index) = Value
End Set
End Property
Public Function Add(ByVal item As HtmlAttribute) As Integer
Return List.Add(item)
End Function
Public Sub Remove(ByVal index As Integer)
If index < List.Count AndAlso List.Item(index) IsNot Nothing Then
List.RemoveAt(index)
Else
Throw New Exception(String.Concat("Index(", index.ToString, ") is not valid. List only has ", List.Count.ToString, " items."))
End If
End Sub
Public Sub Remove(ByRef hAttribute As HtmlAttribute)
If List.Count > 0 AndAlso List.IndexOf(hAttribute) >= 0 Then
List.Remove(hAttribute)
Else
Throw New Exception("Object does not exist in collection.")
End If
End Sub
End Class
End Namespace
C# di conversione tramite http://www.developerfusion.com/:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.ComponentModel;
using System.Web.UI;
namespace Controls
{
public class UpdatePanelCss : UpdatePanel
{
private string _cssClass;
private HtmlTextWriterTag _tag = HtmlTextWriterTag.Div;
public HtmlAttributes HtmlAttributes = new HtmlAttributes();
[DefaultValue("")]
[Description("Applies a CSS style to the panel.")]
public string CssClass {
get { return _cssClass ?? String.Empty; }
set { _cssClass = value; }
}
// Hide the base class's RenderMode property since we don't use it
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new UpdatePanelRenderMode RenderMode {
get { return base.RenderMode; }
set { base.RenderMode = value; }
}
[DefaultValue(HtmlTextWriterTag.Div)]
[Description("The tag to render for the panel.")]
public HtmlTextWriterTag Tag {
get { return _tag; }
set { _tag = value; }
}
protected override void RenderChildren(HtmlTextWriter writer)
{
if (IsInPartialRendering) {
// If the UpdatePanel is rendering in "partial" mode that means
// it's the top-level UpdatePanel in this part of the page, so
// it doesn't render its outer tag. We just delegate to the base
// class to do all the work.
base.RenderChildren(writer);
} else {
// If we're rendering in normal HTML mode we do all the new custom
// rendering. We then go render our children, which is what the
// normal control's behavior is.
writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
if (CssClass.Length > 0) {
writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
}
if (HtmlAttributes.Count > 0) {
foreach (HtmlAttribute ha in HtmlAttributes) {
writer.AddAttribute(ha.AttrName, ha.AttrVal);
}
}
writer.RenderBeginTag(Tag);
foreach (Control child in Controls) {
child.RenderControl(writer);
}
writer.RenderEndTag();
}
}
}
public class HtmlAttribute
{
private string PAttrName;
private string PAttrVal;
public HtmlAttribute(string AttrName, string AttrVal)
{
PAttrName = AttrName;
PAttrVal = AttrVal;
}
public string AttrName {
get { return PAttrName; }
set { PAttrName = value; }
}
public string AttrVal {
get { return PAttrVal; }
set { PAttrVal = value; }
}
}
public class HtmlAttributes : CollectionBase
{
public int Count {
get { return List.Count; }
}
public HtmlAttribute this[int index] {
get { return (HtmlAttribute)List[index]; }
set { List[index] = value; }
}
public int Add(HtmlAttribute item)
{
return List.Add(item);
}
public void Remove(int index)
{
if (index < List.Count && List[index] != null) {
List.RemoveAt(index);
} else {
throw new Exception(string.Concat("Index(", index.ToString(), ") is not valid. List only has ", List.Count.ToString(), " items."));
}
}
public void Remove(ref HtmlAttribute hAttribute)
{
if (List.Count > 0 && List.IndexOf(hAttribute) >= 0) {
List.Remove(hAttribute);
} else {
throw new Exception("Object does not exist in collection.");
}
}
}
}
Sebbene questa risposta sia utile, la domanda è stata posta con il tag C#, non con VB.NET. –
Va tutto bene. Ho upvoted la tua risposta, non c'è bisogno di arrabbiarsi. –
Grazie Ed. Cancellando il mio ultimo. – rainabba
HTML
<asp:UpdatePanel ID="UpdatePanel1" runat="server"></asp:UpdatePanel>
CSS
<style type="text/css">
#UpdatePanel1 { position: relative; }
</style>
Si prega di spiegare il downvote. – krlzlx
CodeBehind:
UpdatePanel panel = new UpdatePanel();
panel.Attributes.Add("class","your-css-class");
Risultato HTML:
<div id="..." class="your-css-class"></div>
- 1. Come aggiungere una classe css a un pulsante z3c.form
- 2. Yii2 - Come aggiungere una classe CSS a un campo ActiveForm
- 3. Come si utilizza un UpdatePanel correttamente? (asp.net)
- 4. Aggiungere una classe CSS per un collegamento in RST
- 5. ASP.Net - Javascript all'interno dell'AJAX UpdatePanel
- 6. Come posso applicare una classe CSS a HtmlTableCell?
- 7. Come aggiungere una nuova classe css in @ Html.TextBox mvc4
- 8. HTML + CSS: Aggiungere una classe/id in testo semplice
- 9. Come aggiungere una classe a particolari li?
- 10. Wtforms, aggiungere dinamicamente una classe a un modulo
- 11. Come si applica una classe CSS a Html.ActionLink in ASP.NET MVC?
- 12. Come aggiungere dinamicamente le righe a una tabella in ASP.NET?
- 13. Come aggiungo una classe css a particolari righe in slickGrid?
- 14. Aggiunta di una classe CSS a date_select
- 15. Come aggiungere una classe a una cella in SlickGrid
- 16. soluzione migliore alternativa per ASP.NET AJAX UpdatePanel
- 17. Come aggiungere un attributo RowVersion a una classe EF4
- 18. Come posso aggiungere una variabile a console.log?
- 19. asp.net usercontrol non sparerà javascript all'interno updatepanel
- 20. jquery ui datepicker dentro asp.net UpdatePanel
- 21. Aggiungere un operatore negativo (?) A una classe in C#
- 22. aggiungere una funzione a una classe decorare
- 23. Importare una classe in ASP.NET
- 24. Aggiungi classe CSS a Html.ActionLink
- 25. Come posso aggiungere un ID univoco a una cella personalizzata?
- 26. Aggiungere una classe a un div con javascript
- 27. Come posso aggiungere una classe JavaFX 2.0 a un progetto Maven esistente?
- 28. Come rimuovere una classe CSS da un componente Wicket?
- 29. Come posso aggiungere una matrice a un plist utilizzando PlistBuddy?
- 30. Aggiungere una classe CSS a blocchi di codice singolo in RMarkdown
Forse avete bisogno di chiarire la tua domanda un po '....una classe (pura) non può essere aggiunta ad un updatepanel (controllo) si aggiungono controlli al pannello di aggiornamento – Jaime
Cosa intendi per "classe"? Classe Css? classe che eredita da System.Web.UI.Control? digitare con alcuni dati di cui si desidera tenere traccia? –
una classe css. il pannello di aggiornamento viene visualizzato come div in modo che possa essere assegnato a una classe css – ErnieStings