从按钮单击设置textchanged事件true

时间:2017-07-02 15:01:04

标签: c# asp.net webforms

我需要从另一个事件中运行一个事件。

我有这样的asp.net页面:

<div>
    <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged" />
    <asp:Label ID="Label1" runat="server" />
    <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="true" OnTextChanged="TextBox2_TextChanged" />
    <asp:Label ID="Label2" runat="server" />
    <asp:TextBox ID="TextBox3" runat="server" AutoPostBack="true" OnTextChanged="TextBox3_TextChanged" />
    <asp:Label ID="Label3" runat="server" />
    <asp:Button Text="SetFromDataBase" ID="btn" runat="server" OnClick="btn_Click" />
</div>

在C#代码中:

Dal run = new Dal();
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    string query1 = string.Format("select * From user where id={0}", TextBox1.Text);
    DataTable dt1 = new DataTable();
    dt1 = run.withquery(query1);
    Label1.Text = dt1.Rows[0]["name"].ToString();
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
    string query2 = string.Format("select * From profile where id={0}", TextBox2.Text);
    DataTable dt2 = new DataTable();
    dt2 = run.withquery(query2);
    Label2.Text = dt2.Rows[0]["profile"].ToString();
}
protected void btn_Click(object sender, EventArgs e)
{
    string query3 = string.Format("select * From Db where id={0}", "1369");
    DataTable dt3 = new DataTable();
    dt3 = run.withquery(query3);
    TextBox1.Text = dt3.Rows[0]["1"].ToString();
    TextBox2.Text = dt3.Rows[0]["2"].ToString();
    TextBox3.Text = dt3.Rows[0]["3"].ToString();

    TextBox1_TextChanged = true;
}

在最后一行TextBox1_TextChanged对于运行事件,以Lable1.text作为名称。 我不知道该怎么办? 感谢帮助。 希望了解我的观点

1 个答案:

答案 0 :(得分:0)

您可以创建单独的方法来设置标签文本,如下所示:

        Dal run = new Dal();
        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
           SetLabel1Text(TextBox1.Text);         
        }
        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {
            string query2 = string.Format("select * From profile where id={0}", TextBox2.Text);
            DataTable dt2 = new DataTable();
            dt2 = run.withquery(query2);
            Label2.Text = dt2.Rows[0]["profile"].ToString();
        }
        protected void btn_Click(object sender, EventArgs e)
        {
            string query3 = string.Format("select * From Db where id={0}", "1369");
            DataTable dt3 = new DataTable();
            dt3 = run.withquery(query3);
            TextBox1.Text = dt3.Rows[0]["1"].ToString();
            TextBox2.Text = dt3.Rows[0]["2"].ToString();
            TextBox3.Text = dt3.Rows[0]["3"].ToString();

            SetLabel1Text(TextBox1.Text);
        }
        public void SetLabel1Text(string txtText)
        {
            string query1 = string.Format("select * From user where id={0}", txtText);
            DataTable dt1 = new DataTable();
            dt1 = run.withquery(query1);
            Label1.Text = dt1.Rows[0]["name"].ToString();
        }

同样,如果你想为另一个Label设置值。