使用1功能重置/清除所有文本框

时间:2014-01-03 03:21:52

标签: c# textbox windows-phone

我有一个Panorama模板,每个标题都是这样的

<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>

如何使用1个功能将所有文本框重置为“”(清除所有文本框)?

我不想使用textbox.Text = ""textbox.Text = string.Empty因为我这里有很多文本框

4 个答案:

答案 0 :(得分:2)

使用循环语句来避免为每个TextBox重写代码:

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

答案 1 :(得分:1)

好吧,如果您有编号的文本框,如TextBox1,TextBox2等,您可以这样做

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

答案 2 :(得分:-1)

嘿,简单地使用jquery ......

步骤1.为所有文本框添加相同的类名

步骤2.在onclick事件

中创建一个带有your_function_name()函数的按钮

步骤3.编写jquery代码以清除该文本框

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

多数民众赞成......

因为它的客户端操作所以你可以避免网络流量...

答案 3 :(得分:-1)

Linq是一个很好的解决方案

BooksellerInfo.Items.OfType()。ToList()。ForEach(f =&gt; f.Text =“”);