i am working on an software in which i want to clear each thing after doing achieving a specific task, like clearing all the fields, list view etc etc and i've to write the a lot of things to be cleared off, like the code of my new button is:
private void btnNew_Click(object sender, EventArgs e)
{
txtProductCode1.ReadOnly = false;
txtInvoiceNo.ReadOnly = false;
cmbCustoemerType.Enabled = false;
cmbCustomerName.Enabled = false;
button1.Enabled = true;
txtProductCode1.Text = null;
txtWageName.Text = null;
txtWageCost.Text = null;
btnFirst.Visible = false;
btnPrevious.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
cmbProductName.Enabled = true;
txtQty.ReadOnly = false;
txtPercant.ReadOnly = false;
txtDiscount.ReadOnly = false;
cmbCustoemerType.Enabled = true;
cmbCustomerName.Enabled = true;
button5.Enabled = true;
btnDelete.Enabled = true;
dtp1.Enabled = true;
btnSave.Text = "&Save";
cmbCustoemerType.Text = null;
cmbCustomerName.Text = null;
txtInvoiceNo.Clear();
txtDiscount.Clear();
txtGrandTotal.Clear();
txtProductName.Clear();
txtSalePrice.Clear();
txtQty.Clear();
txtTotal.Clear();
txtExtraWages.Text = null;
lvExtraWages.Items.Clear();
lvTransaction.Items.Clear();
lblCount.Text = null;
lblNetTotal.Text = null;
txtPercant.Text = null;
txtInvoiceNo.Text = invoice.ToString();
}
is there any way to get rid of writing the code again n again, like i need to enable one thing which i've disabled here on another button so i have to write reverse code of this to achieve the task and it's really annoying! is there any other way to do this?
EDIT:
tried following code:
private void btnNew_Click(object sender, EventArgs e)
{
//using setboxdefault function
SetTextBoxDefaults();
cmbCustoemerType.Text = null;
cmbCustomerName.Text = null;
cmbCustoemerType.Enabled = false;
cmbCustomerName.Enabled = false;
cmbProductName.Enabled = true;
cmbCustoemerType.Enabled = true;
cmbCustomerName.Enabled = true;
button5.Enabled = true;
btnDelete.Enabled = true;
button1.Enabled = true;
btnFirst.Visible = false;
btnPrevious.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
btnSave.Text = "&Save";
lvExtraWages.Items.Clear();
lvTransaction.Items.Clear();
lblCount.Text = null;
lblNetTotal.Text = null;
dtp1.Enabled = true;
txtInvoiceNo.Text = invoice.ToString();
}
private void SetTextBoxDefaults()
{
var textBoxes = GetControls(this, typeof(TextBox));
foreach (var textBox in textBoxes)
{
TextBox.Clear();
}
}
public IEnumerable<Control> GetControls(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetControls(ctrl, type)).Concat(controls).Where(c => c.GetType() == type);
}
but it's giving error on TextBox.Clear(); , error is Error 4
An object reference is required for the non-static field, method, or property 'System.Windows.Forms.TextBoxBase.Clear()'
Please let me know where i am mistaking..!!
As Tim has suggested, you can iterate though the controls and set each one accordingly..
public IEnumerable<Control> GetControls(Control control,Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetControls(ctrl,type)).Concat(controls).Where(c => c.GetType() == type);
}
private void btnNew_Click(object sender, EventArgs e)
{
SetComboBoxDefaults();
SetTextBoxDefaults();
SetButtonDefaults();
}
private void SetComboBoxDefaults()
{
var comboBoxes = GetControls(this, typeof(ComboBox));
foreach (var comboBox in comboBoxes)
{
((ComboBox)comboBox).Enabled = false;
((ComboBox)comboBox).Text = null;
}
}
private void SetTextBoxDefaults()
{
var textBoxes = GetControls(this, typeof(TextBox));
foreach (var textBox in textBoxes)
{
((TextBox)textBox).Clear();
}
}
private void SetButtonDefaults()
{
var buttons = GetControls(this, typeof(Button));
foreach (var button in buttons)
{
((Button)button).Visible = false;
if (button.Name == "btnSave") ((Button)button).Text = "&Save";
}
}
You can also have separate GetControl methods so that it returns a specific type of Control, reducing the need to cast to derived types.
update for edit:
You are using the TextBox type instead of the variable name in the foreach loop. Update it to use textBox (lowercase), not TextBox (pascal) see below:
private void SetTextBoxDefaults()
{
var textBoxes = GetControls(this, typeof(TextBox));
foreach (var textBox in textBoxes)
{
((TextBox)textBox).Clear();
}
}
Remember to stay DRY
Don't
Repeat
Yourself
Partition the reset logic into suitable, small methods that reset a logical grouping of controls. Then invoke those methods where and as needed.
What I have done in several such cases is this:
void SetTextBoxReadOnly(bool val, param TextBox[] textBoxes)
{
if(textBoxes != null && textBoxes.Length > 0)
{
foreach(TextBox textBox in textBoxes)
{
if(textBox != null)
{
textBox.ReadOnly = val;
}
}
}
}
void SetControlEnabled(bool val, param Control[] ctrls)
{
if(ctrls != null && ctrls.Length > 0)
{
foreach(Control ctrl in ctrls)
{
if(ctrl != null)
{
ctrl.Enabled = val;
}
}
}
}
// etc set of methods for similar situations.
// there methods can be invoked as
...
...
...
SetTextBoxReadOnly(true, txtProductCode1,
txtInvoiceNo,
txtQty,
txtPercant,
txtDiscount);
Related
private void txtenable (Boolean txtenable)
{
if(txtenable== false)
{
txtname.Enabled = false;
txtTel.Enabled = false;
txtmobile.Enabled = false;
txtAdress.Enabled = false;
}
else
{
txtname.Enabled = true;
txtTel.Enabled = true;
txtmobile.Enabled = true;
txtAdress.Enabled = true;
}
}
I want use this class but i can not call textboxes. How can call textbox in class?
First you need to be able to accept the TextBox objects within the class, then you can manipulate them how you see fit. I haven't actually tried this, but this is how I would go about setting it up.
public class YourClass
{
TextBox txtName;
TextBox txtTel;
TextBox txtMobile;
TextBox txtAddress;
private void txtenable (Boolean txtenable, TextBox txtName, TextBox txtTel, TextBox txtMobile, TextBox txtAddress)
{
if(txtenable== false)
{
txtName.Enabled = false;
txtTel.Enabled = false;
txtMobile.Enabled = false;
txtAddress.Enabled = false;
}
else
{
txtName.Enabled = true;
txtTel.Enabled = true;
txtMobile.Enabled = true;
txtAddress.Enabled = true;
}
}
In order for you to access the textboxes from within your class you will need to pass them such as:
public class OtherClassContainingTextBoxes
{
private void SomeEvent(object sender, EventArgs e){
txtenable(true, txtName, txtTel, txtMobile, txtAddress);
}
However, based on the example provided, I am unsure why you wouldn't do this in a method within the class you have your textboxes.
You could do something on pageload:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["enable"] == false){
txtenable(false);
}else{
txtenable(true);
}
}
private void txtenable (Boolean txtenable)
{
if(txtenable== false)
{
txtName.Enabled = false;
txtTel.Enabled = false;
txtMobile.Enabled = false;
txtAddress.Enabled = false;
}
else
{
txtName.Enabled = true;
txtTel.Enabled = true;
txtMobile.Enabled = true;
txtAddress.Enabled = true;
}
}
i use my web.config file to define a menu it work fine. But to improve system i want to store my created menu control in an object menu_ to reuse it on next call.
if i add menu_ control to my master page (like i did with generated version) i have a RegisterRequiresControlState error :
RegisterRequiresControlState could not be call before or after prerender.
public class tabloidMenu : ConfigurationElementCollection
{
private Menu menu_;
static public explicit operator Menu(tabloidMenu configMenu)
{
if (configMenu.menu_ != null) return configMenu.menu_;
Menu menu = new Menu();
menu.CssClass = "menu";
menu.EnableViewState = false;
menu.ViewStateMode = System.Web.UI.ViewStateMode.Disabled;
menu.IncludeStyleBlock = false;
menu.Orientation = Orientation.Horizontal;
if (tabloidConfigMenu.configMenu != null)
{
foreach (tabloidConfigMenuItem item in configMenu)
{
menu.Items.Add((MenuItem)item);
}
}
configMenu.menu_=menu;
return menu;
}
in my master page i use
protected void Page_Init(object sender, EventArgs e)
{
bool popupMode = Page.Request["mode"] == "popup";
if (!popupMode)
{
System.Web.UI.HtmlControls.HtmlLink cssLink = new System.Web.UI.HtmlControls.HtmlLink();
cssLink.Href = "~/Styles/Site.css";
cssLink.Attributes.Add("rel", "stylesheet");
cssLink.Attributes.Add("type", "text/css");
Page.Header.Controls.Add(cssLink);
//add menu
if (Tabloid.tabloidConfigMenu.configMenu != null)
{
Menu mn = (Menu)Tabloid.tabloidConfigMenu.configMenu.TopMenu;
mn.EnableViewState = false;
mn.ViewStateMode = System.Web.UI.ViewStateMode.Disabled;
Page.Master.FindControl("MenuHolder").Controls.Add(mn);
}
else
{
Label l = new Label();
l.Text = Tabloid.tabloidConfigMenu.lastError;
Page.Master.FindControl("MenuHolder").Controls.Add(l);
}
}
do you have an idea.
Thanks for your help.
Firefox is causing a few problems for me at the moment and I can't quite figure them out.
I have two listboxes, one is populated when the page is loaded, and the other when an item from the first is selected then click view button. The problem I am having is the 2nd listbox populates for just a split second and then everything is deleted. The function works in all other browsers I have tested.. IE, Chrome, and Safari..
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;
using System.Data.SqlClient;
using DAL;
namespace ACESAdmin2.AcesSetup
{
public partial class storefront : System.Web.UI.Page
{
private string selectedCustomer
{
get { return Session["selectedCustomer"] != null ? (string)Session["selectedCustomer"] : string.Empty; }
set { Session["selectedCustomer"] = value; }
}
private string selectedCatalog
{
get { return Session["selectedCatalog"] != null ? (string)Session["selectedCatalog"] : string.Empty; }
set { Session["selectedCatalog"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadPage();
}
}
private void LoadPage()
{
initControls();
#region role setting
try
{
//check user role to determine editing rights
string usersRole = Roles.GetRolesForUser(Membership.GetUser().UserName)[0];
MembershipUser user = Membership.GetUser();
switch (usersRole)
{
case "BasicUser":
AlignTextNoneRadio.Enabled = false;
AlignTextLeftRadio.Enabled = false;
AlignRightRadio.Enabled = false;
AlignCenterRadio.Enabled = false;
headerText.Enabled = false;
returnPolicy.Enabled = false;
welcomeMessageTextBox.Enabled = false;
homeText.Enabled = false;
homeLink.Enabled = false;
updateStoreButton.Enabled = false;
break;
case "Customer":
homeLink.Enabled = false;
homeText.Enabled = false;
break;
case "SalesRep":
case "Admin":
case "SuperAdmin":
break;
default:
break;
}
}
catch (NullReferenceException error)
{
string message;
message = error.ToString();
Response.Redirect("../error.aspx");
}
#endregion
#region Accordion Pane Settings
if (ViewState["SelectedAccordionIndex"] == null)
{
MyAccordion.SelectedIndex = -1;
}
else
{
MyAccordion.SelectedIndex = (int)ViewState["SelectedAccordionIndex"];
}
#endregion
}
private void initControls()
{
//Viewable to all
#region customer label
if (selectedCustomer != null)
{
string customerName = DAL.Util.getCustomerName().ToString();
if (customerName != null || customerName != String.Empty)
{
lblCustomer.Text = "Customer: <font color=maroon>" + customerName + "</font>";
lblCustomer.Visible = true;
}
else
{
lblCustomer.Text = "Customer: <font color=maroon>" + selectedCustomer.ToString() + "</font>";
lblCustomer.Visible = true;
}
}
#endregion
//Edit Store Front
#region Populate Fields
#region headerAlign
string headerAlign = DAL.Util.getHeaderAlign().ToString();
switch (headerAlign)
{
case "left":
AlignTextLeftRadio.Checked = true;
break;
case "center":
AlignCenterRadio.Checked = true;
break;
case "right":
AlignRightRadio.Checked = true;
break;
default:
AlignTextNoneRadio.Checked = true;
break;
}
#endregion
welcomeMessageTextBox.Text = DAL.Util.getWelcome().ToString();
headerText.Text = DAL.Util.getHeaderText().ToString();
returnPolicy.Text = DAL.Util.getReturn().ToString();
homeLink.Text = DAL.Util.getHomeLink().ToString();
homeText.Text = DAL.Util.getHomeText().ToString();
#endregion
//BJIT Setup
#region popultae fields
ddlSelectVendor.DataSource = DAL.Util.getVendors();
ddlSelectVendor.DataBind();
listCatalogs.DataSource = DAL.Util.getCatalogs();
listCatalogs.DataBind();
#endregion
}
protected void returnButton_Click(object sender, EventArgs e)
{
//Takes user back to main admin page
Response.Redirect("../Admin/mainAdmin.aspx");
}
protected void dropdownlist_SelectedInexChange(object sender, EventArgs e)
{
ViewState["SelectedAccordionIndex"] = MyAccordion.SelectedIndex;
}
protected void updateStoreButton_Click(object sender, EventArgs e)
{
switch(MyAccordion.SelectedIndex)
{
case 0:
#region Header Value
string selectedHeaderAlign = null;
foreach (Control control in this.RadioPanel.Controls)
{
if (control is RadioButton)
{
RadioButton radio = control as RadioButton;
if (radio.Checked)
{
selectedHeaderAlign = radio.Text.ToLower();
}
}
}
#endregion
//updates customer information in the table
string sqlSf = "Update Store SET HeaderAlign = #HeaderAlign, HeaderText=#HeaderText, ReturnPolicy=#ReturnPolicy, WelcomeMessage=#WelcomeMessage, HomeTextLink=#HomeTextLink, HomeLink=#HomeLink"
+ " WHERE CustomerID='" + HttpContext.Current.Session["selectedCustomer"].ToString() + "'";
//setting parameters
#region Parameter Values
AdoUtil.ACESSQLParameterCollection parameters = new AdoUtil.ACESSQLParameterCollection();
AdoUtil.ACESSQLParameter param = new AdoUtil.ACESSQLParameter();
param.ParamName = "#HeaderAlign";
param.ParamValue = selectedHeaderAlign.ToString();
param.ParamDBType = SqlDbType.VarChar;
parameters.Add(param);
param = new AdoUtil.ACESSQLParameter();
param.ParamName = "#HeaderText";
param.ParamValue = headerText.Text.ToString();
param.ParamDBType = SqlDbType.VarChar;
parameters.Add(param);
param = new AdoUtil.ACESSQLParameter();
param.ParamName = "#ReturnPolicy";
param.ParamValue = returnPolicy.Text.ToString();
param.ParamDBType = SqlDbType.VarChar;
parameters.Add(param);
param = new AdoUtil.ACESSQLParameter();
param.ParamName = "#WelcomeMessage";
param.ParamValue = welcomeMessageTextBox.Text.ToString();
param.ParamDBType = SqlDbType.VarChar;
parameters.Add(param);
param = new AdoUtil.ACESSQLParameter();
param.ParamName = "#HomeTextLink";
param.ParamValue = homeText.Text.ToString();
param.ParamDBType = SqlDbType.VarChar;
parameters.Add(param);
param = new AdoUtil.ACESSQLParameter();
param.ParamName = "#HomeLink";
param.ParamValue = homeLink.Text.ToString();
param.ParamDBType = SqlDbType.VarChar;
parameters.Add(param);
#endregion
AdoUtil.ExecuteNonQuery(sqlSf, parameters);
break;
case 1:
//BJIT Updates
string sqlBJIT = "Update";
break;
default:
break;
}
MyAccordion.SelectedIndex = -1;
}
#region BJIT Control Events
//BJIT Button
protected void btnAddCustomer_Click(object sender, EventArgs e)
{
if (listItems.SelectedIndex != -1)
{
ArrayList removeArr = new ArrayList();
listItemProfiles.SelectedIndex = -1;
//Copy selected items to listItemProfiles
foreach (ListItem li in listItems.Items)
{
if (li.Selected)
{
listItemProfiles.Items.Add(li);
removeArr.Add(li);
}
}
//Remove the selected items from listItems
foreach (ListItem li in removeArr)
{
listItems.Items.Remove(li);
}
Util.WebFunctions.SortListBox(listItemProfiles);
}
}
//BJIT Button
protected void btnRemoveCustomer_Click(object sender, EventArgs e)
{
if (listItemProfiles.SelectedIndex != -1)
{
ArrayList removeArr = new ArrayList();
listItems.SelectedIndex = -1;
//Copy selected items to listItems
foreach (ListItem li in listItemProfiles.Items)
{
if (li.Selected)
{
li.Selected = false;
listItems.Items.Add(li);
removeArr.Add(li);
}
}
//Remove the selected items from listItemProfiles
foreach (ListItem li in removeArr)
{
listItemProfiles.Items.Remove(li);
}
Util.WebFunctions.SortListBox(listItems);
}
}
//BJIT Button
protected void viewBtn_Click(object sender, EventArgs e)
{
if (listCatalogs.SelectedItem != null)
{
listItemCatalogs.DataSource = DAL.Util.getCatalogProfile(listCatalogs.SelectedValue.ToString());
listItemCatalogs.DataBind();
}
}
//BJIT Button
protected void editBtn_Click(object sender, EventArgs e)
{
if (listCatalogs.SelectedItem != null)
{
#region controls visible
lblVendor.Visible = true;
ddlSelectVendor.Visible = true;
lblItemProfile.Visible = true;
lblItems.Visible = true;
listItems.Visible = true;
listItemProfiles.Visible = true;
btnAddCustomer.Visible = true;
btnRemoveCustomer.Visible = true;
lblItemsCatalog.Visible = false;
listItemCatalogs.Visible = false;
//listItemCatalogs.Items.Clear();
#endregion
#region disable controls
//listCatalogs.Enabled = false;
listItemCatalogs.Enabled = false;
editBtn.Enabled = false;
viewBtn.Enabled = false;
#endregion
listItemProfiles.DataSource = DAL.Util.getCatalogProfile(listCatalogs.SelectedValue.ToString());
listItemProfiles.DataBind();
}
}
//BJIT Drop Down List
protected void ddlSelectVendor_Changed(object sender, EventArgs e)
{
listItems.DataSource = DAL.Util.getVenorItems(ddlSelectVendor.SelectedItem.ToString());
listItems.DataBind();
}
#endregion
}
}
With this there is a little more functionality that was not described previously and with this Firefox also does not keep the data / keep the controls visible when the editBtn_Click is initiated.
And again if there is anything i can clear up I will do my best to reword or anything.
Thank you.
I don't exactly know what happened, but it is working now. I rebuilt the .cs page and recompiled the whole site and everything works. Also cleared the cache of all browsers..
I have an aspx page where i dynamically add a radiobuttonlist with OnSelectedIndexChanged event. In the event i check for the selected items. i have 2 items.
For the first item,the event is firing well, However if i choose the other option the event is not firing: below the code..
The event is only firing is i change from "Some provided" to "All provided" the other way it is not working
Adding the RBL:
RadioButtonList dControl_b = new RadioButtonList();
dControl_b.ID = "rbl_MinCriteria";
dControl_b.RepeatDirection = System.Web.UI.WebControls.RepeatDirection.Horizontal;
dControl_b.CssClass = "Font";
dControl_b.Font.Name = "Arial";
dControl_b.Font.Size = 8;
dControl_b.ToolTip = "";
dControl_b.SelectedIndex = -1;
dControl_b.SelectedIndexChanged += new EventHandler(rbl_MinCriteria_SelectedIndexChanged);
dControl_b.AutoPostBack = true;
Checking the selected item:
if(rbl_MinCriteria.SelectedItem.ToString() == "All provided")
{
cbl_MinimumCriteria.Items[0].Selected = true;
cbl_MinimumCriteria.Items[1].Selected = true;
cbl_MinimumCriteria.Items[2].Selected = true;
cbl_MinimumCriteria.Items[3].Selected = true;
cbl_MinimumCriteria.Enabled = false;
//*************************************************************
if (ddl_CountryOccurence.SelectedValue != "Please choose")
{
ddl_CountryOccurence.Enabled = false;
}
else
{
ddl_CountryOccurence.Enabled = true;
}
//*************************************************************
if (tb_DueDate.Text != "")
{
tb_DueDate.Enabled = false;
}
else
{
tb_DueDate.Enabled = true;
}
OtherControlI.Enabled = false;
OtherControlII.Enabled = false;
OtherControlIII.Enabled = false;
}
if (rbl_MinCriteria.SelectedItem.ToString() == "Some provided")
{
cbl_MinimumCriteria.Items[0].Selected = false;
cbl_MinimumCriteria.Items[1].Selected = false;
cbl_MinimumCriteria.Items[2].Selected = false;
cbl_MinimumCriteria.Items[3].Selected = false;
cbl_MinimumCriteria.Enabled = true;
//*************************************************************
if (ddl_CountryOccurence.SelectedValue != "Please choose")
{
ddl_CountryOccurence.Enabled = false;
}
else
{
ddl_CountryOccurence.Enabled = true;
}
//*************************************************************
if (tb_DueDate.Text != "")
{
tb_DueDate.Enabled = false;
}
else
{
tb_DueDate.Enabled = true;
}
OtherControlI.Enabled = false;
OtherControlI.SelectedIndex = -1;
OtherControlII.Enabled = false;
OtherControlII.SelectedIndex = -1;
OtherControlIII.Enabled = false;
OtherControlIII.SelectedIndex = -1;
}
Any help and Comment is much appreciated
This is for people who find this question from Google:
On the RadioButtonList, set the AutoPostBack property to true.
RadioButtonList OnSelectedIndexChanged
I have this problem and solved it.
For raising onselectedindexchanged event of RadioButtonList , check below items:
<asp:RadioButtonList ID="rdlCondition" runat="server" AutoPostBack="True"
onselectedindexchanged="rdlCondition_SelectedIndexChanged">
and in the Page_Load set them with code :
rdlCondition.AutoPostBack = true;
rdlCondition.SelectedIndexChanged += new EventHandler (rdlCondition_SelectedIndexChanged);
Looking at the code above there seems to be alot of code reuse. I reorganized your code a bit (assuming you didnt leave anything out). Keep in mind I never tested it.
protected void rbl_MinCriteria_SelectedIndexChanged(object sender,EventArgs e)
{
if (rbl_MinCriteria.SelectedIndex<0) return; //If nothing is selected then do nothing
OtherControlI.Enabled = false;
OtherControlII.Enabled = false;
OtherControlIII.Enabled = false;
if(rbl_MinCriteria.SelectedItem.ToString() == "All provided")
{
cbl_MinimumCriteria.Items[0].Selected = true;
cbl_MinimumCriteria.Items[1].Selected = true;
cbl_MinimumCriteria.Items[2].Selected = true;
cbl_MinimumCriteria.Items[3].Selected = true;
cbl_MinimumCriteria.Enabled = false;
}
if (rbl_MinCriteria.SelectedItem.ToString() == "Some provided")
{
cbl_MinimumCriteria.Items[0].Selected = false;
cbl_MinimumCriteria.Items[1].Selected = false;
cbl_MinimumCriteria.Items[2].Selected = false;
cbl_MinimumCriteria.Items[3].Selected = false;
cbl_MinimumCriteria.Enabled = true;
OtherControlI.SelectedIndex = -1;
OtherControlII.SelectedIndex = -1;
OtherControlIII.SelectedIndex = -1;
}
//*************************************************************
if (ddl_CountryOccurence.SelectedValue != "Please choose")
{
ddl_CountryOccurence.Enabled = false;
}
else
{
ddl_CountryOccurence.Enabled = true;
}
//*************************************************************
if (tb_DueDate.Text != "")
{
tb_DueDate.Enabled = false;
}
else
{
tb_DueDate.Enabled = true;
}
}
I know this doesn't help your current problem but this is just a suggestion. If you could post the code where your actually adding the values to the list I could help a bit more.
EDIT: Your problem could be your not setting the value of your items, only the text. Try using rbl_MinCriteria.SelectedItem.Text =="All provided" instead.
I've made a sample aspx page, and added one panel in .aspx like below:
<asp:Panel ID="Panel1" runat="server"></asp:Panel>
And in code behind, I've added following code:
protected void Page_Load(object sender, EventArgs e)
{
RadioButtonList dControl_b = new RadioButtonList();
dControl_b.ID = "rbl_MinCriteria";
dControl_b.RepeatDirection = System.Web.UI.WebControls.RepeatDirection.Horizontal;
dControl_b.CssClass = "Font";
dControl_b.Font.Name = "Arial";
dControl_b.Font.Size = 8;
dControl_b.ToolTip = "";
dControl_b.SelectedIndex = -1;
dControl_b.SelectedIndexChanged += new EventHandler(rbl_MinCriteria_SelectedIndexChanged);
dControl_b.AutoPostBack = true;
dControl_b.Items.Add(new ListItem("All provided"));
dControl_b.Items.Add(new ListItem("Some provided"));
Panel1.Controls.Add(dControl_b);
}
protected void rbl_MinCriteria_SelectedIndexChanged(object sender,EventArgs e)
{
RadioButtonList rbl_MinCriteria = (RadioButtonList)Panel1.FindControl("rbl_MinCriteria");
if(rbl_MinCriteria.SelectedItem.ToString() == "All provided")
{
}
if (rbl_MinCriteria.SelectedItem.ToString() == "Some provided")
{
}
}
The event is FIRING EVERY TIME the radio button listitem is changed.
So, I'm afraid, you have done something wrong elsewhere. Good luck.
I have a Dropdownlist (DDL1) when I select any item from this dropdownlist(DDL1), results in creation of another dropdownlist(DDL2), This contains some of the items.When I select other Item from DDL1 , Items will change in DDL2, this happens for the each different item selected in DDL1.
when I select a item from DDL2, label content must be shown, intially I'm making Label invisibe and in the code I changed the visibility to true and added content to it. But the label content is not shown when I select a item from DDL2.
Here is my Code
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "Abe Books")
{
DropDownSeller.Visible = true;
lnkUsdBooks.Visible = true;
lnkUsdBooks.Text = "usedbooks#abe.com";
lnkUsdBooks.NavigateUrl = "mailto:usedbook#abe.com";
DropDownSeller.Visible = true;
DropDownSeller.Items.Remove("Chacha Choudary");
DropDownSeller.Items.Remove("SpiderMan");
DropDownSeller.Items.Remove("Amar chitra Katha");
DropDownSeller.Items.Remove("Chandamama");
DropDownSeller.Items.Remove("Mahabharata");
DropDownSeller.Items.Add("Amar chitra Katha");
DropDownSeller.Items.Add("Chandamama");
DropDownSeller.Items.Add("Mahabharata");
DropDownSeller.DataBind();
if (DropDownSeller.SelectedValue == "Amar chitra Katha")
{
lblPrice.Visible = true;
lblPrice.Text = "$69.99";
}
else if (DropDownSeller.SelectedValue == "Chandamama")
{
lblPrice.Visible = true;
lblPrice.Text = "$59.99";
}
else if (DropDownSeller.SelectedValue == "Mahabharata")
{
lblPrice.Visible = true;
lblPrice.Text = "$49.99";
}
else
{
lblPrice.Visible = false;
}
}
Any ideas on this are appreciated
Thanks,
Remove if (!Page.IsPostBack) from the DropDownList1_SelectedIndexChanged because when the page postbacks this condition will be false. Because your page is posting back to the server that's why it is not visible and not showing.
In short your DropDownList1_SelectedIndexChanged should be like..
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "Abe Books")
{
DropDownSeller.Visible = true;
lnkUsdBooks.Visible = true;
lnkUsdBooks.Text = "usedbooks#abe.com";
lnkUsdBooks.NavigateUrl = "mailto:usedbook#abe.com";
DropDownSeller.Visible = true;
DropDownSeller.Items.Clear(); // it will clear all the items, instead you are removing one by one
DropDownSeller.Items.Add("Amar chitra Katha");
DropDownSeller.Items.Add("Chandamama");
DropDownSeller.Items.Add("Mahabharata");
DropDownSeller.DataBind();
}
protected void DropDownSeller_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownSeller.SelectedValue == "Amar chitra Katha")
{
lblPrice.Visible = true;
lblPrice.Text = "$69.99";
}
else if (DropDownSeller.SelectedValue == "Chandamama")
{
lblPrice.Visible = true;
lblPrice.Text = "$59.99";
}
else if (DropDownSeller.SelectedValue == "Mahabharata")
{
lblPrice.Visible = true;
lblPrice.Text = "$49.99";
}
else
{
lblPrice.Visible = false;
}
}