disable href on content page from Master page in vb.net
Just as the title says. I have cod开发者_Go百科e that disables all controls that runat=server. Its as follows
Dim c As Control
For Each c In pc
If c.HasControls Then DisableAllControls(c.Controls)
If c.GetType.ToString.ToLower.IndexOf("webcontrols.dropdownlist") > -1 Then
DirectCast(c, DropDownList).Enabled = False
ElseIf c.GetType.ToString.ToLower.IndexOf("webcontrols.textbox") > -1 Then
DirectCast(c, TextBox).Enabled = False
ElseIf c.GetType.ToString.ToLower.IndexOf("webcontrols.radiobuttonlist") > -1 Then
DirectCast(c, RadioButtonList).Enabled = False
ElseIf c.GetType.ToString.ToLower.IndexOf("webcontrols.radiobutton") > -1 Then
DirectCast(c, RadioButton).Enabled = False
ElseIf c.GetType.ToString.ToLower.IndexOf("webcontrols.button") > -1 Then
DirectCast(c, Button).Enabled = False
End If
Next
But I have a couple of href's in there that i want to disable also. I know they dont runat server, so how can i catch these?
You can add runat="server"
:
<a runat="server" href="..." ></a>
It is the only way to maintain controls using server side code.
You can add runat="server" to HTML controls too. Without it you won't be able to access the control on the server side and you won't be able to disable it.
That code looks invalid (If c.HasControls Then DisableAllControls(c.Controls)
has no matching End If
) but perhaps VB.NET has added inline If
syntax like that without me realising.
Anyway, as far as disabling all runat="server"
controls, you should be able to just do this:
For Each c as WebControl In pc.OfType(Of WebControl)()
' Put your recursive call here as before
c.Enabled = False
Next
Now, to "disable" those other elements, you can either add runat="server"
to them (perhaps not even possible if you have generated HTML), or you can use some JavaScript. I'm going to assume by disable you mean hide in the case of <a>
tags?
jQuery makes this easy, with a sample script being something like:
$(document).load(function() {
$('a').hide();
});
or:
/* hides all a tags under an element with class="someClass" */
$(document).load(function() {
$('.someClass a').hide();
});
You could then have your code render this script, using something like this in your Page:
Dim script as String = "" /* your javascript here */
Me.ClientScript.RegisterClientScriptBlock(Me.GetType(), "HideTagsScript", script)
精彩评论