ASP.Net RadioButton visibility inside a RadioButtonList - asp.net

Is there a way to hide radio buttons inside a RadioButtonList control programmatically?

Under the hood, you can access the attributes of the item and assign it a CSS style.
So you should be able to then programmatically assign it by specifying:
RadioButtonList.Items(1).CssClass.Add("visibility", "hidden")
and get the job done.

Here's how you have to apply a style attribute to a listitem:
RadioButtonList.Items(1).Attributes.Add("style", "display:none")
- OR -
RadioButtonList.Items(1).Attributes.Add("style", "visibility:hidden")

Why not add and remove the radio buttons as needed?
RadioButtonList.Items.Add("Item Name" or index);
RadioButtonList.Items.Remove("Item Name" or index);

Try This:
RadioButtonList.Items.Remove(RadioButtonList.Items.FindByValue("3"));

If you mean with JavaScript, and if I remember correctly, you've got to dig out the ClientID properties of each <input type="radio" ...> tag.

Have you tried to hide it through the itemdatabound event onload or do you need it to hide after it loads?

I haven't tested it, but I'd assume (for C#)
foreach(ListItem myItem in rbl.Items)
{
if(whatever condition)
myItem.Attributes.Add("visibility","hidden");
}

Another answer to not visibility inside a RadioButtonList.
Try this code:
RadioButtonList.Items(1).CssClass.Add("display", "none");
and get the job to no display RadioButtonList in layout.

Related

How do layout how items are shown in asp gridview?

I'd like to change the way gridview shows its items according to the format below.
<img1> <img2> <img3> <img4>
<caption1> <caption1> <caption1> <caption1>
<img5> <img6> <img7> <img8>
<caption5> <caption6> <caption7> <caption8>
and so on...
You can use custom formatting options with the DataList or Repeater controls instead of a GridView. Check these examples for help :
http://csharpdotnetfreak.blogspot.com/2009/05/aspnet-creating-shopping-cart-example.html.
http://www.asp.net/web-forms/tutorials/data-access/displaying-data-with-the-datalist-and-repeater/formatting-the-datalist-and-repeater-based-upon-data-vb
Set AutoGenerateColumn property of gridview to false and then use <asp:TemplateField><ItemTemplate> under <Columns> section to add controls the way you like.

how to display checkboxes on read-only form (ASP.NET)

I have a read-only form filled out automatically from a database. I have several fields with true/false values, and would like them displayed as checkboxes that are either empty or checked. I can databind the checkbox control and disable it, but then it appears grayed out. Is there a simple way to change this so it will show up at a normal, easy-to-read boldness but still be disabled? If not, what's the best way to do this? Should I use an image?
You can go like this as well.
<asp:CheckBox id="checkbox1" Text="Custom" onclick="javascript: return false;" />
Which will render it as
<input type="checkbox"
checked onclick="javascript: return false;">Custom</input>
Interesting question!
There's only one issue I can see with using images instead of normal checkboxes, and that's how the active checkboxes will differ from the disabled (image-based) checkboxes. So, if you go the image route, you will probably want to style all checkboxes. :)
You can effectively disable any checkbox with the following jQuery method (I've only tested in Chrome and IE 9, so far).
$('.readonly:checkbox').click(function(e) {
e.preventDefault();
});
This will "disable" any checkbox with the "readonly" class.
Or, you can use an inline JavaScript function (albiet not recommended, as it adds clutter and confusion):
<input type="checkbox" onclick="event.preventDefault();" />
The reason you would use "preventDefault()" instead of "return false", is because returning false will stop propagation. This means that your click event will not bubble to the parent element. That could potentially cause problems with other code... but it's unlikely.
Also, like Bala R mentioned, you mustn't rely on these restrictions to work. Your server side code should be aware that these values are read only, and refrain from updating them.
Here is jsFiddle example: http://jsfiddle.net/xixionia/3rXCB/
I hope this is helpful! :)
You can do something like this with javascript and use it for the checkbox's onclick handler.
jsFiddle link
function makeMeReadonly(checkbox){
checkbox.checked = !checkbox.checked;
}
and
<asp:CheckBox id="checkbox1" onclick="makeMeReadonly(this)" />
or
<asp:CheckBox id="checkbox1" onclick="this.checked = !this.checked" />
if you want it inline.
but you shouldn't depend on this for security as it will be a regular check box when javascript is not available and/or client code is not that difficult to work around client code restrictions but in your case since you have a readonly view, I wouldn't think you have any logic to save changes.
I just had the same problem and fixed it with an OnClick event on the checkbox. In the event handler the .Checked state is set to the value it is supposed to display.
private void chkBox_CheckStateChanged(object sender, EventArgs e)
{
chkBox.Checked = boolDatabasevalue;
return;
}
Works great, the user can click on the checkbox but the check mark does not change.
The only issue is a slight border shadow indicating the Focus. But the checkmark is, as OP was looking for, of easy-to-read boldness.
NB: this is C#, but in Asp.Net this should work similarly.

Disabling the asp.net link button n radios using Jquery

How to disable the asp.net Link buttons and asp.net radio buttons.
I have used
to enable
$("#sLbtnFirst").attr("disabled", "");
to disable
$("#sLbtnFirst").attr("disabled", "disabled");
but i'm able to click the buttons they are just greying
In .NET radio buttons, the attribute that specifies whether they're clickable is called "enabled", not "disabled".
But, at client-side they are rendered as html input tags which do contain the attribute "disabled" so to disable them you would want to use:
$("#sRbtnFirst").attr("disabled", "disabled");
To enable:
$("#sRbtnFirst").removeAttr("disabled");
EDIT: I've tried your jQuery myself in a .NET app and it seems to grey out the radio buttons fine (and prevent clicking). The LinkButton's a different story though.
You'll also need to remove the "href" attribute to prevent it from performing an action on click. So:
$("#sLbtnFirst").attr("disabled", "disabled");
$("#sLbtnFirst").removeAttr("href");
That should disable the LinkButton.
Also, remember that your IDs will change when they are rendered in the form so they won't be what they are when you view your aspx page in Visual Studio. .NET will change them into something like:
#ct100_sLbtnFirst
Link buttons render as anchor tags (<a href=) and the disabled attribute is not defined for this tag. As far as radio buttons are concerned you could apply the disabled attribute to them, they will grey out and their value won't be sent when you post the form.
To enable/disable linkbutton, try this:
//disabling
document.getElementById('sLbtnFirst').onclick = function() {return false;}
//enabling
document.getElementById('sLbtnFirst').onclick = function() {return true;}
You should use:
$("#sRbtnFirst").removeAttr("disabled");

disable jquery datepicker

I have used jquery datepicker in my .aspx page. The control is working fine. What i need is to disable the control if the textbox on which it is linked is disabled. For ex. I am showing datepicker on textbox "txtDateOfAssignment". If the Enabled property of this textbox is false then datepicker should not be active on that.
Anybody have an idea?
Thanks in advance.
if you look at the documentation you'll notice that there is a "disable" method, so you can do :
$('id-of-your-textbox').datepicker('disable');
Ok, finally you're not using jquery-UI but jquery-datepicker, so it should be more something like as referred to the documentation here
$(document).ready(function () {
$('.date-picker').dpSetDisabled(true);
});
If you want to disable only 1 datepicker dont use the class selector ".date-picker" but the id of the datepicker you want to disable.
Another possible Solution is
var j$=jQuery.noConflict();
j$("#inputTextID").datepicker(('disable' ));
j$("#inputTextID").datepicker(('enable' ));

jQuery and ASP.NET names

What is the correct way to get a control that was rendered by ASP.NET with jQuery?
Example: I have a checkbox that was generated like this:
<input id="ctl00_Content_chkOk" type="checkbox" name="ctl00$Content$chkOk" />
If I want to select it to get if it's checked, I tried this:
$('form input[name=chkOk]').attr("checked")
and
$('chkOk').attr("checked")
But it didn't work, I had to do it like this
$('form input[name=ctl00$Content$chkOk]').attr("checked")
And I guess this would've worked too:
$('ctl00$Content$chkOk').attr("checked")
Is this the correct way to do it? Is there another selector I can use?
You can use the server side control ClientID property:
var isChecked = $('#<%=YourCheckBox.ClientID%>').attr("checked");
or you could use the "ends with" selector: attribute$=value
var isChecked = $('form input[name$=chkOk]').attr("checked");
I always used this notation
$('#ctl00_Content_chkOk:checked').length; // will evaluate as true when checked
You can do
$("input[id*=_chkOk]").attr('checked');
$('#ctl00_Content_chkOk').attr('checked');
$('form #ctl00_Content_chkOk').attr('checked');
$('form input#ctl00_Content_chkOk').attr('checked');
$('form input#ctl00_Content_chkOk[type="checkbox"]').attr('checked');
Pick one ;)
Yeah, JQuery only cares about what is rendered on the page, whether through the ASP.NET engine or through simple HTML, so you'll have to access it via the id or name. The two bottom versions you used would work as well as $('#ct100_Content_chk0k').attr("checked").
Adam
I tend to go back and forth between two ways. I either use the control's clientID property as mentioned by CMS, or I generate the javascript code in my code behind and use the page's ClientScriptManager to write it to the browser.

Resources