SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    402
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    [C# net4] DropDownList disabled after change value

    Hello guys, hope in your appreciated help.

    It possible disable one DropDownList after changed the value in the same DropDownList?

    How to intercept the change in DropDownList ?

    my problem is:

    I've one dropdownlist with or without selected value:
    Code:
                        <asp:DropDownList ID="txt" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Save_SelectedIndexChanged">
                        </asp:DropDownList>
    
    
    
    
    private void txtDDL()
        {
            txt.Items.Clear();
            txt.Items.Add(new ListItem("------", ""));
            txt.Items.Add(new ListItem("A", "A"));
            txt.Items.Add(new ListItem("B", "B"));
            txt.Items.Add(new ListItem("C", "C"));
            txt.Items.Add(new ListItem("D", "D"));
            txt.AppendDataBoundItems = true;
    
            mySQL = "SELECT txt FROM dotable WHERE txt =  ? ";
    
            OdbcCommand objCmd =
                new OdbcCommand(mySQL, myConnectionString);
            objCmd.Parameters.AddWithValue("param1", dbDDL.SelectedItem.Value);
            objCmd.CommandType = CommandType.Text;
            objCmd.CommandText = mySQL;
    
            try
            {
                myConnectionString.Open();
    
                OdbcDataReader dr = objCmd.ExecuteReader();
                while (dr.Read())
                {
                    if (dr["txt"].ToString() != "")
                    {
                        txt.DataTextField = "txt";
                        txt.DataValueField = "txt";
                        txt.DataBind();
                        txt.SelectedValue = dr["txt"].ToString();
    
                        if (dr["txt"].ToString() == "A" || dr["txt"].ToString() == "B")
                        {
                            txt.Enabled = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                myConnectionString.Close();
            }
        }
    when I select one new value of my txtDDL fired this event:

    Code:
    protected void Save_SelectedIndexChanged(object sender, EventArgs e)
    {
    OdbcCommand cmd = new OdbcCommand("update ....", myConnectionString);
    cmd.Parameters.AddWithValue("param1", txtDDL.SelectedItem.Value);
    cmd.Parameters.AddWithValue("param2", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
    try
    {
    myConnectionString.Open();
    cmd.ExecuteNonQuery();
    cmd.Dispose();
    
    StringBuilder sb = new StringBuilder();
    sb.Append("<script language=javascript>\n");
    sb.Append("alert("ok");
    sb.Append("window.history.go(-1);\n");
    sb.Append("\n</script>");
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", sb.ToString(), false);
    
    }
    finally
    {
    myConnectionString.Close();
    }
    }
    when the event is fired and return to home page I need disable the dropDown List.

    Any suggestion would be greatly appreciated
    Thank you in advance

  2. #2
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It may be better to add items in the page_load void then add the rest to the selectedindex void executing two SQL statements so the dropdownlist (txt) will have a value:

    mySQL = "SELECT txt FROM dotable WHERE txt = ? ";

    mySQL = "SELECT txt FROM dotable WHERE txt=" & txt.text & ";";

  3. #3
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,634
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    So you enjoy getting SQL injected?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •