• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 512
  • Last Modified:

How to? Hide textbox on Dropdownlist selection in FormView

I haev a formview used for editing/Inserting recrods.  I need to dispaly or hide texytboxes base don selection in dropdownlist and radiobuttonlists.  I can do this with selectedIndexchanged Events in a normal form,  but in a formview the events dont fire.
How do you fire the events from within a formview and update the formview?
0
mattfox77
Asked:
mattfox77
1 Solution
 
strickddCommented:
As long as they have an event wired up and autopostback is set to true, it will execute the event even from inside a formview control. Make sure these are set on the dropdown.

You can also do most of what you want in javascript

codebehind:
myDDL.Attributes.Add("onchange", "document.getElementById('" + myControlToHide.ClientID + "').style.display = 'none'");
0
 
mattfox77Author Commented:
I had the event wired up but autoPostBack wasn't set! Once I set AutoPostBack = truie on the dropdown, it worked.
I then had to set if statements on databound and in the event to evaluate the status and change visible of textbox accordingly.
I aslo had to setup if statements to identify the FormView Current Mode to find a textbox on edit and a label on ReadOnly

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Public_HRApp3.Application
{
    public partial class Identification1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            hfApplicant.Value = "75";
            hfUserName.Value = "test2";
            hfAppStatus.Value = "Identification";
        }
        protected void ddlStart_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            DropDownList ddlStart = (DropDownList)fvApplicant.FindControl("ddlStart");
            TextBox txtStartDate = (TextBox)fvApplicant.FindControl("txtStartDate");
            if (ddlStart.SelectedIndex == 3)
            {
                txtStartDate.Visible = true;
            }
            else
            {
                txtStartDate.Visible = false;
            }
        }

        protected void fvApplicant_PageIndexChanging(object sender, FormViewPageEventArgs e)
        {

        }

        protected void fvApplicant_DataBound(object sender, EventArgs e)
        {
            if (fvApplicant.CurrentMode == FormViewMode.ReadOnly)
            {
                Label lblCounty = (Label)fvApplicant.FindControl("lblCounty");
                Label CountyTxt = (Label)fvApplicant.FindControl("CountyTxt");
                Label lblState = (Label)fvApplicant.FindControl("lblState");
                RadioButtonList rblFTEDesired = (RadioButtonList)fvApplicant.FindControl("rblFTEDesired");
                Label lblPartTimeTxt = (Label)fvApplicant.FindControl("lblPartTimeTxt");
                Label lblPartTimeHoursAvailable = (Label)fvApplicant.FindControl("lblPartTimeHoursAvailable");
                if (lblState.Text == "MO")
                {
                    lblCounty.Visible = true;
                    CountyTxt.Visible = true;

                }
                else
                {
                    lblCounty.Visible = false;
                    CountyTxt.Visible = false;
                }

                if (rblFTEDesired.SelectedValue == "False")
                {
                    lblPartTimeTxt.Visible = true;
                    lblPartTimeHoursAvailable.Visible = true;
                }
                else
                {
                    lblPartTimeTxt.Visible = false;
                    lblPartTimeHoursAvailable.Visible = false;
                }
            }
            if (fvApplicant.CurrentMode == FormViewMode.Edit)
            {
                DropDownList ddlState = (DropDownList)fvApplicant.FindControl("ddlState");
                DropDownList ddlCounty = (DropDownList)fvApplicant.FindControl("ddlCounty");
                Label CountyTxt = (Label)fvApplicant.FindControl("CountyTxt");
                RequiredFieldValidator vldCounty = (RequiredFieldValidator)fvApplicant.FindControl("vldCounty");
                RadioButtonList rblFTEDesired = (RadioButtonList)fvApplicant.FindControl("rblFTEDesired");
                Label lblPartTimeTxt = (Label)fvApplicant.FindControl("lblPartTimeTxt");
                TextBox txtPartTimeHoursAvailable = (TextBox)fvApplicant.FindControl("txtPartTimeHoursAvailable");

                if (ddlState.SelectedValue == "MO")
                {
                    ddlCounty.Visible = true;
                    CountyTxt.Visible = true;
                    vldCounty.Visible = true;
                    vldCounty.Enabled = true;
                }
                else
                {
                    ddlCounty.Visible = false;
                    CountyTxt.Visible = false;
                    vldCounty.Visible = false;
                    vldCounty.Enabled = false;
                }
                if (rblFTEDesired.SelectedValue == "False")
                {
                    lblPartTimeTxt.Visible = true;
                    txtPartTimeHoursAvailable.Visible = true;
                }
                else
                {
                    lblPartTimeTxt.Visible = false;
                    txtPartTimeHoursAvailable.Visible = false;
                }

            }
            if (fvApplicant.CurrentMode == FormViewMode.Insert)
            {
               
            }
        }
        protected void rblFTEDesired_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            RadioButtonList rblFTEDesired = (RadioButtonList)fvApplicant.FindControl("rblFTEDesired");
            Label lblPartTimeTxt = (Label)fvApplicant.FindControl("lblPartTimeTxt");
            TextBox txtPartTimeHoursAvailable = (TextBox)fvApplicant.FindControl("txtPartTimeHoursAvailable");

            if (rblFTEDesired.SelectedValue == "False")
            {
                lblPartTimeTxt.Visible = true;
                txtPartTimeHoursAvailable.Visible = true;
            }
            else
            {
                lblPartTimeTxt.Visible = false;
                txtPartTimeHoursAvailable.Visible = false;
            }
        }
        protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlState = (DropDownList)fvApplicant.FindControl("ddlState");
            DropDownList ddlCounty = (DropDownList)fvApplicant.FindControl("ddlCounty");
            Label CountyTxt = (Label)fvApplicant.FindControl("CountyTxt");
            RequiredFieldValidator vldCounty = (RequiredFieldValidator)fvApplicant.FindControl("vldCounty");
            if (ddlState.SelectedValue == "MO")
            {
                ddlCounty.Visible = true;
                CountyTxt.Visible = true;
                vldCounty.Visible = true;
                vldCounty.Enabled = true;
            }
            else
            {
                ddlCounty.Visible = false;
                CountyTxt.Visible = false;
                vldCounty.Visible = false;
                vldCounty.Enabled = false;
            }
        }
        protected void EditButton_Click(object sender, EventArgs e)
        {
            fvApplicant.ChangeMode(FormViewMode.Edit);
        }
    }
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now