SelectedIndexChanged

S

Steve Wolfie

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
 
T

TJS

Is "9" the selected index or the value of the selection ?

if it is the value then try :

if dropcategory.selectedvalue = "9" then
....
 
S

Steve Wolfie

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
 
B

Bruce Barker

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)
 
S

Steven Cheng[MSFT]

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_change(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_SelectedIndexChanged);
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.)
 
T

TJS

it would be nice to know how to use this in the vb-sdk format


Steven Cheng said:
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_change(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_SelectedIndexChanged);
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.)
 
S

Steven Cheng[MSFT]

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.)
 
S

Steven Cheng[MSFT]

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.)
 
S

Steven Cheng[MSFT]

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top