在SelectedIndex上启用Textbox更改

时间:2013-07-17 18:32:25

标签: c# asp.net selectedindex

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

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

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

以下是我到目前为止的内容,我正在努力的部分是selectedindex更改,我不知道如何对其进行编码,以便在下拉框中选择“修改”时,相应的文本框将变为启用状态。

ASPX页面

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

CS页

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)
    {

    }

1 个答案:

答案 0 :(得分: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;
    }
}