如何添加一個css類到c#代碼後面的c#代碼中的更新面板如何添加一個css類到ASP.Net的更新面板?
回答
正如你所看到的更新面板沒有CSS類屬性。所以既然不能直接完成,你需要一個解決方法;有兩個(從UpdatePanel and CSS抓取),可以得到你想要的行爲。
一是圍繞更新面板採用了DIV:
<div id="foo" style="visibility: hidden; position: absolute">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
</asp:UpdatePanel>
</div>
另一種是應用基於更新面板ID的CSS選擇:
<style type="text/css">
#<%=UpdatePanel1.ClientID%> {
visibility: hidden;
position: absolute;
}
</style>
另一種方式中未提及該文章圍繞面板中的一個div和樣式更新面板基於它呈現爲一個div:
<style type="text/css">
#foo div {
visibility: hidden;
position: absolute;
}
</style>
<div id="foo">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
</asp:UpdatePanel>
</div>
更新面板可以呈現爲div或跨度(取決於模式)。達到你想要的最簡單方法是包裹在UpdatePanel標準面板:
<asp:Panel ID="Panel1" runat="Server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
</asp:UpdatePanel>
</asp:Panel>
的你可以做到這一點的代碼隱藏:
Panel1.CssClass = "myCssClass";
你也可以使用一個div,像LFSR諮詢說,並添加runat="server"
,然後更改類屬性。但面板更容易處理(面板只是呈現爲div)。
你可以使用一個類HTML屬性
<asp:UpdatePanel ID="UpdatePanel1" runat="server" class="MyCssClass">
</asp:UpdatePanel>
這在.NET 4中起作用,但是當您嘗試使用System.Web.HttpParseException執行此操作時,早期的.NET分析器爆炸:類型'System.Web.UI.UpdatePanel'沒有名爲'class'`的公共屬性 – bdukes 2012-01-30 16:45:27
你也可以做,因爲我已經和剛剛創建一個繼承UpdatePanel的一個新的類。我得到了這個地方的基礎,但我不記得在哪裏,所以我不能完全信任,但我只是調整了這個想法。我將爲HTML屬性執行相同的操作(因爲.attributes()集合用於UpdatePanel上的css而不是像大多數其他web.ui.controls一樣是原始HTML屬性)。
更新:我已經更新,包括一些其他的自定義,我已經允許添加任何屬性。真的這複製了第一個定製,除了第一個創建一個相當標準的方法,這是完全靈活的(因此不標準)。經由http://www.developerfusion.com/
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#轉換:
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.");
}
}
}
}
HTML
<asp:UpdatePanel ID="UpdatePanel1" runat="server"></asp:UpdatePanel>
CSS
<style type="text/css">
#UpdatePanel1 { position: relative; }
</style>
代碼隱藏:
UpdatePanel panel = new UpdatePanel();
panel.Attributes.Add("class","your-css-class");
HTML結果:
<div id="..." class="your-css-class"></div>
- 1. 在代碼中CSS類添加到一個面板後面
- 2. Asp.Net更新從另一個更新面板的面板
- 3. 如何從管理面板添加css類到woocommerce類別?
- 4. 在asp.net網站的更新面板中添加html到div?
- 5. asp.net將行添加到更新面板中的表
- 6. asp.net多個更新面板
- 7. 另一個更新面板問題ASP.NET
- 8. Asp.net更新面板
- 9. 向新面板添加一個grapic.drawline
- 10. ASP.NET刷新更新面板
- 11. 如何使用更新面板在asp.net
- 12. 更新在面板中添加的UserControl
- 13. CSS類添加到ASP.NET的ActionLink的MVC
- 14. 如何添加一個CSS類到一個div?
- 15. 我如何添加新的面板在運行時在asp.net
- 16. 一個更新面板與多個更新面板
- 17. 如何添加一個CSS類到jsGrid的最後一行
- 18. 如何將兩個寬度添加到CSS中的一個類
- 19. 如何將圖像「打印」到ASP.NET更新面板頁面?
- 20. 添加一個usercontrol到面板
- 21. C#添加一個圖片到面板
- 22. 如何使用ActionListener在同一個Applet中添加新面板
- 23. 我如何添加一個CSS文件到頁面的正文?
- 24. 我如何在面板類添加到我的框架主類
- 25. asp.net ajax更新面板
- 26. ASP.NET和更新面板
- 27. ASP.NET更新面板問題
- 28. ASP.Net Listview&AJAX更新面板
- 29. 更新ASP.NET面板異步
- 30. ASP.Net更新面板超時
也許你需要澄清你的問題有點....一個(純)類不能添加到你添加控件到更新面板的updatepanel(控件) – Jaime 2009-07-29 19:09:08
你是什麼意思的「類」? Css類?從System.Web.UI.Control繼承的類?鍵入一些你想跟蹤的數據? – 2009-07-29 19:10:01
一個css類。 更新面板呈現爲一個div,因此它應該能夠被分配一個css類 – ErnieStings 2009-07-29 19:16:30