C#用作全局的功能

时间:2018-03-20 08:55:18

标签: c# asp.net

我的页面中有多个下拉列表。要加载项目,我使用以下代码:

force.on("tick", function() {
    link.attr("x1", function(d) {
            return d.source.x;
        })
        .attr("y1", function(d) {
            return d.source.y;
        })
        .attr("x2", function(d) {
            return d.target.x;
        })
        .attr("y2", function(d) {
            return d.target.y;
        });

    node.attr("transform", function(d) {
        if (d.fixed != true) {
            return "translate(" + d.x + "," + d.y + ")";
        } else {
            return "translate(" + d.px + "," + d.py + ")";
        }

        // here is what I tried to do
        if (d.x < 960 && d.y < 600) {
            vis.attr("viewBox", "0 0 960 600");
        } else if (d.x > 960 && d.y < 600) {
            vis.attr("viewBox", "0 0 " + d.x + " 600");
        } else if (d.x < 960 && d.y > 600) {
            vis.attr("viewBox", "0 0 960 " + d.y);
        } else {
            vis.attr("viewBox", "0 0 " + d.x + " " + d.y)
        }
    });
});

然后我在页面加载时调用上面的函数。

我的问题是,如何在上面的代码中创建单个函数,其中代码用于多个下拉列表?

例如,我只需要更改查询和下拉列表ID。

1 个答案:

答案 0 :(得分:2)

将DropDownList和Query作为参数传递

protected void GetItemList(DropDownList ddl, string query, string text, string value)
    {
        using (SqlCommand cmd = new SqlCommand(query))
        {
            cmd.CommandType = CommandType.Text;
            cmd.Connection = conn;
            conn.Open();
            ddl.DataSource = cmd.ExecuteReader();
            ddl.DataTextField = text;
            ddl.DataValueField = value;
            ddl.DataBind();
            conn.Close();
        }
    }

调用函数如:

GetItemList(cboGroup, "select ID, Group from TableGroup order by Group")