On each button click, on selection of the dropdownlist item, get the count of selected listitem and put it in a textbox.
Output:
Rajesh: 2(selected 2 times on button click)
Vinay : 5(selected 5 times)
On each button click, on selection of the dropdownlist item, get the count of selected listitem and put it in a textbox.
Output:
Rajesh: 2(selected 2 times on button click)
Vinay : 5(selected 5 times)
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
ramya bharathRank #3,780 · 391 reputationPosted May 29, 2015, 11:55 AM EDT
On each select you have to check which value has been selected and on button click display the count of total selection.
Steps to be followed.
1. Create a public hash table in your code. Either in server side or client side anything is fine
Am providing an example in JavaScript since c# you can create dictionary objects which is easier
Code 1
var myArray = {Rajesh:0,Vinay:0}:
2. On selection change create a event to check what selection it is and update the value of it
Code 2:
myArray[selectedvalue] = myArray[selectedvalue]+1;
3. On button click call a event to loop through all the elements of the array and append the value in text box
for(key in myArray) {
Displaytextbox.text=Displaytextbox.text+key+":"+myArray[key];
}
Refer this on how to handle associated array in JavaScript http://blog.xkoder.com/2008/07/10/javascript-associative-arrays-demystified/
Refer this on how to do it using dictionary in server side http://www.dotnetperls.com/dictionary
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted May 29, 2015, 3:36 PM EDT
protected void Page_Load(object sender, EventArgs e)
{
// Load_Data();
if (!IsPostBack)
{
Dictionary.Add("Rajesh", 0);
Dictionary.Add("Vinay", 0);
Dictionary.Add("Hari", 0);
Dictionary.Add("Anand", 0);
}
}
{
if (DropDownList1.SelectedValue == "Rajesh")
Dictionary["Rajesh"] = Dictionary["Rajesh"] + 1;
else if (DropDownList1.SelectedValue == "Vinay")
Dictionary["Vinay"] = Dictionary["Vinay"] + 1;
else if (DropDownList1.SelectedValue == "Hari")
Dictionary["Hari"] = Dictionary["Hari"] + 1;
else if (DropDownList1.SelectedValue == "Anand")
Dictionary["Anand"] = Dictionary["Anand"] + 1;
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (KeyValuePair
{
Response.Write(inr.Key + "= " + inr.Value);
}
Dictionary["Rajesh"] = 0; // here reset the each dictionary value //
Dictionary["Vinay"] = 0; // you can avoid this code if you don't require //
Dictionary["Hari"] = 0;
Dictionary["Anand"] = 0;
}
}
Nanhe SiddiqueRank #1,103 · 1.7K reputationPosted May 29, 2015, 8:57 PM EDT
d developer13 reputationPosted Jun 3, 2015, 9:32 AM EDT
ramya bharathRank #3,780 · 391 reputationPosted Jun 3, 2015, 6:45 PM EDT
I.e on page load create the dictionary
On selection change if rajesh is selected add key as Rajesh vand value as 1.
On selection of vinay add similarly in the dictionary.