disable or hide listbox by dropdown selection in C# - c#

HELLO
i have one dropdown box and one listbox ,
my dropdown box values is
1-ALL
2-CUSTOM
my listbox values is retrieved from sql database
email addresses
what i want to do is if i select ALL from the dropdown box , it will disable or hide the list box in the web page. , if i choose "CUSTOM" it will enable it again.
i tried this code, but it doesn't work
if (DropDownList1.Text == "CUSTOM")
{
ListBox1.Visible = true;
}
note : i put visible = false in the properties of the listbox1
where is the problem exactly ? and where should i put this condition on the .cs page ?

You need to add an event to the dropdown box, so that your code is executed when the event fires. If you are using the designer, select the dropdown then above where the properties are there should be a little lightning symbol. Click on that and you will see all the events that the dropdown can fire. In there look for SelectedIndexChanged. Double click in there and it will create some code for you, which will look something like:
protected void mycombobox_SelectedIndexChanged(object o, EventArgs e)
{
}
Put your code in that section.

UPDATED:
If the text in the dropdown is CUSTOM then use this
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "CUSTOM")
{
ListBox1.Visible = true;
}
}
You will also need to set the AutoPostBack="true" on the DropDownList1.

Related

How to reset combobox?

I have a combobox with dropdown style and 2 text boxes. I want to add a condition that if the string of either of the 2 text boxes are not null than the combobox should get reset if any item is selected from it.
I'm using combobox.SelectedIndex=-1 in my if clause but it does not work I guess because I'm using it in a wrong event.
Make sure that both your textboxes are using the TextChanged event, and then point them to the same method. If both boxes are not null, then the combobox will reset. If you want it to be one or the other, just changed the && to ||
private void TextBox_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
{
comboBox1.SelectedIndex = -1;
}
}
Try This
combobox.Items.Clear();
or
combobox.DataSource = null;
I hope you managing you Text_Changed event well , cause you have not posted that code
Same as clearselection in c#
the command for WF is
combobox.ResetText()

disable dropdownlist option after selection asp.net webform

I want to disable my dropdownlist after a user select the droopdownlist. So, if the user enter this page, the user only lead to
dropdownlist when they have not choose before
the selected item value if they have choose before, but they can not select again (means: it disabled)
http://i.stack.imgur.com/H7uYT.png
because every selected item here will trigger some calculation to the next page.
Do you have any tutorial to do this? Or can you help me to solve this?
Just set the DowpDownList's AutoPostBack property to true and handle the SelectedIndexChanged-event. There you can disable it:
protected void Ddl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList) sender;
ddl.Enabled = false;
// store the SelectedValue somewhere, f.e. in the Session
// or Response.Redirect to the next page and pass it via url-parameter
}

How to enable or disable a combo box based on a radio button's value C#

I have a simple stupid question but I just can get the hang of it...So i have a form in C#, with a combo box button and also a radio button. I want when selected a value from radio button that the combo box to be disabled, if not the combo box to be enabled...
Any hints?
Many thanks in advance,
I would suggest subscribing to the radioButton checked change event, then as Sudhakar suggested update the combo box enabled property according to what you want to happen.
So for example;
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
comboBox1.Enabled = !radioButton1.Checked;
}
if you want to Enable/Disable Combobox based on the RadioButton Checked status you can check for Checked property of the RadioButton and Enable/Disable the Combobox.
Checked Property returns the boolean value, so if it is true you can disable the ComboBox .
if it is false you can Enable the ComboBox.
Note : Handle the RadioButton CheckedChanged Event to handle it properly.
Try This :
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked)
comboBox1.Enabled = false;
else
comboBox1.Enabled = true;
}
You can use DataBinding to acheive this.
The result may look like this:
public Form1()
{
InitializeComponent();
comboBox1.DataBindings.Add(new System.Windows.Forms.Binding("Enabled", radioButton1, "Checked", true, DataSourceUpdateMode.OnPropertyChanged));
}

Hide button on drop down selection -1

I need my create button to be hidden unless a facility is selected in my dropdown. When it is at -1 message i need my button to be hidden.
Code for button
<asp:Button ID="btnCreate" runat="server" Text="Create New" Width="89px" Font-Size="X-Small" OnClick="btnCreate_Click" />
Drop down code
private void ResetForm()
{
try
{
//facility dropdown
ddlFacility2.Items.Clear();
ddlFacility2.DataSource = this.DataLayer.model.MS_spGetFacilityInfo(null).OrderBy(x => x.FacilityName);
ddlFacility2.DataTextField = "FacilityName";
ddlFacility2.DataValueField = "FacilityID";
ddlFacility2.DataBind();
ddlFacility2.Items.Insert(0, new ListItem("All Facility Records..", "-1"));
BindGrid();
}
catch (Exception ex)
{
this.SetMessage(ex.ToString(), PageMessageType.Error);
AISLogger.WriteException(ex);
}
}
in first time page load if the default value selected is -1 you can set your button visible false as default.
in your droupdown list selected index change event you can enable/dissable button based on droupdown list selected value.
Add a OnSelectedIndexChange event to your dropdown list or add a clientside event to your dropdownlist. Double Click on your ddl you will see a function named ddlFacility2_OnSelectedIndexChanged in you code behind and add the below code to it.
Add AutoPostBack=true to you ddl
protected void ddlFacility2_OnSelectedIndexChanged(object sender, EventArgs e)
{
if(ddlFacility2.SelectedIndex>-1)
{
btnCreate.Enabled = true;
}
else
{
btnCreate.Enabled = false;
}
}
You can wire up a JQuery script that can bind to your DropDownList's selected value...
In this example, the button's visibility is bound on a click from another button:
$('#Button1').bind("click", function() {
$("#Button2").hide();
});
I dont know the exact syntax to use for the binding to selected value, but the above code should be a good place to start.

Preserve datalist data on load event in c#

I have datalist containing radio buttons and check boxes. If I select some of them and then redirect to another aspx page. when I come to previous page the radio buttons and check boxes in datalist must be selected. I have selected data in session variable. How to do this using c#.
Using the ItemDataBound event I would retrieve the radio button and compare it's value to your Session variable
something like this:
protected void MyDataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
RadioButton myRadioButton = e.Items.FindControl("myRadioButton") as RadioButton;
if (myRadioButton == null) { }
else
{
if (string.IsNullOrEmpty(Session[myRadioButton.Value])) {}
else myRadioButton.Checked = true;
}
}

Categories

Resources