EddieDuy
EddieDuy

Reputation: 61

Reset/Clear all textbox with 1 function

I Have a Panorama template which each header like this

<controls:PanoramaItem Header="Bookseller" HeaderTemplate="{StaticResource HeaderTemplate}">
            <Grid x:Name="ContentPanel7"  Grid.Row="1" >
                <ListBox x:Name="BooksellerInfo" >
                    <TextBlock   Text="Bookseller's Name" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" />
                    <TextBox x:Name="booksellernametext" Width="460"  TextWrapping="Wrap"/>
                    <TextBlock   Text="Bookseller's Address:" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" />
                    <TextBox x:Name="bookselleraddresstext" Width="460"  TextWrapping="Wrap"/>
                    <TextBlock   Text="Bookseller's Email:" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" />
                    <TextBox x:Name="bookselleremailtext" Width="460"  TextWrapping="Wrap"/>
                    <TextBlock   Text="Bookseller's Homepage:" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" />
                    <TextBox x:Name="booksellerhomepagetext" Width="460"  TextWrapping="Wrap"/>
                    <TextBlock   Text="Bookseller's Phone:" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" />
                    <TextBox x:Name="booksellerphonetext" Width="460"  TextWrapping="Wrap"/>
                    <TextBlock   Text="Assortment of Book:" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" />
                    <TextBox x:Name="assortmentofbooktext" Width="460"  TextWrapping="Wrap"/>
                    <TextBlock   Text="Get Newsletter by Email:" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" />
                    <TextBox x:Name="byemailtext" Width="460"  TextWrapping="Wrap"/>
                    <TextBlock   Text="Get Newsletter by Ordinary Mail:" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" />
                    <TextBox x:Name="byordinarymailtext" Width="460"  TextWrapping="Wrap"/>
                    <TextBlock   Text="Bookseller's Event:" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" />
                    <TextBox x:Name="booksellereventtext" Width="460"  TextWrapping="Wrap"/>
                    <TextBlock   Text="Bookseller's Speciality:" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" />
                    <TextBox x:Name="booksellerspecialitytext" Width="460"  TextWrapping="Wrap"/>
                    <TextBlock   Text="Local Brand Address:" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" />
                    <TextBox x:Name="lcbrandaddresstext" Width="460"  TextWrapping="Wrap"/>
                    <TextBlock   Text="Local Brand Phone:" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" />
                    <TextBox x:Name="lcbrandphonetext" Width="460"  TextWrapping="Wrap"/>
                    <TextBlock   Text="Local Brand Homepage:" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" />
                    <TextBox x:Name="lcbrandhomepagetext" Width="460"  TextWrapping="Wrap"/>
                    <TextBlock   Text="Local Brand Email:" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" />
                    <TextBox x:Name="lcbrandemailtext" Width="460"  TextWrapping="Wrap"/>
                </ListBox>
            </Grid>
        </controls:PanoramaItem>

How can I reset all textbox into "" (clear all textbox) with 1 function?

I dont want to use textbox.Text = "" or textbox.Text = string.Empty because I have so many textbox here

Upvotes: 1

Views: 178

Answers (4)

har07
har07

Reputation: 89325

Use looping statement to avoid rewriting code for every TextBox :

foreach (var item in BooksellerInfo.Items)
{
    if (item is TextBox)
    {
        TextBox textBox = (TextBox) item;
        textBox.Text = "";
    }
}

Upvotes: 2

Carmelo La Monica
Carmelo La Monica

Reputation: 765

Linq is a good Solution

BooksellerInfo.Items.OfType().ToList().ForEach(f=> f.Text = "");

Upvotes: -1

vino20
vino20

Reputation: 429

Hey simplly use jquery...

step 1. Add same class name for all the text box

step 2. create a button with your_function_name() function in onclick event

step 3. write jquery code to clear that text box

 $('#searchField').val('');

thats it...

because its client side operation so u can avoid network traffic...

Upvotes: -1

Nagaraj S
Nagaraj S

Reputation: 13484

well, if you have the textboxes numbered, like TextBox1, TextBox2, etc, you could do something like this

protected void Button1_Click(object sender, EventArgs e)
{

for (int i = 1; i <= 20; i++)
{

TextBox temp = Page.FindControl("TextBox" + i) as TextBox;temp.Text = "";
}

}

c-sharpcorner.com

Upvotes: 1

Related Questions