0

我根据页面的 id 号从数据库中获取一些记录,将显示的记录数量会有所不同,从 1 条记录到 50 条记录。我需要获取这些记录,然后选择要修改、删除的记录,或者根本不会改变。我希望它看起来像这样:

无法使用stackoverflow图像上传器显示图像,所以在这里:https ://dl.dropboxusercontent.com/u/9446763/code/dropdownlist.jpg

如果选择了“无更改”或“删除”,我希望禁用文本字段,如果选择了“修改”选项,我希望启用修改的字段。

以下是我到目前为止所拥有的,我正在努力的部分是 selectedindex 更改我不知道如何对其进行编码,以便在下拉框中选择 Modify 时,启用相应的文本框。

ASPX 页面

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

客户服务页面

private void createControls()
    {
        var id = Request.Params["ID"];
        System.Data.OleDb.OleDbConnection pcn;
        System.Data.OleDb.OleDbCommand pcm;
        System.Data.OleDb.OleDbDataReader prs;
        pcn = new System.Data.OleDb.OleDbConnection("");
        pcm = new System.Data.OleDb.OleDbCommand();
        pcn.Open();
        pcm.Connection = pcn;
        var tableSql = @"select * FROM grouplist where ptid = '" + id + "'";
        pcm.CommandText = tableSql;
        prs = pcm.ExecuteReader();
        var rowcount = 0;
        while (prs.Read())
        {
            rowcount++; 

            PlaceHolder1.Controls.Add(new Literal() { Text = "<div class='row'><div class='span3'>" });

            TextBox tx = new TextBox();
            tx.ID = "txtData" + rowcount.ToString();
            tx.Text = prs["name"].ToString().Trim();
            tx.Width = 200;
            tx.CssClass = "span2";
            tx.Enabled = false;
            PlaceHolder1.Controls.Add(tx);

            PlaceHolder1.Controls.Add(new Literal() { Text = "</div><div class='span2'>" });

            TextBox txa = new TextBox();
            txa.ID = "amtData" + rowcount.ToString();
            txa.Text = prs["amt"].ToString();
            txa.CssClass = "span2";
            txa.Enabled = false;
            PlaceHolder1.Controls.Add(txa);

            PlaceHolder1.Controls.Add(new Literal() { Text = "</div><div class='span3'>" });

            DropDownList ddl = new DropDownList();
            ddl.Items.Add("No Change");
            ddl.Items.Add("Modify");
            ddl.Items.Add("Remove");
            ddl.Width = 200;
            ddl.CssClass = "span2";
            ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
            ddl.AutoPostBack = true;
            ddl.ID = "ddlData" + rowcount.ToString();

            PlaceHolder1.Controls.Add(ddl);

            PlaceHolder1.Controls.Add(new Literal() { Text = "</div></div>" });

        }
        prs.Close();
    }
  void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
4

1 回答 1

0

您需要在您的类中拥有对您的 DropDownList 和您想要启用/禁用的其他控件的引用的成员。

public class YourClass {
    private DropDownList ddl;
    private TestBox txa;

    private void createControls {
        // ...
        TextBox txa = new TextBox();
        txa.ID = "amtData" + rowcount.ToString();
        // ...
        DropDownList ddl = new DropDownList();
        ddl.Items.Add("No Change");
        // ... etc.
    }

    void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl.SelectedIndex == 1)
            txa.Enabled = true;
    }
}
于 2013-07-17T20:12:50.623 回答