943,740 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 1777
  • ASP.NET RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 31st, 2008
0

Re: regarding drpo down list

Try this without a postback. This works with javascript to do it on the client, not on the server:

ASP.NET Syntax (Toggle Plain Text)
  1. <asp:DropDownList ID="ddldept" onchange="chkChange(this);" runat="server" Style="z-index: 131; left: 242px; position: absolute; top: 153px">
  2. <asp:ListItem>Solutions</asp:ListItem>
  3. <asp:ListItem>SA</asp:ListItem>
  4. <asp:ListItem>Training</asp:ListItem>
  5. <asp:ListItem>HR</asp:ListItem>
  6. <asp:ListItem>Finance</asp:ListItem>
  7. <asp:ListItem>Documentation</asp:ListItem>
  8. <asp:ListItem>Quality</asp:ListItem>
  9. <asp:ListItem>NewDepartment</asp:ListItem>
  10. </asp:DropDownList>
  11. <asp:TextBox ID="TextBox1" runat="server" style="display: none;" />
  12. <script type="text/javascript">
  13. function chkChange(ddl)
  14. {
  15. var textbox = document.getElementById("<%= TextBox1.ClientID %>");
  16.  
  17. if (ddl.options[ddl.selectedIndex].value == "NewDepartment")
  18. {
  19. textbox.style.display = "block";
  20. }
  21. else
  22. {
  23. textbox.style.display = "none";
  24. }
  25. }
  26. </script>

By changing the options, it will do a check to see if the value selected is "NewDepartment". If it is, the textbox will be shown. if not, it will be hidden.
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Jan 1st, 2009
0

Re: regarding drpo down list

Happy New Year 2009!!!!!!!!!!!!!!!
Thanks for your reply to my query but if i am using this code it is giving me an error at onchange=chkChange(this) saying that it(onchange()) is not a valid attribute can you suggest me any changes?

Thanks in advance,
Karthik.
Reputation Points: 10
Solved Threads: 0
Light Poster
kodingkarthik is offline Offline
37 posts
since Dec 2008
Jan 1st, 2009
0

Re: regarding drpo down list

Happy New Year 2009

"onchange() is not a valid attribute" is just a warning message. That's because you're using a webcontrol, which doesn't have an onchange
method. Visual Studio is letting you know that OnChange isn't a valid event for the DropDownList control. However, it is a valid event for an HTML input element and will be passed through to the rendered HTML and should work as expected. But if the warning bothers you, you can use ddldept.Attributes.Add("onchange", "chkChange(this);") in your code behind page load event , to set the client side attribute (and delete the onchange attrbute from your source).

In short the above code (of SheSaidImaPregy) will work despite the warnings that you metioned.
Last edited by Aneesh_Argent; Jan 1st, 2009 at 3:07 am.
Reputation Points: 16
Solved Threads: 18
Junior Poster
Aneesh_Argent is offline Offline
104 posts
since Dec 2008
Jan 1st, 2009
0

Re: regarding drpo down list

Thanks for your help but i am getting an IE Script error window saying tht would you like to continue with Yes/no Buttons
Reputation Points: 10
Solved Threads: 0
Light Poster
kodingkarthik is offline Offline
37 posts
since Dec 2008
Jan 1st, 2009
0

Re: regarding drpo down list

Can u post ur complete code(.aspx). Or is it exactly the same as above(I suppose not coz its working fine for me in IE and FF)
Reputation Points: 16
Solved Threads: 18
Junior Poster
Aneesh_Argent is offline Offline
104 posts
since Dec 2008
Jan 1st, 2009
0

Re: regarding drpo down list

