Common method for clearform in asp.net?

I use the main page with content pages. I want to write a genral method for cleaning text fields, and for an index pointer for an index - 0. Please be guided by this.

+3
source share
2 answers

Server approach

If you want to clear the TextBoxes and DropDownLists from the postback, you can go through the collection Controlsand see for each control whether it is a TextBox or DropDownList. Here's a function in C #:

void ClearInputs(ControlCollection ctrls)
{
    foreach (Control ctrl in ctrls)
    {
        if (ctrl is TextBox)
            ((TextBox)ctrl).Text = string.Empty;
        else if (ctrl is DropDownList)
            ((DropDownList)ctrl).ClearSelection();

        ClearInputs(ctrl.Controls);
    }
}

To use this, you call ClearInputspassing in the control collection that you want to execute. To clear all TextBoxes and DropDownLists on the page you are using:

ClearInputs(Page.Controls);

Customer approach

. , JavaScript DOM clear/ reset . JavaScript jQuery library :

function clearElements() {
    $("input[type=text]").val('');
    $("select").attr('selectedIndex', 0); 
}

value <input type="text" ... /> selectedIndex <select> 0. script JSFiddle.net, script: http://jsfiddle.net/xs6G9/

. . WebForm ASP.NET.

!

+4

<input type="reset"> .

0

Source: https://habr.com/ru/post/1781553/


All Articles