?
Solved

Assign value to textbox from dropdown list

Posted on 2013-05-29
6
Medium Priority
?
501 Views
Last Modified: 2013-06-03
I am trying to assign values to three text boxes based on the selected value of a drop down box when a button is clicked.

I am new to asp.net.

Can anyone tell me what is wrong with my code as nothing happens when i click the button?


Protected Sub btnPreview_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPreview.Click
        If ddlMonth.SelectedItem.value = "January" Then
            CycleNo1.Text = 1
            CycleNo2.Text = 1
            CycleNo3.Text = CycleNo1.Text + 1
        ElseIf ddlMonth.SelectedItem.value = "February" Then
            CycleNo1.Text = 2
            CycleNo2.Text = 2
            CycleNo3.Text = CycleNo1.Text + 1
        ElseIf ddlMonth.SelectedItem.Value = "March" Then
            CycleNo1.Text = 3
            CycleNo2.Text = 3
            CycleNo3.Text = CycleNo1.Text + 1
        End If
    End Sub
0
Comment
Question by:ernie_shah
6 Comments
 
LVL 15

Expert Comment

by:rajeeshmca
ID: 39206757
Hi,

Could you please post the aspx page as well

Also check with the spelling and Case sensitiveness (Capital and small letters)

Regards
Rajeesh
0
 

Author Comment

by:ernie_shah
ID: 39206784
I am away from my office for the week but will post on Monday. Please look forward. So grateful for the assistance.
0
 
LVL 10

Expert Comment

by:Monica P
ID: 39206993
<form id="form1" runat="server">
    <div>
    <asp:dropdownlist ID="ddlMonth" runat="server" AutoPostBack ="true" >
    <asp:ListItem Text="January" Value ="January"></asp:ListItem>
    <asp:ListItem Text="February" Value ="Feburary"></asp:ListItem>
    <asp:ListItem Text="March" Value ="March"></asp:ListItem>
    </asp:dropdownlist>
    
        <asp:TextBox ID="CycleNo1" runat="server"></asp:TextBox>
        <asp:TextBox ID="CycleNo2"
            runat="server"></asp:TextBox>
            <asp:TextBox ID="CycleNo3" runat="server"></asp:TextBox>
            
        <asp:Button ID="btnPreview" runat="server" Text="Button" />
    </div>
    </form>

Open in new window


Protected Sub btnPreview_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPreview.Click
        If ddlMonth.SelectedItem.Value = "January" Then
            CycleNo1.Text = 1
            CycleNo2.Text = 1
            CycleNo3.Text = CycleNo1.Text + 1
        ElseIf ddlMonth.SelectedItem.Value = "February" Then
            CycleNo1.Text = 2
            CycleNo2.Text = 2
            CycleNo3.Text = CycleNo1.Text + 1
        ElseIf ddlMonth.SelectedItem.Value = "March" Then
            CycleNo1.Text = 3
            CycleNo2.Text = 3
            CycleNo3.Text = CycleNo1.Text + 1
        End If
    End Sub

Open in new window



Place button click code by double clicking the button in design mode or Select the events of click from Cod behind file for button "btnPreview"
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Accepted Solution

by:
Naman Goel earned 2000 total points
ID: 39208613
Yeah, I have created following page, Create a web form and edit designer based on your requirement, in Items List of you dropdownbox control enter same value for Text as well as Value property.

Goto properties of btnPreview and select Click event and enter your code:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication3._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
        <asp:DropDownList ID="ddlMonth" runat="server">
            <asp:ListItem>January</asp:ListItem>
            <asp:ListItem>February</asp:ListItem>
            <asp:ListItem>March</asp:ListItem>
            <asp:ListItem>April</asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="CycleNo1" runat="server"></asp:TextBox>
        <asp:TextBox ID="CycleNo2" runat="server"></asp:TextBox>
        <asp:TextBox ID="CycleNo3" runat="server"></asp:TextBox>
        <asp:Button ID="btnPreview" runat="server" Text="Button" />
    </form>
</body>
</html>

Open in new window


following are the code:
Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnPreview_Click(sender As Object, e As EventArgs) Handles btnPreview.Click
        If ddlMonth.SelectedItem.Value = "January" Then
            CycleNo1.Text = 1
            CycleNo2.Text = 1
            CycleNo3.Text = CycleNo1.Text + 1
        ElseIf ddlMonth.SelectedItem.Value = "February" Then
            CycleNo1.Text = 2
            CycleNo2.Text = 2
            CycleNo3.Text = CycleNo1.Text + 1
        ElseIf ddlMonth.SelectedItem.Value = "March" Then
            CycleNo1.Text = 3
            CycleNo2.Text = 3
            CycleNo3.Text = CycleNo1.Text + 1
        End If
    End Sub
End Class

Open in new window

0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 39208693
adding designer code :

'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated. 
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict On
Option Explicit On


Partial Public Class _Default

    '''<summary>
    '''form1 control.
    '''</summary>
    '''<remarks>
    '''Auto-generated field.
    '''To modify move field declaration from designer file to code-behind file.
    '''</remarks>
    Protected WithEvents form1 As Global.System.Web.UI.HtmlControls.HtmlForm

    '''<summary>
    '''ddlMonth control.
    '''</summary>
    '''<remarks>
    '''Auto-generated field.
    '''To modify move field declaration from designer file to code-behind file.
    '''</remarks>
    Protected WithEvents ddlMonth As Global.System.Web.UI.WebControls.DropDownList

    '''<summary>
    '''CycleNo1 control.
    '''</summary>
    '''<remarks>
    '''Auto-generated field.
    '''To modify move field declaration from designer file to code-behind file.
    '''</remarks>
    Protected WithEvents CycleNo1 As Global.System.Web.UI.WebControls.TextBox

    '''<summary>
    '''CycleNo2 control.
    '''</summary>
    '''<remarks>
    '''Auto-generated field.
    '''To modify move field declaration from designer file to code-behind file.
    '''</remarks>
    Protected WithEvents CycleNo2 As Global.System.Web.UI.WebControls.TextBox

    '''<summary>
    '''CycleNo3 control.
    '''</summary>
    '''<remarks>
    '''Auto-generated field.
    '''To modify move field declaration from designer file to code-behind file.
    '''</remarks>
    Protected WithEvents CycleNo3 As Global.System.Web.UI.WebControls.TextBox

    '''<summary>
    '''btnPreview control.
    '''</summary>
    '''<remarks>
    '''Auto-generated field.
    '''To modify move field declaration from designer file to code-behind file.
    '''</remarks>
    Protected WithEvents btnPreview As Global.System.Web.UI.WebControls.Button
End Class

Open in new window

0
 

Author Comment

by:ernie_shah
ID: 39216367
Good Morning:

Great thanks.

I also realize my combobox that is extracting data from the database has its value to the primary key which is numeric (2 rather than February) and all works fine now.

Much appreciated.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I don't know if many of you have made the great mistake of using the Cisco Thin Client model with the management software VXC. If you have then you are probably more then familiar with the incredibly clunky interface, the numerous work arounds, and …
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
The viewer will learn how to successfully download and install the SARDU utility on Windows 8, without downloading adware.
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question