Click here to Skip to main content
16,020,519 members
Please Sign up or sign in to vote.
2.87/5 (4 votes)
See more:
i write javascript function
JavaScript
function Shape()
{
    var lbl1=document.getElementById("<%= Label3.ClientID%>");   
    var ddshap=document.getElementById("<%= ddshpe.ClientID%>");
    if(ddshap.value=='Rectangular')
    { 
       lbl1.innerHTML='Length of sample in cm';
       document.getElementById("rectangular").style.display = '';
    }
    else
    {
       lbl1.innerHTML='Diameter of sample in cm';
       document.getElementById("rectangular").style.display = 'none';
    }
}

and set it to dropdowanlist
C#
<asp:DropDownList ID="ddshpe" runat="server" Height="22px" onchange="Shape();" 
CssClass="Textbox">
<asp:ListItem>Rectangular</asp:ListItem>
<asp:ListItem>Cylindrical</asp:ListItem>
</asp:DropDownList>

it work fine after browse and dropdowan iteam change but when i select iteam from code its not working
C#
protected void Page_Load(object sender, EventArgs e)
{
   ddshpe.Items.FindByText("Cylindrical").Selected = true;
}


tabulation corrected
Posted
Updated 5-May-14 5:07am
v3
Comments
Kornfeld Eliyahu Peter 5-May-14 7:40am    
Can you open debug window in the browser after it's 'not working' and see if there is a message for you?
Kornfeld Eliyahu Peter 5-May-14 7:40am    
I copied you code into a VS project and it's seems to work fine...Maybe there is an other part of your code where the problem is?
dhiraj mane 5-May-14 7:48am    
i select "Cylindrical" from ddshape at page load...because i select it then document.getElementById("rectangular").style.display = 'none'; this control in displayed which is wrong...java script function is not working when i DropDownList select change @ page load

Selecting the default value on the server side will not affect the client side. Switch forth back the selection at the client and you will see that the JavaScript code is just fine...
You have to synchronize your display with your original selection, for that add a line to hide 'rectangular' in the Page_Load method...
Do NOT use 'rectangular'.Visible = false, that will prevent the rendering of 'rectangular'...
 
Share this answer
 
Replace '==' with '='. Your '==' would throw interpreter's exception which propagates and id silently handled in the browser, not allowing to interpret next line. Run your Javascript code under the debugger, check up id values. You can also view the resulting HTML and Javascript code in browser, by looking at "View Page Source" (or something like that), and, in particular, compare id values in script and HTML.

—SA
 
Share this answer
 
Comments
King Fisher 5-May-14 8:13am    
hi sir,
How do i Debug Javascript and Jquery Scripts? Do i need any tool ?

Thanks.
Sanket Saxena 5-May-14 8:44am    
Use firebug to debug your script and check console if any error came.
King Fisher 5-May-14 8:48am    
thank you,
Sanket Saxena 5-May-14 8:50am    
Welcome :)
King Fisher 5-May-14 8:53am    
i have that but how do i handle that ? any Reference Link

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900