howto: Chg textbox visible on dropdown selection?

P

Phil Streiff

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
 
A

Andy Gaskell

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");
}
 
P

Phil Streiff

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
 
K

Kilic Beg

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 said:
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
 
A

Andy Gaskell

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 said:
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.
 
P

Phil

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
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top