【问题标题】:Dropdownlist changes all values in Gridview when selected and not specific row下拉列表在选择而不是特定行时更改 Gridview 中的所有值
【发布时间】:2018-05-27 14:31:35
【问题描述】:

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

【问题讨论】:

  • 您正在foreach 循环中调用您的方法。你需要知道哪个下拉索引发生了变化(比如一些i),然后只为services[i]调用你的方法
  • 谢谢马克。你能提供一个示例代码吗?
  • 很难知道你需要什么 foreach 循环和其他方法以及你的观点是什么。但希望答案是有用的。使用CurrentCell.RowIndex获取当前选中行的索引
  • 我将需要 foreach,因为稍后我需要挑选出某些服务。例如 .Contains 或 .Endswith。截至目前,我只是想让下拉菜单按预期运行。

标签: c# asp.net gridview drop-down-menu foreach


【解决方案1】:

SelectedIndexGridView 属性:

int i = yourGridView.SelectedIndex;

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

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

并停止服务:

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

【讨论】:

  • 感谢 Mark 为我提供示例和解释。我需要保留foreach。 GridView 也没有 CurrentCell 属性。我需要将 GridView 更改为 DataGridView 吗?
  • 谢谢。我会尝试一下。我真的很感激!
猜你喜欢
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-06
  • 2015-08-15
  • 2017-09-07
相关资源
最近更新 更多