我有一个表单,它有一个单击文本框,在按下Enter键后将一些数据发送到数据库。数据显示在中继器控制中的文本框下方。通过在该文本框的TextChanged事件中将数据绑定到中继器,立即在窗体上显示输入数据。这段代码如何写得更好?
在CodeBehind中,我调用了两次BindRepeater方法,一次是在每次新的页面加载时以及一次在文本框的TextChanged事件中。
这怎么可以重写调用BindRepeater只有一次,仍然实现相同的效果?
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindRepeater();
}
}
protected void BindRepeater()
{
// data retrieval
// repeater binding
}
protected void CreateData(string newdata)
{
// data insert
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if (TextBox1.Text != string.Empty)
{
string _newData = TextBox1.Text.Trim();
CreateData(_newData);
BindRepeater();
}
}
为什么你不想从两个地方打电话的任何特定原因?代码看起来对我好! – DavidGouge 2012-02-18 18:45:38
可能属于http://codereview.stackexchange.com/ – 2012-02-18 18:45:48
@DavidGouge:这是否正常?即使可能有更多的地方可能会调用BindRepeater? – Animesh 2012-02-18 18:47:20