473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

SelectedIndexChanged

Hi!

I am *still* developing an asp.net app that i would like to do the
following:

i want a user to select a category from a drop down. if they select the
last option "other" then i would like to dynamically display a textbox that
i have initially set the visible attribute to "false"

i use the code as follows:

Private Sub dropcategory_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles dropcategory.SelectedIndexChanged

if dropcategory.selectedindex = 9 then

invisibletextbox.visible = true

else

invisibletextbox.visible = false

End Sub

Cant figure out why it doesn't work.

seems only to work when using a button click event.

Thanks

Steve
Nov 19 '05 #1
9 2990
TJS
Is "9" the selected index or the value of the selection ?

if it is the value then try :

if dropcategory.selectedvalue = "9" then
....
"Steve Wolfie" <st*********@community.nospam> wrote in message
news:Om**************@TK2MSFTNGP12.phx.gbl...
Hi!

I am *still* developing an asp.net app that i would like to do the
following:

i want a user to select a category from a drop down. if they select the
last option "other" then i would like to dynamically display a textbox
that i have initially set the visible attribute to "false"

i use the code as follows:

Private Sub dropcategory_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
dropcategory.SelectedIndexChanged

if dropcategory.selectedindex = 9 then

invisibletextbox.visible = true

else

invisibletextbox.visible = false

End Sub

Cant figure out why it doesn't work.

seems only to work when using a button click event.

Thanks

Steve

Nov 19 '05 #2
no, it would be the index. i know the difference between index and value

the value in this case would be "other" and then the resulting textbox would
be for the "other" category that the user would then fill in...
thanks

steve
"TJS" <no****@here.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Is "9" the selected index or the value of the selection ?

if it is the value then try :

if dropcategory.selectedvalue = "9" then
...
"Steve Wolfie" <st*********@community.nospam> wrote in message
news:Om**************@TK2MSFTNGP12.phx.gbl...
Hi!

I am *still* developing an asp.net app that i would like to do the
following:

i want a user to select a category from a drop down. if they select the
last option "other" then i would like to dynamically display a textbox
that i have initially set the visible attribute to "false"

i use the code as follows:

Private Sub dropcategory_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
dropcategory.SelectedIndexChanged

if dropcategory.selectedindex = 9 then

invisibletextbox.visible = true

else

invisibletextbox.visible = false

End Sub

Cant figure out why it doesn't work.

seems only to work when using a button click event.

Thanks

Steve


Nov 19 '05 #3
set autopostback on. this will cause your page to postpack everytime the
user selects a value in the dropdown.

note: setting autopostback on a select makes using the keyboard (arrow keys
to select) useless as every keystroke causes a postback, and the browser
will hang. to get around this, have client script disable the select on
select, so the postback completes before the next select.

-- bruce (sqlwork.com)
"Steve Wolfie" <st*********@community.nospam> wrote in message
news:Om**************@TK2MSFTNGP12.phx.gbl...
Hi!

I am *still* developing an asp.net app that i would like to do the
following:

i want a user to select a category from a drop down. if they select the
last option "other" then i would like to dynamically display a textbox
that i have initially set the visible attribute to "false"

i use the code as follows:

Private Sub dropcategory_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
dropcategory.SelectedIndexChanged

if dropcategory.selectedindex = 9 then

invisibletextbox.visible = true

else

invisibletextbox.visible = false

End Sub

Cant figure out why it doesn't work.

seems only to work when using a button click event.

Thanks

Steve

Nov 19 '05 #4
Hi Steve,

So does Bruce's suggestion on checking the "autopostback" property work? By
default DropDownlist's AutoPostBack is set to false so that changing it's
selectedItem at clientside won't cause post back. Anyway, here is a simple
test page I've made which contains code using both clientside script and
serverside code to achieve the result you required, you may have a test on
your side to see whether it works:

