C# .NET - How to get combo Box value from usercontrol?

Asked By Eswaran Radhakrishnan on 30-Jul-08 02:14 AM

Hi all,

I have an usercontrol that contains several controls including combobox shows colors. I have added this user control to my windows form. I have a button in my form. If I click the button I need to create another usercontrol in the same form and displayed beneth of exisiting one.

I may choose a red color in the first user control and green color from second combobox in the usercontrol. I need to store both values in the database table.

How to reterive data's from combobox from usercontrol.


R. Eswaran

use properties inside user control - Santhosh N replied to Eswaran Radhakrishnan on 30-Jul-08 02:27 AM

Expose the values that are required in the form at user control level by creating properties for those...

To elaborate, create a property in the user control to return and set the selected value of the combo inside it...

You can make use of this property in the form where user control is placed on its name...

Use FindControl - Sagar P replied to Eswaran Radhakrishnan on 30-Jul-08 02:58 AM

If you want to access any property of any child control then you have to use FindControl for that.


do it something like this. or just workaround it. It will solve your prob.
Try this.

Best Luck!!!!!!!!!!!!!!!

u can get value of control in javascript - Web Star replied to Eswaran Radhakrishnan on 30-Jul-08 08:03 AM

Here is  a javascript function to get the clientId of a control

 u  can get value of control in javascript and store where u wnat
function GetClientId(strid)
     var count=document.forms [ 0 ] . length ;
     var i = 0 ;
     var eleName;
     for (i = 0 ;  i < count ;  i++ )
       eleName = document.forms [ 0 ] . elements [ i ] .id;
       pos=eleName.indexOf ( strid ) ;
       if(pos >= 0)  break;          
    return eleName.Value;

Just pass the orginal id of text box into this control and you will get the ClientId back.
eg:-- checking if a textbox with id txtName is empty or not

get the value of the combo box in javascript and display - alice johnson replied to Eswaran Radhakrishnan on 30-Jul-08 02:21 PM

function showValue()
<Select id=myCboBox>
<option value="1">One</option>
<option value="2">two</option>
<option value="3">Three</option>

<input type="button" onclick="showValue();" value="show"/>

<div id=divShow></div>
Solved... - Atul Shinde replied to Eswaran Radhakrishnan on 01-Aug-08 02:01 AM
You have to create object of that UserControl. Then use find control method of that user control and then put the combobox name in it.