| | |
regarding drpo down list
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
Try this without a postback. This works with javascript to do it on the client, not on the server:
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.
ASP.NET Syntax (Toggle Plain Text)
<asp:DropDownList ID="ddldept" onchange="chkChange(this);" runat="server" Style="z-index: 131; left: 242px; position: absolute; top: 153px"> <asp:ListItem>Solutions</asp:ListItem> <asp:ListItem>SA</asp:ListItem> <asp:ListItem>Training</asp:ListItem> <asp:ListItem>HR</asp:ListItem> <asp:ListItem>Finance</asp:ListItem> <asp:ListItem>Documentation</asp:ListItem> <asp:ListItem>Quality</asp:ListItem> <asp:ListItem>NewDepartment</asp:ListItem> </asp:DropDownList> <asp:TextBox ID="TextBox1" runat="server" style="display: none;" /> <script type="text/javascript"> function chkChange(ddl) { var textbox = document.getElementById("<%= TextBox1.ClientID %>"); if (ddl.options[ddl.selectedIndex].value == "NewDepartment") { textbox.style.display = "block"; } else { textbox.style.display = "none"; } } </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.
I answer pm's.
I answer questions.
I answer quickly.
I answer.
I answer questions.
I answer quickly.
I answer.
•
•
Join Date: Dec 2008
Posts: 104
Reputation:
Solved Threads: 18
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.
"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.
•
•
Join Date: Dec 2008
Posts: 37
Reputation:
Solved Threads: 0
Here is my code:
ASP.NET Syntax (Toggle Plain Text)
<asp:DropDownList ID="ddldept" OnChange="chkChange(this);" runat="server" Style="z-index: 118; left: 242px; position: absolute; top: 153px"> <asp:ListItem>Solutions</asp:ListItem> <asp:ListItem>SA</asp:ListItem> <asp:ListItem>Training</asp:ListItem> <asp:ListItem>HR</asp:ListItem> <asp:ListItem>Finance</asp:ListItem> <asp:ListItem>Documentation</asp:ListItem> <asp:ListItem>Quality</asp:ListItem> <asp:ListItem>NewDepartment</asp:ListItem> </asp:DropDownList> <asp:TextBox ID="txtdept" runat="server" Style="z-index: 127; left: 361px; position: absolute; top: 153px" Width="113px" Visible="False"></asp:TextBox> <script type="text/javascript"> function chkChange(ddl) { var textbox = document.getElementById("<%= txtdept.ClientID %>"); if (ddl.options[ddl.selectedIndex].value == "NewDepartment") { textbox.style.display = "block"; } else { textbox.style.display = "none"; } } </script> <asp:TextBox ID="txtdesig" runat="server" Style="z-index: 119; left: 590px; position: absolute; top: 149px"></asp:TextBox>
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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.
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.
I answer pm's.
I answer questions.
I answer quickly.
I answer.
I answer questions.
I answer quickly.
I answer.
•
•
Join Date: Dec 2008
Posts: 37
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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)
document.getElementById("<%= ddldept.ClientID %>").options.add(new Option(index, "value", "text"));
Last edited by SheSaidImaPregy; Jan 1st, 2009 at 5:47 pm.
I answer pm's.
I answer questions.
I answer quickly.
I answer.
I answer questions.
I answer quickly.
I answer.
![]() |
Other Threads in the ASP.NET Forum
- Previous Thread: How to get an excel file while button click.
- Next Thread: Custom class object question
| Thread Tools | Search this Thread |
.net activexcontrol advice ajax alltypeofvideos appliances asp asp.net bc30451 beginner bottomasp.net box browser button c# cac checkbox click commonfunctions control css dataaccesslayer database datagridview datagridviewcheckbox datalist deadlock deployment development dgv dialog dropdownlist dynamic dynamically edit embeddingactivexcontrol expose fileuploader fill findcontrol flash formatdecimal formview gridview gudi iframe iis javascript listbox login microsoft mono mouse mssql multistepregistration news novell numerical objects opera panelmasterpagebuttoncontrols radio redirect registration relationaldatabases reportemail rotatepage save schoolproject search security sessionvariables silverlight smartcard smoobjects software sql-server sqlserver2005 ssl suse textbox tracking treeview unauthorized validatedate validation vb.net video videos virtualdirectory vista visualstudio web webapplications webdevelopemnt webdevelopment webprogramming webservice xsl youareanotmemberofthedebuggerusers






