如何将新服务器添加到带有文本和按钮的列表中?

时间:2019-02-11 14:27:16

标签: c# asp.net

我需要创建一个动态列表来显示服务器是否正在运行,每次添加新服务器时,该列表都必须使用标签和按钮来显示服务器的状态,如何创建添加的列表或网格按钮/标签自动检测到我添加了新服务器?

当前看起来像这样:https://i.imgur.com/6B4YFw7.png

SqlConnection condb = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename =|DataDirectory|\\ConnectionStrings.mdf; Integrated Security = True");
        SqlDataAdapter test = new SqlDataAdapter("Select * From [Table]" ,condb);
        DataTable dt1 = new DataTable();
        test.Fill(dt1);
        foreach (DataRow row in dt1.Rows)
        {

            string Ip= row["Ip"].ToString();
            string Id = row["User"].ToString();
            string Port = row["Port"].ToString();
            string Password = row["Password"].ToString();

            string credentials = $"server={Ip}; user id={Id}; port={Port}; Password={Password}";

            //Opens the connection and sends "show slave status" command.
            MySqlConnection con = new MySqlConnection(credentials);
            con.Open(); //Opens the connection
            MySqlCommand cmd = new MySqlCommand(sqlQuery, con); //Sends the query to "show slave status"
            MySqlDataAdapter adp = new MySqlDataAdapter(cmd);

            adp.Fill(ds); //Fill Dataset.
            dt = ds.Tables[0]; //Then assign table to dt.


            foreach (DataRow dataRow in dt.Rows) //Checks the server if it is running or not.
            {
                string value = dataRow.Field<string>("Slave_IO_Running"); //Looks for "Slave_IO_Running" status.


                if (value == "Yes")
                {
                    Label1.Text = "WORKING";
                    Label1.ForeColor = System.Drawing.Color.Green;
                }
                else
                {
                    Label1.Text = "REPLICATION ERROR";
                    Label1.ForeColor = System.Drawing.Color.Red;
                }

            }

        } 

1 个答案:

答案 0 :(得分:0)

尝试包装用于在ASP:UpdatePanel中显示服务器列表的GridView /表。

<asp:UpdatePanel runat="server" ID="upnlServerList" UpdateMode="Conditional">
    <ContentTemplate>
        <%-- YOUR GRIDVIEW HERE --%>
    </ContentTemplate>
</asp:UpdatePanel>

然后,在代码隐藏中,当您调用方法/触发事件以将新事件添加到列表中时,请调用UpdatePanel更新方法:

upnlServerList.Update();

如果这不是您想要的内容,则还可以考虑使用ASP:Repeaters,它允许您将列表绑定到转发器并以其他格式显示列表。