Let say I want to hide one of the radio buttons with value 0, what code can make it visible = false? I was using Javascript, C# and ASP.NET.
<asp:ListItem Value="1"> 1 </asp:ListItem>
<asp:ListItem Value="2"> 2 </asp:ListItem>
<asp:ListItem Value="3"> 3 </asp:ListItem>
<asp:ListItem Value="4"> 4</asp:ListItem>
<asp:ListItem Value="0" Selected="True" Enabled="False">
foreach (ListViewDataItem item in ListView1.Items)
{
var rbl = (RadioButtonList)item.FindControl("rblSelect");
var selectedValue = int.Parse(rbl.SelectedItem.Value);
var selectedText = rbl.SelectedItem.Text;
var selectedIndex = rbl.SelectedIndex;
rbl.Items[0].Attributes.CssStyle.Add("visibility", "hidden");
}
Try This : From CodeBehind
MyRadioButtonList.Items[0].Attributes.CssStyle.Add("visibility", "hidden");
EDIT:
int count = 0; //index of item tobe hidden
foreach (ListViewDataItem item in ListView1.Items)
{
var rbl = (RadioButtonList)item.FindControl("rblSelect");
var selectedValue = int.Parse(rbl.SelectedItem.Value);
var selectedText = rbl.SelectedItem.Text;
var selectedIndex = rbl.SelectedIndex;
if(count == 0)
rbl.Attributes.CssStyle.Add("visibility", "hidden");
count++;
}
try this
rbl.Items[0].Enabled = false;
Use the following:
MyRadioButtonList.Items[0].Attributes.CssStyle.Add("display", "none");
In javascript it's possible to hide it with ".style.display"
var radio = document.getElementById('idOfYourRadiobutton');
radio.style.display = 'none'; // to hide
radio.style.display = 'block'; // to show
You need to amend the code as follows:
rdBtn.items.remove("name_of_the_item");
In this case, the rdBtn is the RadioButtonList ID
You can try this if you don't want to display it:
ListView1.Items.FindByValue("0").Attributes.CssStyle.Add("display", "none")
Changes ("display", "none") to ("visibility", "hidden") to hide it.
What is the different between hide and none display?
Well, if you hide a radio button between two other radio buttons, there is still a space between two those button. If you don't display it, there is no such weird space.
Related
I work in ASP NET 4.0 and C-sharp.
I have one form page .aspx with this DropDownList (Inside is the ID) and save the value S or N in the matching field in database :
<asp:DropDownList ID="Inside" runat="server" Width="100" CssClass="ddl_Class">
<asp:ListItem Text="-------" Value=""></asp:ListItem>
<asp:ListItem Text="S" Value="S"></asp:ListItem>
<asp:ListItem Text="N" Value="N"></asp:ListItem>
</asp:DropDownList>
I need that when the value of field Inside in database is valid and is not null, in the DropDownList it's selected the value set in the database and disable the DropDownList.
I have tried this code in .cs page without success.
Anybody know how can I do that?
code-behind
InsideDB = dr["Inside"].ToString();
if (!string.IsNullOrEmpty(InsideDB.ToString()))
{
Inside.Text = InsideDB.ToString();
Inside.Enabled = false;
}
else
{
Inside.Enabled = true;
}
Try SelectedValue instead of Text property.
InsideDB = dr["Inside"].ToString();
Inside.Enabled = true;
if (!string.IsNullOrEmpty(InsideDB.ToString()))
{
Inside.SelectedValue = InsideDB.ToString();
Inside.Enabled = false;
}
Try something like
Inside.SelectedIndex = Inside.Items.IndexOf(Inside.Items.FindByText(InsideDB.ToString()));
instead of Inside.Text = InsideDB.ToString();
This assumes that you have an item with that text in the dropdownlist already.
I know this is old post. But this may help others.
Inside.Items.FindByText(InsideDB.ToString()).Selected=true;
Inside.Enabled = false;
I am trying to deselect item (once selected) when user will click on the item in listbox.
I have a script which is deselecting the item on user click but that is happening even when the item is selected for first time as well.
This is what I have:
JS:
function deselect()
{
var list = document.getElementById('<%=ddlCity.ClientID%>');
var listLength = list.options.length;
for (var i = 0; i < listLength; i++) {
if (list.options[i].selected) {
list.selectedIndex = -1;
}
}
}
And the html:
<asp:ListBox ID="ddlCity" CssClass="formfield" runat="server" DataSourceID="SqlDataSourceCity"
DataTextField="Description" DataValueField="CityID" SelectionMode="Multiple" onclick="deselect();"
OnDataBound="DropDownList3_DataBound"></asp:ListBox>
Any idea how I can edit that JS to deselect the value once the value is selected, but when user selects the value for first time the value to be selected.
Thanks, Laziale
I would like to know how to maintain the control state that has been modified in Javascript.
I have two TextBoxes, one DropDownList and a button (all Runat=Server) in C# ASP.net 2010 Express.
First textbox is just accept whatever data user input. Second textbox enable state will change based on DDL selected value. If ddl selected value is "-", second textbox will become Enabled = False.
If not "-", it will become Enabled = True. This enable is done through Javascript.
In my Page Load event, I have below code.
if (!IsPostBack)
{
txtKey2.Text = "";
txtKey2.BackColor = System.Drawing.ColorTranslator.FromHtml("#CCCCCC");
txtKey2.Enabled = false;
}
And in my aspx page, I have some javascript which will clear the textbox data and disable the textbox.
Here is for Second Textbox.
<asp:TextBox ID="txtKey2" runat="server" Width="425px" EnableViewState="False"></asp:TextBox>
And here is for DDL.
<asp:DropDownList ID="selKey1" runat="server" onchange="EnableSelkey(this.value,1)">
<asp:ListItem Value="0">-</asp:ListItem>
<asp:ListItem Value="1">AND</asp:ListItem>
<asp:ListItem Value="2">OR</asp:ListItem>
</asp:DropDownList>
Here is the code for my Javascript. (I have a plan to implement other textbox and ddl so in my code I have Else if condition).
function EnableSelkey(val, strID) {
var txtBox;
if (strID == "1")
txtBox = document.getElementById('<%= txtKey2.ClientID %>');
else if (strID == "2")
txtBox = document.getElementById('<%= txtKey3.ClientID %>');
else
txtBox = document.getElementById('<%= txtKey4.ClientID %>');
if (val != 0) {
txtBox.disabled = false;
txtBox.style.backgroundColor = "White";
txtBox.value = "";
txtBox.select();
}
else {
txtBox.disabled = true;
txtBox.style.backgroundColor = "#CCCCCC";
txtBox.value = "";
}
}
I have nothing in button click event.
By using above all code, when I run the project, the page loads Ok.
The second textbox enabled state is set to False (through Page_Load event). So far Ok.
Then from my browser, I choose ddl value to other instead of "-", the textbox become enable because of javascript. This is Ok.
I input the value and click on the button. Page PostBack happens here. Textbox is still enabled (because of EnableViewState = False for my textbox).
I choose ddl value to "-", second textbox became disabled.
Click on the button, page postback happen, but this time the textbox is still enabled. << This is the issue I'm trying to solve. I change EnableViewState, ViewStateMode in different values but still the same.
Is there any solution for this one?
Here is my test image URL.
State 1 ,
State 2 ,
State 3
Sorry for the long post.
I have tried and found no solution beside using additional HiddenField control.
I update the hidden field value when I update the status of my textbox in Javascript.
And on my Page Load event, I checked all the hidden field values and based on the hiddenfield values, I will disable/enable my textboxes which is for me not a good solutions.
Imagine I have 10 or 15 textboxes on my form, I need to have 10 or 15 hidden field just to maintain client side action result.
Currently, this is the only solution for me.
I'm not sure can this consider as 'Answer' so I haven't close this question yet.
<asp:DropDownList ID="selKey1" runat="server" onchange="EnableSelkey(this.value,1)">
<asp:ListItem Value="0">-</asp:ListItem>
<asp:ListItem Value="1">AND</asp:ListItem>
<asp:ListItem Value="2">OR</asp:ListItem>
</asp:DropDownList>
function EnableSelkey(val, strID) {
var txtBox;
if (strID == "1")
txtBox = document.getElementById('<%= txtKey2.ClientID %>');
else if (strID == "2")
txtBox = document.getElementById('<%= txtKey3.ClientID %>');
else
txtBox = document.getElementById('<%= txtKey4.ClientID %>');
if (val != 0) {
txtBox.disabled = false;
txtBox.style.backgroundColor = "White";
txtBox.value = "";
txtBox.select();
}
else {
txtBox.disabled = true;
txtBox.style.backgroundColor = "#CCCCCC";
txtBox.value = "";
}
}
You Have to call you javascript function on every postback.
if (!IsPostBack)
{
txtKey2.Text = "";
txtKey2.BackColor = System.Drawing.ColorTranslator.FromHtml("#CCCCCC");
txtKey2.Enabled = false;
}
ScriptManager.RegisterClientScriptBlock(this, typeof(System.Web.UI.Page), "MyJSFunction", "EnableSelkey("+selKey1.SelectedValue+",1);", true);
It May be help you, Let me know for further help.
I found a solution that worked was to put the code that I put in the !IsPostBack section for enabling and disabling the textboxes also directly in the page load event.
i have a datalist contains checkboxlist.
<asp:DataList ID="dtlstfilter" runat="server">
<ItemTemplate>
<asp:CheckBoxList ForeColor="Gray" AutoPostBack="true" OnSelectedIndexChanged="chklist_SelectedIndexChanged" ID="chklist"
runat="server">
</asp:CheckBoxList>
</ItemTemplate>
</asp:DataList>
when i check one from the checkbox list in the SelectedIndexChanged event i got the selected value using
CheckBoxList c = (CheckBoxList)sender;
string selectedvalue= c.SelectedValue;
likewise how can get the value from a checkboxlist if i uncheck one from the checkboxlist
The SelectedIndexChanged gets also fired if you uncheck a CheckBox. So it works the same way. But if you want to know the (now) unchecked item(s), you have to store the old selection somewhere, for example in the ViewState:
private IEnumerable<string> SelectedValues
{
get
{
if (ViewState["SelectedValues"] == null && dtlstfilter.SelectedIndex >= -1)
{
ViewState["SelectedValues"] = dtlstfilter.Items.Cast<ListItem>()
.Where(li => li.Selected)
.Select(li => li.Value)
.ToList();
}else
ViewState["SelectedValues"] = Enumerable.Empty<string>();
return (IEnumerable<string>)ViewState["SelectedValues"];
}
set { ViewState["SelectedValues"] = value; }
}
protected void chklist_SelectedIndexChanged(Object sender, EventArgs e)
{
CheckBoxList c = (CheckBoxList)sender;
var oldSelection = this.SelectedValues;
var newSelection = c.Items.Cast<ListItem>()
.Where(li => li.Selected)
.Select(li => li.Value);
var uncheckedItems = newSelection.Except(oldSelection);
}
This should even work if multiple checkboxes can be selected.
You can take the jQuery Route if it suits you...
if (!IsPostBack)
{
foreach (ListItem item in chkList.Items)
{
//adding a dummy class to use at client side.
item.Attributes.Add("class", "chkItem");
}
}
Put one button on your form with style display : none. And a Hidden Field to track the currently checked checkbox.
<asp:Button ID="hdnButton" runat="server" style="display:none;" OnClick="hdnButton_Click"/>
<asp:HiddenField ID="hdnCurrent" runat="server" />
The jQuery Part....
$(".chkItem input:checkbox").change(function(){
$("#hdnCurrent").val($(this).attr("id") + "|" + $(this).attr("checked"));
$("#hdnButton").click();
});
You can use more hidden fields if you don't want to do string operations on backend. Depends on your taste.
Then handle the button click event like below.
protected void hdnButton_Click(object sender, EventArgs e)
{
String[] Value = hdnCurrent.Value.Split('|');
if (Value[1] == "true")
{
//Do operations here when the check box is checked
}
else
{
//Do operations here when the check box is unchecked
}
//Value[0] contains the id of the check box that is checked/unchecked.
}
<asp:TemplateField HeaderText="Select One">
<ItemTemplate>
<asp:RadioButton ID="RadioButton1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow di in GridView1.Rows)
{
RadioButton rad = (RadioButton)di.FindControl("RadioButton1");
if (rad.Checked&&rad!=null)
{
s = di.Cells[1].Text;
}
}
Response.Redirect("applicants.aspx?form=" +s);
}
I'm selecting the rows that are selected with this but I have a problem here I want user to be able to select only one radiobutton but its allowing all the radiobuttons to be selected at once.Can you help me in removing this problem please.
please.
Maybe I'm too late here on the party but this will do the trick,
check out here......
This might be useful for someone watching this answer in the future.
In the ASP page you need to set the GroupName property to be the same for all the radio buttons, e.g.:
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="RadioGroup" />
well for that you can use follwing code
first of all you should define the groupName.The follwing code will work
<asp:RadioButton ID="RadioButton1" OnCheckedChanged="rbSelector_CheckedChanged" AutoPostBack="true" GroupName="Apply" runat="server"></asp:RadioButton>
C#
protected void rbSelector_CheckedChanged(object sender, System.EventArgs e)
{
foreach (GridViewRow oldrow in GridView2.Rows)
{
((RadioButton)oldrow.FindControl("RadioButton1")).Checked = false;
}
//Set the new selected row
RadioButton rb = (RadioButton)sender;
GridViewRow row = (GridViewRow)rb.NamingContainer;
((RadioButton)row.FindControl("RadioButton1")).Checked = true;
}
You can try this link to select single radiobutton in grid : http://www.c-sharpcorner.com/uploadfile/krishnasarala/select-single-radio-button-in-gridview-in-Asp-Net/
Using the GroupName property by itself won't work, each radio button will still get a unique name attribute since they're in different rows of the grid.
One option is to emit the radio button markup manually using a Literal control (example). This will make it easy to group the radio buttons on the client-side, but requires a bit more work to determine which button was selected on postback.
When I needed this behavior, I found it easier to keep the radio buttons as server-side controls, and just enforce the button group w/ jQuery. Put your RadioButton in a TemplateField as you've shown, then add this code to uncheck all the other buttons when one is checked:
$(document).ready(function () {
// could also pass in a unique ID selector
createManualRadioButtonGroupForGridView(".myGridViewClass");
});
function createManualRadioButtonGroupForGridView(gridViewSelector) {
$(gridViewSelector + " input[type=radio]").change(function () {
var checkedRadioButton = this;
$(gridViewSelector + " input[type=radio]").each(function (e) {
if (this !== checkedRadioButton) {
$(this).prop("checked", false);
}
});
});
}