Click here to Skip to main content
14,384,769 members
Rate this:
Please Sign up or sign in to vote.
See more:
i write javascript function
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
<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
protected void Page_Load(object sender, EventArgs e)
{
   ddshpe.Items.FindByText("Cylindrical").Selected = true;
}


tabulation corrected
Posted
Updated 5-May-14 6: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
Rate this:
Please Sign up or sign in to vote.

Solution 3

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'...
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

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
   
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
Sanket Saxena 5-May-14 8:55am
   
press F12 you will see the window open in your browser. There are all the links, console , script etc. use whatever you want to. :)
King Fisher 5-May-14 9:06am
   
Thank you ;)
Sanket Saxena 5-May-14 9:10am
   
welcome again buddy :)
   
I used Visual Studio as it; tried something else. You can debug it...
—SA

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100