问题描述
如何使用内容页面上的按钮点击事件清除从母版页继承的内容页面控件的数据 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 + ",";
}
}
}
}
更多信息访问控件集合 [