清除ASP.NET服务器控制数据 [英] clearing ASP.NET server controls data

查看:60
本文介绍了清除ASP.NET服务器控制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用内容页面上的按钮点击事件清除从母版页继承的内容页面控件的数据



i已在按钮中编写以下代码点击。但我无法清除控制日期

任何人都可以在这方面帮助我



how to clear the data of content page controls which is inheriting from master page using button click event which is present on content page

i have written following code in button click .but i am not able to clear the controls date
can any body help me out in this regard

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Reuseable;
 

namespace ModelCode
{
public partial class GenerateConnectionString : System.Web.UI.Page
{
Utilities Utils = new Utilities();
protected void Page_Load(object sender, EventArgs e)
{

}
 
protected void btnGenerateConnectionString_Click(object sender, EventArgs e)
{
 
lblResult.Text = "server=" + txtServerName.Text + "; database=" + txtDatabaseName.Text + "; user id=" + txtUserId.Text + "; password=" + txtPassword.Text + ";";
lblResult.Text = Utils.EncrypString(lblResult.Text);
}
 
protected void Button1_Click(object sender, EventArgs e)
{
 
CleartextBoxes(this.Page);
//Response.Redirect("~/GenerateConnectionString.aspx");

}
public void CleartextBoxes(Control parent)
{
 
foreach (Control ctrl in parent.Controls)
{
if (ctrl is TextBox)
{
TextBox tb = (TextBox)ctrl;
tb.Text = string.Empty;
}
else if (ctrl is DropDownList)
{
DropDownList dl = (DropDownList)ctrl;
dl.SelectedIndex = 0;
}
else if (ctrl is CheckBox)
{
CheckBox cb = (CheckBox)ctrl;
cb.Checked = false;
}
}
} 

}
}

推荐答案

更大的代码示例会有所帮助,但我怀疑你有范围问题。什么是'this'指的是你的代码执行的位置?



试试这个:



A larger code sample would be helpful however I suspect your having scope issues. What is 'this' referring to in relation to where your code is being executed?

Try this:

private void Button1_Click(object sender, System.EventArgs e)
{
    string allTextBoxValues = "";
    foreach (Control c in Page.Controls)
    {
        foreach (Control childc in c.Controls)
        {
            if (childc is TextBox)
            {
                allTextBoxValues += ((TextBox)childc).Text + ",";
            }
        }
    }
}





更多信息访问控件集合 [ ^ ]


这篇关于清除ASP.NET服务器控制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