c# - Wrong ClientID for dynamically added child control -


i trying rework asp.net usercontrol .ascx markup webcontrol(only .cs).

in my_new_webcontrol.cs have:

private readonly dropdownlist _source = new dropdownlist();  protected override void oninit(eventargs e) {     base.oninit(e);     _source.id = string.format("{0}{1}{2}", id, idseparator, "source");     controls.add(_source); }  protected override void render(htmltextwriter writer) {     rendercontents(writer); }  protected override void rendercontents(htmltextwriter writer) {     _source.rendercontrol(writer); } 

problem generates dropdownlist id="maincontent_combobox1$source" , name="ctl00$maincontent$combobox1$source". name generated expected id wrong , here should _ instead of $.

how can achieve id="maincontent_combobox1_source" , name="ctl00$maincontent$combobox1$source".

update 1.

tried put dropdownlist panel knaģis suggested:

private readonly dropdownlist _source = new dropdownlist(); private readonly panel _panel = new panel();  protected override void oninit(eventargs e) {     base.oninit(e);     _panel.id = id;     _panel.controls.add(_source);     controls.add(_panel); }  protected override void render(htmltextwriter writer) {     rendercontents(writer); }  protected override void rendercontents(htmltextwriter writer) {     _panel.rendercontrol(writer); } 

no success. generated html(combobox1 in id , name missing):

<div id="maincontent_combobox1">     <select name="ctl00$maincontent$source" onchange="javascript:settimeout('__dopostback(\'ctl00$maincontent$source\',\'\')', 0)" id="maincontent_source"></select>     </div> 

answer

as knaģis suggested missed inamingcontainer in server control class declaration

public class combobox: webcontrol, inamingcontainer 

you should add child panel (or container control) id of combobox1 , within add dropdownlist id of source. let asp.net generate hierarchical identifiers.

in short - if want preserve values of clientid , name reworked control, should use exact same control tree .ascx markup.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -

matlab - How to equate a structure array to structure array -