==========aspx=============
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="jscript">
function lstClient_change(lst)
{
var txt = document.getElementById("txtClient");
var selVal = lst.options[lst.selectedIndex].value;
if(selVal.toLowerCase() == "others")
{
txt.style.display = "";
txt.value = "Please input other values";
}else
{
txt.style.display ="none";
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">

<table width="600">
<tr>
<td>Using Client Script:<br>
<asp:DropDownList id="lstClient" runat="server"></asp:DropDownList>
<asp:TextBox id="txtClient" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Using Server Code:<br>
<asp:DropDownList id="lstServer" runat="server"
AutoPostBack="True"></asp:DropDownList>
<asp:TextBox id="txtServer" runat="server"
Visible="False"></asp:TextBox>
</td>
</tr>
</table>
</form>
</body>
</HTML>

=========code behind=========
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList lstClient;
protected System.Web.UI.WebControls.DropDownList lstServer;
protected System.Web.UI.WebControls.TextBox txtClient;
protected System.Web.UI.WebControls.TextBox txtServer;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
string[] items = {"aaa","bbb","ccc","ddd","eee","fff","ggg","others "};

lstClient.DataSource = lstServer.DataSource = items;
lstClient.DataBind();
lstServer.DataBind();

lstClient.Attributes.Add("onchange","lstClient_cha nge(this)");
txtClient.Style.Add("display","none");
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{

InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.lstServer.SelectedIndexChanged += new
System.EventHandler(this.lstServer_SelectedIndexCh anged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

private void lstServer_SelectedIndexChanged(object sender,
System.EventArgs e)
{
if(lstServer.SelectedValue == "others")
{
txtServer.Visible = true;
txtServer.Text = "Please input other value";
}
else
{
txtServer.Visible = false;
}
}
}

=====================================

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #5
TJS
it would be nice to know how to use this in the vb-sdk format
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:Ga**************@TK2MSFTNGXA01.phx.gbl...
Hi Steve,

So does Bruce's suggestion on checking the "autopostback" property work?
By
default DropDownlist's AutoPostBack is set to false so that changing it's
selectedItem at clientside won't cause post back. Anyway, here is a
simple
test page I've made which contains code using both clientside script and
serverside code to achieve the result you required, you may have a test on
your side to see whether it works:

==========aspx=============
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="jscript">
function lstClient_change(lst)
{
var txt = document.getElementById("txtClient");
var selVal = lst.options[lst.selectedIndex].value;
if(selVal.toLowerCase() == "others")
{
txt.style.display = "";
txt.value = "Please input other values";
}else
{
txt.style.display ="none";
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">

<table width="600">
<tr>
<td>Using Client Script:<br>
<asp:DropDownList id="lstClient" runat="server"></asp:DropDownList>
<asp:TextBox id="txtClient" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Using Server Code:<br>
<asp:DropDownList id="lstServer" runat="server"
AutoPostBack="True"></asp:DropDownList>
<asp:TextBox id="txtServer" runat="server"
Visible="False"></asp:TextBox>
</td>
</tr>
</table>
</form>
</body>
</HTML>

=========code behind=========
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList lstClient;
protected System.Web.UI.WebControls.DropDownList lstServer;
protected System.Web.UI.WebControls.TextBox txtClient;
protected System.Web.UI.WebControls.TextBox txtServer;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
string[] items = {"aaa","bbb","ccc","ddd","eee","fff","ggg","others "};

lstClient.DataSource = lstServer.DataSource = items;
lstClient.DataBind();
lstServer.DataBind();

lstClient.Attributes.Add("onchange","lstClient_cha nge(this)");
txtClient.Style.Add("display","none");
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{

InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.lstServer.SelectedIndexChanged += new
System.EventHandler(this.lstServer_SelectedIndexCh anged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

private void lstServer_SelectedIndexChanged(object sender,
System.EventArgs e)
{
if(lstServer.SelectedValue == "others")
{
txtServer.Visible = true;
txtServer.Text = "Please input other value";
}
else
{
txtServer.Visible = false;
}
}
}

=====================================

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #6
Hi TJS,

Does the vb-sdk you mentioned means VB.NET? Or in another word, I'd prefer
a VB.NET format page code ? Anyway, I'll rewrite a VB.NET one and paste it
later.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #7
OK, Here is the modified VB.NET version

#the aspx page is the same as the C# one:
========code behind (VB.NET)===========
Public Class selectindexpage
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents lstClient As System.Web.UI.WebControls.DropDownList
Protected WithEvents txtClient As System.Web.UI.WebControls.TextBox
Protected WithEvents lstServer As System.Web.UI.WebControls.DropDownList
Protected WithEvents txtServer As System.Web.UI.WebControls.TextBox

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then

Dim items() As String = {"aaa", "bbb", "ccc", "ddd", "eee",
"fff", "ggg", "others"}

lstClient.DataSource = items
lstServer.DataSource = items
lstClient.DataBind()
lstServer.DataBind()

lstClient.Attributes.Add("onchange", "lstClient_change(this)")
txtClient.Style.Add("display", "none")

End If
End Sub


Private Sub lstServer_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
lstServer.SelectedIndexChanged
If lstServer.SelectedValue = "others" Then

txtServer.Visible = True
txtServer.Text = "Please input other value"

Else

txtServer.Visible = False
End If
End Sub
End Class

========================

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #8
TJS
vb-sdk means vb.net language and no code behind...
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:BS**************@TK2MSFTNGXA01.phx.gbl...
Hi TJS,

Does the vb-sdk you mentioned means VB.NET? Or in another word, I'd
prefer
a VB.NET format page code ? Anyway, I'll rewrite a VB.NET one and paste
it
later.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #9
Hi TJS,

Thanks for your explanation, I'm often using codebehind since it's the
VS.NET IDE's default behavior. Also since I only used two simple
functions/events in my codebehin in the demo page, you can simply copy them
into the aspx template
<script ruant=server > ....<script>

If anything else we can help, please feel free to post here.

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: LV | last post by:
Hello, I would like to manually set one of my list view items as selected. When this item is set, I would like for a method to execute. I have a delegate on the list view for SelectedIndexChanged....
0
by: LV | last post by:
Hello, When I manually set a list view item to be a selected item, the SelectedIndexChanged event is not firing. Am I missing something here? using System; using System.Windows.Forms; ...
1
by: Donal | last post by:
I have 3 related dropdowns. When the 1st is changed, the 2nd is updated, and when the 2nd is changed, the 3rd is updated. When i change the 1st dropdown (sites), the SelectedIndexChanged fires...
0
by: PeacError | last post by:
Using Microsoft Visual Studio .NET 2003, Visual C# .NET 1.1: I apologize if this question has been addressed elsewhere, but I could not find a reference to it in the search engine for this...
0
by: tafpin | last post by:
I have an application with a datagrid. In the IDE I have 2 template columns. The first has an image button and the second contains a link button. According to the results that I get back I must...
2
by: rdb | last post by:
VB.NET web program with a webform w/2 dropdownlistboxes, set to AutoPostBack TRUE, selection in either dropdown fires the SelectedIndexChanged events correctly UNTIL I navigate to second webform in...
7
by: sparkle | last post by:
Hi Everybody, I'm filling a combobox from a class, which works fine on it's own. But when I insert code to fill in other controls something in the combobox fill is causing the...
3
by: Alec MacLean | last post by:
Hi, I have a couple of win forms where I am editing values that are stored in a SQL database. I'm using the listbox control to hold the data object each form interacts with. Each object is...
3
by: martin1 | last post by:
Hi, All, I want user select first item (called All) in listbox, then all other items are selected by SetSelected method, but in loop (see code below) whenever going to SetSelected(), the...
4
by: lakepeir | last post by:
Hello, I have combobox with a selectedindexchanged method that seems to be called when starting the application, launching the form with the combobox and making a change in the drop down box of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.