Solved

Assign value to textbox from dropdown list

Posted on 2013-05-29
6
478 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 13

Accepted Solution

by:
Naman Goel earned 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Employees depend heavily on their PCs, and new threats like ransomware make it even more critical to protect their important data.
The viewer will learn how to successfully download and install the SARDU utility on Windows 8, without downloading adware.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

832 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