How to get the selected value of a listbox that is rendered using HtmlTextWriter?http://forums.asp.net/t/1530466.aspx/1?How+to+get+the+selected+value+of+a+listbox+that+is+rendered+using+HtmlTextWriter+Sat, 27 Feb 2010 01:34:00 -050015304663703249http://forums.asp.net/p/1530466/3703249.aspx/1?How+to+get+the+selected+value+of+a+listbox+that+is+rendered+using+HtmlTextWriter+How to get the selected value of a listbox that is rendered using HtmlTextWriter? <p><img src="file:///C:/DOCUME%7E1/RJJ/LOCALS%7E1/Temp/moz-screenshot-1.png"><img src="file:///C:/DOCUME%7E1/RJJ/LOCALS%7E1/Temp/moz-screenshot-2.png"><img src="file:///C:/DOCUME%7E1/RJJ/LOCALS%7E1/Temp/moz-screenshot-3.png">Hi,</p> <p>Im using a web form that contains a button(having id=btnSubmit) dropdownlist(having id='ddlDynamic') and a panel which has a dynamicpopulateextender that makes use of the fllg webservice method</p> <p>[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]<br> &nbsp;&nbsp;&nbsp; public static string GetDynamicContent3(string contextKey)<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DropDownList lsDynamic = new DropDownList();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lsDynamic.ID = &quot;lsDynamic&quot;;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lsDynamic.Items.Add(new ListItem(&quot;Numba1&quot;));<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lsDynamic.Items.Add(new ListItem(&quot;Numba2&quot;));</p> <p><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lsDynamic.Items[1].Selected = true;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*These values are just sample values which will actually be coming from a database</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Even the item is selected based on another database value*/<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lsDynamic.Attributes.Add(&quot;onchange&quot;,&quot;caller(this.value)&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StringWriter sw = new StringWriter();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HtmlTextWriter htw = new HtmlTextWriter(sw);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lsDynamic.RenderControl(htw);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; htw.Close();<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return sw.ToString();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp;&nbsp; }</p> <p>Now at page load this panel will already be containing an empty listbox with id 'lsDynamic'<br> </p> <p>As you can see I am creating a new listbox with a selected item in the panel to replace the old empty one.(I hope my terminology is correct)<br> </p> <p>This web service method kicks into action when i change the index of ddlDynamic<br> </p> <p>The listbox and the selecteditem also gets rendered fine inside the panel but when i click btnSubmit, I am unable to retrieve the selected index of the listbox</p> <p>It always returns Null. Is there something else i need to do in order to properly register the new control I have added so that server side events can handle it</p> <p>properly.<br> </p> <p>Do help me out in this regard.</p> <p>Let me know if you guys need any more info which would help you solve my problem<br> </p> <p>Kindly forgive me if I ve forgotten to do something very obvious because as i said 'Im new to Ajax'</p> <p>Regards</p> <p>RonJac<br> </p> <p><br> </p> 2010-02-26T17:58:38-05:003703419http://forums.asp.net/p/1530466/3703419.aspx/1?Re+How+to+get+the+selected+value+of+a+listbox+that+is+rendered+using+HtmlTextWriter+Re: How to get the selected value of a listbox that is rendered using HtmlTextWriter? <p>It is probably easier to just put an update panel around the dropdownlist and do the database stuff on partial postback. I think it is returning null because it is being changed client-side.<br> </p> 2010-02-26T19:27:46-05:003703804http://forums.asp.net/p/1530466/3703804.aspx/1?Re+How+to+get+the+selected+value+of+a+listbox+that+is+rendered+using+HtmlTextWriter+Re: How to get the selected value of a listbox that is rendered using HtmlTextWriter? <p>Ok fine. I ll drill down my question like this.</p> <p>Using dynamicpopulateextender on asp textboxes to make them populate on the OnChange event of a combobox works fine and I am able to retrieve the values on btnSubmit_Click.</p> <p>Similarly is there any way to select the correct listbox value (the listbox already has values from which i only need the dynamicpopulateextender to SELECT the correct value based on database calculations). I hope i dint confuse you now.</p> <p>Yea the update panel thing will work Im sure of it. But then i gotta encompass the other forms too in an update panel and I have to remove the dynamic populate extender and the webservice methods from all the controls in subsequent form.</p> <p>Sorry If im askin a bit too much but is there any way to dynamically select a listboxitem based on a combobox value(via database). Any extender would be helpful.</p> <p>Even a radiobuttonlist instead of a listbox would be fine. I just need the for user to be able to see the correct option that is selected and he should be able to modify the value by changing the selected item of the listbox (or radiobuttonlist or whatever control works out) and the modified value ought to get updated in the database when i click the submit buton.</p> <p>Sorry if my previous question was not so clear.</p> <p><br> </p> <p><br> </p> <p><br> </p> 2010-02-27T01:34:00-05:00