Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User control problem

Status
Not open for further replies.

beral

Programmer
Jan 7, 2004
12
SE
Hello,

I have a usercontrol in which I have a dropdowlist which is charged dynamically with data.
I want to use this control in a aspx page ,When I use it in the aspx page , on the clic button of my form,I can't recover the selected item.It returns nothing when I do this:
Response.Write(Request.Params.Get("myUserControl"))

I do have dataValueField in the dropdownlist.

can anyone help me please??

Thanks
 
I think that since you are creating the Drop Down List dynamically, you cannot access it's post-back value by it's ID (from the Request.Form collection). If you enable Trace on the page and submit the form, you will see the values you want to capture, but their names will be something like ctrl_0:ctl_1:myUserControl, which is the name you will need to use when you try to access it in the Request.Form collection. Something like Request.Form("ctrl_0:ctl_1:myUserControl"). Obviously, most of that will vary depending on where the control is placed, so one solution is to loop through each item in the Request.Form collection, split it's name into a string array (on the ":" character), and get the last element - which will be the ID you gave your control. Then, if it's the right item, you can get it's value.

I've done it that way a couple of times - maybe there is a better way.

Do you need a C# example of what I have described?

David
[pipe]
 
Yes Please could you send me some?
 
Being a user control , does that control expose some properties , methods using which it gets you the selected value in the dropdownlist box.

This property could be easily accessed at the server side
on the postback due to the button click.

Parchure Nikhil
 
Since the the control is created only during the first page load, and not during postback, the only way I know of getting the selected value is to grab it from the forms collection. But since asp.net names the controls according to their places on the control tree, you can't just access it by what you think it's named. So, one hack is to loop through all the posted keys until you get the one you want.

Code:
string selectedValue;

foreach(string param in Request.Form.AllKeys){
    
    if(param.IndexOf("MyDropDownList") != -1){
        
        selectedValue = Request.Form[param].ToString(); 
        break; 
    }
}

Never mind about that splitting into array stuff - (I was thinking of a something else :))

HTH

David
[pipe]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top