Solved

Assign value to textbox from dropdown list

Posted on 2013-05-29
6
455 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
Comment Utility
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
Comment Utility
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
Comment Utility
<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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 13

Accepted Solution

by:
Naman Goel earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now