Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

Dynamically show/hide some fields in a Form

$
0
0

Hi All,

I generate a form in the Page_Load, I want to dynamically show/hide some controls with in this generated form. My code looks like below.

I have a table in my .aspx page.
<asp:Table id="FieldTable" runat="server" BackColor="White"  CssClass="DDGridView"   width="95%">
    
</asp:Table>

I have a Field table in the database which contains different Fields. Each field has a type(can be string,drop-down list,long string etc).In the page load of the .aspx, I first get all the rows in the field table. For each row, based on the type I add a row to the 'FieldTable'.

For example if the field has a type string.
Then I add a field label and display a textbox. If the field is of type drop-down list, I display a drop-down list.

For the string field, mY code which adds a row to the FieldTable looks like below.

            TableRow r = new TableRow();
            TableCell labelCell = new TableCell();
            TableCell valueCell = new TableCell();

            labelCell.Width = new Unit(100, UnitType.Pixel);
            //valueCell.Width = new Unit(400, UnitType.Pixel);

            Label fieldLabel = new Label();
            fieldLabel.Text = name;
            fieldLabel.ToolTip = description;
            fieldLabel.Font.Bold = true;

            TextBox fieldValue = new TextBox();
            fieldValue.Text = defaultValue;
            fieldValue.ID=ID;

            RegularExpressionValidator lengthValidator = new RegularExpressionValidator();
            lengthValidator.ControlToValidate = ID;
            lengthValidator.ErrorMessage = "'" + name + "' field text must be less than 100 characters.";
            lengthValidator.Display = ValidatorDisplay.None;
            lengthValidator.ValidationExpression = "[\\s\\S]{0,100}";
            valueCell.Controls.Add(lengthValidator);

            if (disabled) { fieldValue.Enabled = false; }

            labelCell.Controls.Add(fieldLabel);
            valueCell.Controls.Add(fieldValue);

            if (required)
            {
                RequiredFieldValidator rfv = new RequiredFieldValidator();
                rfv.ControlToValidate = ID;
                name = name.Replace("* ", "");
                rfv.ErrorMessage = "'" + name + "' is a required field.";
                rfv.Display = ValidatorDisplay.None;
                valueCell.Controls.Add(rfv);

            }

            r.Cells.Add(labelCell);
            r.Cells.Add(valueCell);

            FieldTable.Rows.Add(r);
For the drop-down list, the TextBox is replaced with drop-down list.



So in this scenario, Once all the fields are added to the table and displayed as a form, I want to dynamically show/hide some fields.

For example if I have two string fields str1 and str2, I want to show str2 only when str1 has certain value. I am not sure how can acheive this. Please suggest some ideas.

Thanks


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>