469,282 Members | 2,025 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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 2817
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.