简体   繁体   English

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

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

I have a DropDownList in a Gridview that allows you to start or stop services on remote servers. 我在Gridview中有一个DropDownList,可让您启动或停止远程服务器上的服务。 The DropDownList will allow you to start or stop services but it starts right from the first service in the list and loops through all of them. DropDownList将允许您启动或停止服务,但它从列表中的第一个服务开始,并循环遍历所有服务。 How can I get the DropDownList to only perform action on the service name for that specific row and not the entire Gridview? 如何获取DropDownList仅对特定行的服务名称而不是整个Gridview进行操作? The services are added to a list that is binded to the GridView. 这些服务将添加到绑定到GridView的列表中。 Thanks. 谢谢。

    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();
    }
}

There is the SelectedIndex property for the GridView : GridViewSelectedIndex属性:

int i = yourGridView.SelectedIndex;

Then in your code remove foreach if you don't need it, and change your invoking lines to perform actions on i element: 然后,如果不需要,则在代码中删除foreach ,并更改调用行以对i元素执行操作:

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

And to stop the service: 并停止服务:

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

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

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