javascript - How to call 'onchange' event of asp.net listbox after its codebehind function is called -
i have listbox defined as:
<asp:listbox id="lst_agenda" onchange="drawchart()" onselectedindexchanged="lst_agenda_selectedindexchanged" selectionmode="multiple" cssclass="txt12 mylistbox" autopostback="true" datatextfield="name" runat="server" width="100%" height="100%" />
after select items in this, lst_agenda_selectedindexchanged method of codebehind called calls other listboxes populated in cascading way. there function in javascript named drawchart generates chart based on values selected in these listboxes.
hence want codebehind function should called before javascript function call on listbox changed event. happening opposite, javascript function drawchart gets called first without filling values in listboxes.
i tried calling javascript function codebehind method directly using:
scriptmanager.registerclientscriptblock(this, this.gettype(), "myscript", "drawchart()", true);
but using gives me null values 'document.getelementbyid' call.
the codebehind function , js call as:
protected void lst_agenda_selectedindexchanged(object sender, eventargs e) { lst_agenda_changed(); foreach (listitem li in lst_subagenda.items) li.selected = true; foreach (listitem li in lst_doctors.items) li.selected = true; lst_subagenda_changed(); lst_group_changed(); //scriptmanager.registerclientscriptblock(this, this.gettype(), "myscript", "drawchart()", true); }
the javascript drawchart as:
function drawchart() { var tempdatetoweek = ""; var tempdatetoyear = ""; var dateofweek = new date(); tempdatetoweek = document.getelementbyid('txtrangeto').value.substring(0, 2); tempdatetoyear = document.getelementbyid('txtrangeto').value.substring(6, 10); ...........
i getting error in drawchart if call codebehind @ line fill variable tempdatetoweek value of document.getelementbyid('txtrangeto').
so how can achieve this?
you should call registerstartupscript
instead of registerclientscriptblock
in event handler in code-behind. cause javascript code executed once page has loaded after postback, dom elements accessible document.getelementbyid
.
protected void lst_agenda_selectedindexchanged(object sender, eventargs e) { lst_agenda_changed(); foreach (listitem li in lst_subagenda.items) li.selected = true; foreach (listitem li in lst_doctors.items) li.selected = true; lst_subagenda_changed(); lst_group_changed(); scriptmanager.registerstartupscript(this, gettype(), "drawchart", "drawchart();", true); }
the onchange
event handler should removed dropdownlist avoid calling client code before postback.
Comments
Post a Comment