繁体   English   中英

下拉列表会在选中时更改Gridview中的所有值,而不是特定行

[英]Dropdownlist changes all values in Gridview when selected and not specific row

我在Gridview中有一个DropDownList,可让您启动或停止远程服务器上的服务。 DropDownList将允许您启动或停止服务,但它从列表中的第一个服务开始,并循环遍历所有服务。 如何获取DropDownList仅对特定行的服务名称而不是整个Gridview进行操作? 这些服务将添加到绑定到GridView的列表中。 谢谢。

    protected void ddlAction_SelectedIndexChanged(object sender, EventArgs e)
{
    ConnectionOptions options = new ConnectionOptions();
    options.Username = "myUsername";
    options.Password = "myPassword"; 
    options.EnablePrivileges = true;
    serverName.Text = "myServerName";

    if (txtbox2.Text != string.Empty)
    {
        //Create the scope that will connect to the default root for WMI
        var scope = new ManagementScope(string.Format(@"\\{0}\root\cimv2", serverName.Text), options);
        scope.Connect();

        //Create a path to the services with the default options
        ObjectGetOptions option = new ObjectGetOptions(null, TimeSpan.MaxValue, true);
        ManagementPath spoolerPath = new ManagementPath("Win32_Service");
        ManagementClass servicesManager = new ManagementClass(scope, spoolerPath, option);
        try
        {
            //Get all of the services running on this server
            using (ManagementObjectCollection services = servicesManager.GetInstances())
            {
                foreach (ManagementObject service in services)
                {
                    list.Add(new myServers() { Name = service["Name"].ToString(), State = service["State"].ToString(), Servers1 = serverName.Text });

                    GridViewRow gvr = ((DropDownList)sender).NamingContainer as GridViewRow;

                    if (gvr != null)
                    {
                        //We can find all the controls in this row and do operations on them
                        var ddlQuantity = gvr.FindControl("ddlAction") as DropDownList;
                        if (ddlQuantity != null)
                        {
                            if (ddlQuantity.SelectedValue != "-1")
                            {
                                if (ddlQuantity.SelectedValue == "1")
                                {
                                    service.InvokeMethod("StartService", null);
                                }
                                else if (ddlQuantity.SelectedValue == "2")
                                {
                                    service.InvokeMethod("StopService", null);
                                }
                            }
                        }
                    }
                }

            }
        }
        catch (UnauthorizedAccessException)

        {
            throw;
        }
        gvServer.DataSource = list;

        gvServer.DataBind();
    }
}

GridViewSelectedIndex属性:

int i = yourGridView.SelectedIndex;

然后,如果不需要,则在代码中删除foreach ,并更改调用行以对i元素执行操作:

services[i].InvokeMethod("StartService", null);

并停止服务:

services[i].InvokeMethod("StopService", null);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM