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;
}
<aspropDownList id="ddlAcctType"
runat="server"
AutoPostBack="True"
onSelectedItemChanged="isContractor"
EnableViewState="True">
</aspropDownList>
<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
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;
}
<aspropDownList id="ddlAcctType"
runat="server"
AutoPostBack="True"
onSelectedItemChanged="isContractor"
EnableViewState="True">
</aspropDownList>
<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