Hi,
I have a scenario like this..
In a page I have many dropdown and a single save button.Supposing if user changes 3 values of dropdown,and then he clicks "save button".
1.A dialog box should appear which confirm that he has changed the value of three dropdowns.
How should I achieve this problem?Help me with the code..
Thanks
Loading
KritiPosted Jan 7, 2010, 3:19 AM
Nilanka DharmadasaPosted Jan 7, 2010, 2:04 AM
I changed my code to support a web application.
Use it.
private int numberofchangeddropdown = 0;
private System.Collections.ArrayList dropdowns = new System.Collections.ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
foreach (Control c in this.Controls)
{
if (c is DropDownList)
{
DropDownList obj = (DropDownList)c;
obj.SelectedIndexChanged += new System.EventHandler(this.Dropdownlist_SelectedIndexChanged);
//You bind the same event to all the dropdowns.
}
}
}
protected void Dropdownlist_SelectedIndexChanged(object sender, EventArgs e)
{
//You check whether you already counted that dropdown. Otherwise if you change one dropdown two times it will count 2 which is not correct.
if (!dropdowns.Contains(sender))
{
dropdowns.Add(sender);
numberofchangeddropdown++;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Page.RegisterClientScriptBlock("msg", "");
}
Santhosh NPosted Jan 7, 2010, 2:00 AM
KritiPosted Jan 7, 2010, 1:57 AM
I dont know the total number of drop-down to bind it in the code..Its dynamic..A page retrieves the number of drpdown according to some case..
Nilanka DharmadasaPosted Jan 7, 2010, 1:49 AM
My code works for any number of dropdowns. You can use it. But please do not forget to tick Do you like this answer checkbox.
In my code you have only one event and you bind all the dropdowns to that event.
If you download the code from my previous reply, it will be easier for you.
If you are using a web application, replace ComboBox with DropDownList. There will be no other difference.
private int numberofchangeddropdown = 0;
private ArrayList dropdowns = new ArrayList();
public Form1()
{
InitializeComponent();
foreach (Control c in this.Controls)
{
if(c is ComboBox)
{
ComboBox obj = (ComboBox)c;
obj.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
//You bind the same event to all the dropdowns.
}
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("You changed "+ numberofchangeddropdown.ToString() + " dropdowns !");
}
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
//You check whether you already counted that dropdown. Otherwise if you change one dropdown two times it will count 2 which is not correct.
if (!dropdowns.Contains(sender))
{
dropdowns.Add(sender);
numberofchangeddropdown++;
}
}
Santhosh NPosted Jan 7, 2010, 1:42 AM
foreach (Control c in this.form1.Controls)
if (c.GetType().Name == "DropDownList")
if (((DropDownList)c).SelectedIndex != 0)
cnt += 1;
Page.RegisterClientScriptBlock("msg", "");
cnt = 0;
KritiPosted Jan 7, 2010, 1:29 AM
Nilanka DharmadasaPosted Jan 7, 2010, 1:27 AM
It's not difficult.
I wrote a small application for you. Download it and check. It supports any number of dropdowns.
If my answer helps you, please do not forget to tick 'Do you like this answer' checkbox.
Santhosh NPosted Jan 7, 2010, 1:20 AM
static int cnt = 0;
protected void Page_Load(object sender, EventArgs e)
{
//your page load code..
}
protected void Dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
cnt += 1;
}
protected void Dropdownlist2_SelectedIndexChanged(object sender, EventArgs e)
{
cnt += 1;
}
protected void Dropdownlist3_SelectedIndexChanged(object sender, EventArgs e)
{
cnt += 1;
}
protected void Button1_Click(object sender, EventArgs e)
{
Page.RegisterClientScriptBlock("msg", "");
}
and this can be customized properly to check if the dropdown is clicked twice by having a seperate variable for each dropdown and check in the button click..
KritiPosted Jan 7, 2010, 12:12 AM
Thanks
Hiren SoniPosted Jan 6, 2010, 12:27 PM