howto: Chg textbox visible on dropdown selection?

Discussion in 'ASP .Net Web Controls' started by Phil Streiff, May 21, 2004.

  1. Phil Streiff

    Phil Streiff Guest

    I am trying to get a webform textbox control to show|hide based on a
    dropdownlist selection. The dropdownlist displays AccountType. The
    choices are Employee or Contractor. If the user selects Contractor
    then they also need to furnish their "Contract Number". Therefore, the
    txtContractNum textbox should remain hidden unless the user selects
    Contractor, then "viola!", the txtContractNum textbox appears,
    prompting the user to enter a contract number.

    Here is what I have tried so far, without success:
    -------------------------------------------------------------------------
    public void Page_Load (object sender, System.EventArgs e) {
    if (!IsPostBack) {
    SqlConnection myConn = new
    SqlConnection(ConfigurationSettings.AppSettings["connString"]);
    SqlCommand acctCmd = new SqlCommand("sp_Get_AccountType",
    myConn);
    acctCmd.CommandType = CommandType.StoredProcedure;

    myConn.Open();
    SqlDataReader drAccount = acctCmd.ExecuteReader();

    //Set up the data binding.
    ddlAcctType.DataSource = drAccount;
    ddlAcctType.DataTextField = "AccountType";
    ddlAcctType.DataValueField = "AcctTypeID";
    ddlAcctType.DataBind();
    //Close the connection.
    myConn.Close();
    drAccount.Close();
    }
    }

    public void isContractor(object sender, System.EventArgs e) {
    //make txtContractNum visible if Contractor selected in ddlAcctType
    if (ddlAcctType.SelectedItem.Value == "2") {
    txtContractNum.Visible = true;
    }
    txtContractNum.Visible = false;
    }


    <asp:DropDownList id="ddlAcctType"
    runat="server"
    AutoPostBack="True"
    onSelectedItemChanged="isContractor"
    EnableViewState="True">
    </asp:DropDownList>

    <asp:TextBox id="txtContractNum"
    runat="server"
    value="Contract number"
    </asp:TextBox>

    ------------------------------------------------------------------------

    The form does a postback when AcctType is changed but never seems to
    properly evaluate the expression in order to distinguish between "2"
    (Contractor) or "1" (Employee) and show or hide the txtContractNum
    control as desired.

    Can anyone see what I am doing wrong and offer some guidance on how I
    need to fix this?

    TIA,
    Phil
     
    Phil Streiff, May 21, 2004
    #1
    1. Advertising

  2. Phil Streiff

    Andy Gaskell Guest

    The problem with your isContractor method is that the txtContractNum.Visible
    = false is always executed. Try changing your isContractor method to this:

    public void isContractor(object sender, System.EventArgs e)
    {
    txtContractNum.Visible = (ddlAcctType.SelectedItem.Value == "2");
    }

    "Phil Streiff" <> wrote in message
    news:...
    > I am trying to get a webform textbox control to show|hide based on a
    > dropdownlist selection. The dropdownlist displays AccountType. The
    > choices are Employee or Contractor. If the user selects Contractor
    > then they also need to furnish their "Contract Number". Therefore, the
    > txtContractNum textbox should remain hidden unless the user selects
    > Contractor, then "viola!", the txtContractNum textbox appears,
    > prompting the user to enter a contract number.
    >
    > Here is what I have tried so far, without success:
    > -------------------------------------------------------------------------
    > public void Page_Load (object sender, System.EventArgs e) {
    > if (!IsPostBack) {
    > SqlConnection myConn = new
    > SqlConnection(ConfigurationSettings.AppSettings["connString"]);
    > SqlCommand acctCmd = new SqlCommand("sp_Get_AccountType",
    > myConn);
    > acctCmd.CommandType = CommandType.StoredProcedure;
    >
    > myConn.Open();
    > SqlDataReader drAccount = acctCmd.ExecuteReader();
    >
    > //Set up the data binding.
    > ddlAcctType.DataSource = drAccount;
    > ddlAcctType.DataTextField = "AccountType";
    > ddlAcctType.DataValueField = "AcctTypeID";
    > ddlAcctType.DataBind();
    > //Close the connection.
    > myConn.Close();
    > drAccount.Close();
    > }
    > }
    >
    > public void isContractor(object sender, System.EventArgs e) {
    > //make txtContractNum visible if Contractor selected in ddlAcctType
    > if (ddlAcctType.SelectedItem.Value == "2") {
    > txtContractNum.Visible = true;
    > }
    > txtContractNum.Visible = false;
    > }
    >
    >
    > <asp:DropDownList id="ddlAcctType"
    > runat="server"
    > AutoPostBack="True"
    > onSelectedItemChanged="isContractor"
    > EnableViewState="True">
    > </asp:DropDownList>
    >
    > <asp:TextBox id="txtContractNum"
    > runat="server"
    > value="Contract number"
    > </asp:TextBox>
    >
    > ------------------------------------------------------------------------
    >
    > The form does a postback when AcctType is changed but never seems to
    > properly evaluate the expression in order to distinguish between "2"
    > (Contractor) or "1" (Employee) and show or hide the txtContractNum
    > control as desired.
    >
    > Can anyone see what I am doing wrong and offer some guidance on how I
    > need to fix this?
    >
    > TIA,
    > Phil
     
    Andy Gaskell, May 22, 2004
    #2
    1. Advertising

  3. Phil Streiff

    Phil Streiff Guest

    Andy

    Thanks for your reply. I tried your suggestion but the textbox visible property is not changing when I change the ddl selection. just stays visible all the time now

    Any other ideas

    thanks
    Phil
     
    Phil Streiff, May 24, 2004
    #3
  4. Phil Streiff

    Kilic Beg Guest

    put an else case in your isContractor method..

    > public void isContractor(object sender, System.EventArgs e) {
    > //make txtContractNum visible if Contractor selected in ddlAcctType
    > if (ddlAcctType.SelectedItem.Value == "2") {
    > txtContractNum.Visible = true;
    > }

    else
    > txtContractNum.Visible = false;
    > }


    ----------
    Kilic Beg

    "Phil Streiff" <> wrote in message
    news:...
    > I am trying to get a webform textbox control to show|hide based on a
    > dropdownlist selection. The dropdownlist displays AccountType. The
    > choices are Employee or Contractor. If the user selects Contractor
    > then they also need to furnish their "Contract Number". Therefore, the
    > txtContractNum textbox should remain hidden unless the user selects
    > Contractor, then "viola!", the txtContractNum textbox appears,
    > prompting the user to enter a contract number.
    >
    > Here is what I have tried so far, without success:
    > -------------------------------------------------------------------------
    > public void Page_Load (object sender, System.EventArgs e) {
    > if (!IsPostBack) {
    > SqlConnection myConn = new
    > SqlConnection(ConfigurationSettings.AppSettings["connString"]);
    > SqlCommand acctCmd = new SqlCommand("sp_Get_AccountType",
    > myConn);
    > acctCmd.CommandType = CommandType.StoredProcedure;
    >
    > myConn.Open();
    > SqlDataReader drAccount = acctCmd.ExecuteReader();
    >
    > //Set up the data binding.
    > ddlAcctType.DataSource = drAccount;
    > ddlAcctType.DataTextField = "AccountType";
    > ddlAcctType.DataValueField = "AcctTypeID";
    > ddlAcctType.DataBind();
    > //Close the connection.
    > myConn.Close();
    > drAccount.Close();
    > }
    > }
    >
    > public void isContractor(object sender, System.EventArgs e) {
    > //make txtContractNum visible if Contractor selected in ddlAcctType
    > if (ddlAcctType.SelectedItem.Value == "2") {
    > txtContractNum.Visible = true;
    > }
    > txtContractNum.Visible = false;
    > }
    >
    >
    > <asp:DropDownList id="ddlAcctType"
    > runat="server"
    > AutoPostBack="True"
    > onSelectedItemChanged="isContractor"
    > EnableViewState="True">
    > </asp:DropDownList>
    >
    > <asp:TextBox id="txtContractNum"
    > runat="server"
    > value="Contract number"
    > </asp:TextBox>
    >
    > ------------------------------------------------------------------------
    >
    > The form does a postback when AcctType is changed but never seems to
    > properly evaluate the expression in order to distinguish between "2"
    > (Contractor) or "1" (Employee) and show or hide the txtContractNum
    > control as desired.
    >
    > Can anyone see what I am doing wrong and offer some guidance on how I
    > need to fix this?
    >
    > TIA,
    > Phil
     
    Kilic Beg, May 24, 2004
    #4
  5. Phil Streiff

    Andy Gaskell Guest

    I'm not sure if Employee or Contractor is showing up first in your
    DropDownList, but we should handle it either way. Let's change the code a
    bit so we don't have to duplicate logic:

    First, add a new method:
    private void UpdateTextBox()
    {
    txtContractNum.Visible = (ddlAcctType.SelectedItem.Value == "2");
    }

    Next, update this method to call the new method
    public void isContractor(object sender, System.EventArgs e)
    {
    UpdateTextBox();
    }

    Finally, add one line of code to Page_Load:
    public void Page_Load (object sender, System.EventArgs e) {
    if (!IsPostBack) {
    SqlConnection myConn = new
    SqlConnection(ConfigurationSettings.AppSettings["connString"]);
    SqlCommand acctCmd = new SqlCommand("sp_Get_AccountType",
    myConn);
    acctCmd.CommandType = CommandType.StoredProcedure;

    myConn.Open();
    SqlDataReader drAccount = acctCmd.ExecuteReader();

    //Set up the data binding.
    ddlAcctType.DataSource = drAccount;
    ddlAcctType.DataTextField = "AccountType";
    ddlAcctType.DataValueField = "AcctTypeID";
    ddlAcctType.DataBind();
    //Close the connection.
    myConn.Close();
    drAccount.Close();

    UpdateTextBox(); //NEW LINE
    }
    }



    "Phil Streiff" <> wrote in message
    news:...
    > Andy:
    >
    > Thanks for your reply. I tried your suggestion but the textbox visible

    property is not changing when I change the ddl selection. just stays visible
    all the time now.
    >
    > Any other ideas?
    >
    > thanks,
    > Phil
     
    Andy Gaskell, May 25, 2004
    #5
  6. Phil Streiff

    Phil Guest

    Thanks Andy. Initially your solution didn't work until I moved the UpdateTextBox method to the "else" block of the if (!IsPostBack) class. Now the method runs because it IS a PostBack event (ddlAcctType AutoPostBack=true)

    Take care
    Phil
     
    Phil, May 27, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith A. Rowe
    Replies:
    2
    Views:
    305
    Keith A. Rowe
    Feb 14, 2004
  2. curiousity

    HOWTO autopost the selection list upon selection

    curiousity, Nov 21, 2003, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    291
    curiousity
    Nov 21, 2003
  3. Jurjen de Groot
    Replies:
    0
    Views:
    334
    Jurjen de Groot
    May 19, 2008
  4. didier Belot
    Replies:
    5
    Views:
    216
    Randy Webb
    Aug 29, 2004
  5. maya
    Replies:
    3
    Views:
    114
Loading...

Share This Page