|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
VBScript - Discussion - Listbox_SelectedIndexChanged event no effects
hi all,
i'm using asp.net with a listbox for user to do selection for sqlQuery, now i met a problem that i want select an item from the list and if the item is contains "SUB" in string then i want visible another listbox in vb code lstbox_SelectedIndexChanged. but when i debugging, i select the list box item but no effect to the selectedIndexChanged event, so i cant make my another listbox appear if met my condition. if i enable "AutoPostBack" then the item i select will be refreshed. so how can i do in this situation? thanks for help in advanced |
|
#2
|
||||
|
||||
|
you have two common options: either put your drop down inside UpdatePanel
then using AutoPostBack won't reload the whole page, or more simple but less elegant solution is using client side code to show the other list box. let me know your decision, can't help with UpdatePanel since I never personally work with these but if you choose client side solution I can give some code. |
|
#3
|
|||
|
|||
|
i am using listbox but dun know have same action as dropdown or not. regards the updatePanel, is it a simple panel with name UpdatePanel or there a special UpdatePanel? i prefer to use clientside code, that will be faster right? so can u guide me some code?
|
|
#4
|
||||
|
||||
|
Quote:
You'll have to pass the Client ID of the listbox you wish to hide to the client side code, then override the "onchange" of the first listbox: Code:
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterClientScriptBlock( Page.GetType(), "listbox_id", string.Format("var _listBoxId = '{0}'; ", OtherListBox.ClientID), true );
lstBox.Attributes["onchange"] = "ListBoxChanged(this);";
}
(this is C#, it should be simple to translate to VB.NET - replace OtherListBox with the name of the drop down list you wish to hide) Having this, add this client side code to your .aspx page to make it work: Code:
<script type="text/javascript">
function ListBoxChanged(oListbox)
{
var otherListbox = document.getElementById(_listBoxId);
if (oListbox.value.indexOf("SUB") >= 0)
{
otherListbox.style.display = "none";
}
else
{
otherListbox.style.display = "";
}
}
</script>
That's it... please read the code and try to understand how it works, I'm sure if you spend some time you'll figure the logic behind it and it will help you solve problems that might occur with the code. Let me know how it goes! ![]() Last edited by Shadow Wizard : October 23rd, 2009 at 06:50 AM. |
|
#5
|
|||
|
|||
|
hi, thanks reply.
when i apply to the code Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Page.ClientScript.RegisterClientScriptBlock(Page.G etType(), "listbox_id", String.Format("var _listBoxId = '{0}'; ", lstSubAcute.ClientID), True) lstDiag.Attributes.Add("onchange", "ListBoxChanged(this);return false") End Sub where lstDiag is the main listbox to decide show/hide second listbox lstSubAcute is the second listbox to show/hide but when i test run, the page all disappear. so i think i passing wrong thing to the codes. can suggest me?? |
|
#6
|
||||
|
||||
|
you probably have AutoPostBack set as True for your drop down.
Please change this to be False and changing the value won't "make the page disappear", as you call it. |
|
#7
|
|||
|
|||
|
hi, i have checked, sorry i too blur, accidentally set form visible to false.. hahah .. another things is, u mention the drop down is same as list box i mention?? thanks for help me to debug.
and now, the second listbox is showing directly, then i want to do like this. i have 3 items in 1st list box, when i select 3rd items then just show the second listbox, if i change to select 1 or 2 item, then second listbox is hide. how do code it in listbox selectionChanged? from your code: oListbox.value.indexOf("SUB") >= 0 indexOf("SUB") mean?? >=0 mean?? thanks |
|
#8
|
|||
|
|||
|
Hi, thanks, is function already.
and i also understand the meaning that i ask in previous post. it functions as what i want. and now i need to let the second listbox invisible at begining cos my 1st item in 1st listbox is to hide the second listbox. but i set 1st listbox.selectedIndex to 0 but it wont run the javascript to hide second listbox. then i try set visible of 2nd listbox to false at development, then error occur in javascript cos cant find the control(2nd listbox). Any suggestion to this?? thanks "shadow" for helping ![]() |
|
#9
|
||||
|
||||
|
glad you got it, well done.
![]() to make the drop down initially hidden, add the code in bold to your .aspx code: Code:
<asp:DropDownList id="lstSubAcute" runat="server" style="display: none;"> this will make your list hidden, and when the JS function will run, it will show it. |
|
#10
|
|||
|
|||
|
Thanks again for solve my current javascript problem.
but only a bit problem is in design way, the second box will go down new line automatically if i visible it, unless i give a few space after the second box then the second box will appear at the same line as design. Thank Shadow Wizard~ |
![]() |
| Viewing: ASP Free Forums > Programming > .NET Development > VBScript - Discussion - Listbox_SelectedIndexChanged event no effects |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|