Here is my code:
ASP.NET Syntax (Toggle Plain Text)
  1. <asp:DropDownList ID="ddldept" OnChange="chkChange(this);" runat="server" Style="z-index: 118; left: 242px; position: absolute;
  2. top: 153px">
  3. <asp:ListItem>Solutions</asp:ListItem>
  4. <asp:ListItem>SA</asp:ListItem>
  5. <asp:ListItem>Training</asp:ListItem>
  6. <asp:ListItem>HR</asp:ListItem>
  7. <asp:ListItem>Finance</asp:ListItem>
  8. <asp:ListItem>Documentation</asp:ListItem>
  9. <asp:ListItem>Quality</asp:ListItem>
  10. <asp:ListItem>NewDepartment</asp:ListItem>
  11. </asp:DropDownList>
  12. <asp:TextBox ID="txtdept" runat="server" Style="z-index: 127; left: 361px; position: absolute;
  13. top: 153px" Width="113px" Visible="False"></asp:TextBox>
  14. <script type="text/javascript">
  15. function chkChange(ddl)
  16. {
  17. var textbox = document.getElementById("<%= txtdept.ClientID %>");
  18. if (ddl.options[ddl.selectedIndex].value == "NewDepartment")
  19. {
  20. textbox.style.display = "block";
  21. }
  22. else
  23. {
  24. textbox.style.display = "none";
  25. }
  26. }
  27. </script>
  28. <asp:TextBox ID="txtdesig" runat="server" Style="z-index: 119; left: 590px; position: absolute;
  29. top: 149px"></asp:TextBox>
Reputation Points: 10
Solved Threads: 0
Light Poster
kodingkarthik is offline Offline
37 posts
since Dec 2008
Jan 1st, 2009
0

Re: regarding drpo down list

Ok just replace your textbox with this


<asp:TextBox ID="txtdept" runat="server" Style="z-index: 127; left: 361px; position: absolute;
top: 153px; display:none" Width="113px"></asp:TextBox>
Reputation Points: 16
Solved Threads: 18
Junior Poster
Aneesh_Argent is offline Offline
104 posts
since Dec 2008
Jan 1st, 2009
0

Re: regarding drpo down list

Aneesh_Argent is right, you need to replace the Textbox, or remove the "Visible='False'" property. The one thing you will need to learn about the visibility property of these web controls is that if it is "False", it will not be rendered on the page. You were receiving an error because your textbox didn't exist on the client's computer (browser), since it was never rendered from the server. By removing the Visible="False", and adding in your Style attribute the "display: none;", you will have the desired output, and would not require a postback to the server which will save time for a simple task.

Also, by coding this way, you will be seeing a lot of "warning" messages. If you wish to avoid the warning messages, use Aneesh's suggestion and manually attach the event from the code-behind, or inline if that is what you are using. (Code-behind has the code file associtated with it: default.aspx --> default.aspx.cs || Inline coding has the code on your same page: default.aspx). Attach the event just how he suggested. You will avoid these warnings, but it's a lot of extra code and can be more difficult to change in the future as it brings the design and business layers together.
Last edited by SheSaidImaPregy; Jan 1st, 2009 at 10:29 am.
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Jan 1st, 2009
0

Re: regarding drpo down list

Thanks to both of you but if i do like that i am not getting my desired o/p
My requirement is
If the user selects NewDepartment in the combobox then only the text box should be visible until then it is should be hidden and later when the textbox is displayed then the cursor should be in the textbox. if the user enters a value into the textbox it must be captured and populated into the combo box dynamically and also for that particular employee the value for the newdepartment must be saved into the database.

thanks for u all,
Karthik.
Reputation Points: 10
Solved Threads: 0
Light Poster
kodingkarthik is offline Offline
37 posts
since Dec 2008
Jan 1st, 2009
0

Re: regarding drpo down list

In javascript (the script tags below your dropdownlist), you can set focus to an element by first grabbing the element (document.getElementById("id")) and applying ".focus()". Becareful of this, it will throw an error if your element trying to be focused doesn't exist or is hidden. To add an option to the dropdownlist, you first grab it like above, then add the attributes ".options" and then ".add". An example is below:

javascript Syntax (Toggle Plain Text)
  1. document.getElementById("<%= ddldept.ClientID %>").options.add(new Option(index, "value", "text"));
Last edited by SheSaidImaPregy; Jan 1st, 2009 at 5:47 pm.
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: How to get an excel file while button click.
Next Thread in ASP.NET Forum Timeline: Custom class object question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC