I am looking to create an adapter for a single control, in this case a radiobutton, so that I can override one of its attributes. I am looking to override the output of the GroupName property so that I can use the radiobutton in gridviews and actually have
it function as intended.
Because my page is using a master page, any radiobutton I place in my gridview is renamed with a unique name and so do not function in a group. If i give my radiobutton a GroupName of "myGroup" it is renamed "ctl00$cphMain$Wizard1$gvPackages$ctl02$myGroup"
which is not very helpful.
I am new to Control Adapters and so far I have looked through the example files to try and work out the best approach.
Is there a way to obtain the html that would be rendered by the standard control and then modify it by replacing part of the html string or am I stuck with writing all the html output myself to completely replace the rendering of the control? ( I tried calling
Control.Render but obviously this dissapears into an infinite loop.) Am I going to have to inspect every property of the Control and rewrite the html in the RenderContent function?
Sorry for the basic questions but all the examples are of complex compound controls and I can't find an example of just rendering a single simple control.
Below is the code as I have it so far. This seems to render the radiobutton ok but state of the control is not persisted in the viewstate. Should this maintain state itself or am I going to have to manually add some code to maintain viewstate as well?
Namespace CSSFriendly
Public Class RadioButtonAdapter
Inherits System.Web.UI.WebControls.Adapters.WebControlAdapter
Protected Overrides Sub RenderContents(ByVal writer
As System.Web.UI.HtmlTextWriter)
Dim rb
As RadioButton =
CType(Control, RadioButton)
If Not IsNothing(rb)
AndAlso rb.Visible
Then
writer.WriteBeginTag("input")
writer.WriteAttribute("type",
"radio")
writer.WriteAttribute("id", rb.UniqueID)
writer.WriteAttribute("name", rb.GroupName)
writer.WriteAttribute("value", rb.ID)
If rb.CssClass <>
String.Empty Then
writer.WriteAttribute("class", rb.CssClass)
End If
If rb.Checked
Then
writer.WriteAttribute("checked",
"checked")
End If
If rb.Enabled =
False Then
writer.WriteAttribute("disabled",
"disabled")
End If
If rb.ToolTip <>
String.Empty
Then
writer.WriteAttribute("title", rb.ToolTip)
End If
If rb.AccessKey <>
String.Empty
Then
writer.WriteAttribute("accesskey", rb.AccessKey)
End If
writer.Write(HtmlTextWriter.SelfClosingTagEnd)
End If
End Sub
Protected Overrides Sub RenderBeginTag(ByVal
writer As System.Web.UI.HtmlTextWriter)
' Write the label if the Text attribute is set and TextAlign=Left
Dim rb
As RadioButton =
CType(Control, RadioButton)
If rb.Visible
AndAlso rb.Text <>
String.Empty
AndAlso rb.TextAlign = TextAlign.Left
Then
writer.WriteBeginTag("label")
writer.WriteAttribute("for", rb.UniqueID)
writer.Write(HtmlTextWriter.TagRightChar)
writer.Write(rb.Text)
writer.WriteEndTag("label")
End If
End Sub
Protected Overrides Sub RenderEndTag(ByVal
writer As
System.Web.UI.HtmlTextWriter)
' Write the label if the Text attribute is set and TextAlign=Right
Dim rb
As RadioButton =
CType(Control, RadioButton)
If rb.Visible
AndAlso rb.Text <>
String.Empty
AndAlso rb.TextAlign = TextAlign.Right
Then
writer.WriteBeginTag("label")
writer.WriteAttribute("for", rb.UniqueID)
writer.Write(HtmlTextWriter.TagRightChar)
writer.Write(rb.Text)
writer.WriteEndTag("label")
End If
End Sub
nickfoster
Member
58 Points
40 Posts
Adapter for single control - radiobutton
Aug 03, 2006 08:17 AM|LINK
Because my page is using a master page, any radiobutton I place in my gridview is renamed with a unique name and so do not function in a group. If i give my radiobutton a GroupName of "myGroup" it is renamed "ctl00$cphMain$Wizard1$gvPackages$ctl02$myGroup" which is not very helpful.
I am new to Control Adapters and so far I have looked through the example files to try and work out the best approach.
Is there a way to obtain the html that would be rendered by the standard control and then modify it by replacing part of the html string or am I stuck with writing all the html output myself to completely replace the rendering of the control? ( I tried calling Control.Render but obviously this dissapears into an infinite loop.) Am I going to have to inspect every property of the Control and rewrite the html in the RenderContent function?
Sorry for the basic questions but all the examples are of complex compound controls and I can't find an example of just rendering a single simple control.
Cheers,
Nick
nickfoster
Member
58 Points
40 Posts
Adapter for single control - radiobutton - viewstate problem
Aug 03, 2006 02:26 PM|LINK
************************************************************************
Imports System
Imports System.Data
Imports System.Collections
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Namespace CSSFriendly
Public Class RadioButtonAdapter
Inherits System.Web.UI.WebControls.Adapters.WebControlAdapter
Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim rb As RadioButton = CType(Control, RadioButton)
If Not IsNothing(rb) AndAlso rb.Visible Then
writer.WriteBeginTag("input")
writer.WriteAttribute("type", "radio")
writer.WriteAttribute("id", rb.UniqueID)
writer.WriteAttribute("name", rb.GroupName)
writer.WriteAttribute("value", rb.ID)
If rb.CssClass <> String.Empty Then
writer.WriteAttribute("class", rb.CssClass)
End If
If rb.Checked Then
writer.WriteAttribute("checked", "checked")
End If
If rb.Enabled = False Then
writer.WriteAttribute("disabled", "disabled")
End If
If rb.ToolTip <> String.Empty Then
writer.WriteAttribute("title", rb.ToolTip)
End If
If rb.AccessKey <> String.Empty Then
writer.WriteAttribute("accesskey", rb.AccessKey)
End If
writer.Write(HtmlTextWriter.SelfClosingTagEnd)
End If
End Sub
Protected Overrides Sub RenderBeginTag(ByVal writer As System.Web.UI.HtmlTextWriter)
' Write the label if the Text attribute is set and TextAlign=Left
Dim rb As RadioButton = CType(Control, RadioButton)
If rb.Visible AndAlso rb.Text <> String.Empty AndAlso rb.TextAlign = TextAlign.Left Then
writer.WriteBeginTag("label")
writer.WriteAttribute("for", rb.UniqueID)
writer.Write(HtmlTextWriter.TagRightChar)
writer.Write(rb.Text)
writer.WriteEndTag("label")
End If
End Sub
Protected Overrides Sub RenderEndTag(ByVal writer As System.Web.UI.HtmlTextWriter)
' Write the label if the Text attribute is set and TextAlign=Right
Dim rb As RadioButton = CType(Control, RadioButton)
If rb.Visible AndAlso rb.Text <> String.Empty AndAlso rb.TextAlign = TextAlign.Right Then
writer.WriteBeginTag("label")
writer.WriteAttribute("for", rb.UniqueID)
writer.Write(HtmlTextWriter.TagRightChar)
writer.Write(rb.Text)
writer.WriteEndTag("label")
End If
End Sub
End Class
End Namespace