locked
Dropdown Event Not Firing, just resets to first value RRS feed

  • Question

  • User2037455357 posted

    Hello there

    I have a dropDownlist that has a OnSelectedIndexChanged event.

    The event only has a variable declaration with a checkpoint just so I can see the event is being fired. But it never does, instead the dropdown defaults to the first value.

    I have AutoPostBack = true on the dropdownlist. I cannot see what else im doing wrong. The dropdownlist id = QA_Detail_QAnumber_DropDown

    See aspx & c# below.

    --------------------------------------------   ASPX -----------------------------------------------------------------------

     

    <%@ Page Title="" Language="C#" MasterPageFile="~/Master.Master" AutoEventWireup="true" EnableEventValidation ="False" CodeBehind="Quality_Assurance_Detail.aspx.cs" Inherits="TestDropdowns.Quality_Assurance_Detail" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    
    
        <script src="js/bootstrap.min.js"></script>   
    <link href="css/bootstrap.min.css" rel="stylesheet" /> 
    
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
     
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    
    <style> 
    
        .Center {
                width: 100%;
                text-align: center;
        
            }
    
        .Panel_QADetail {
                /*padding:20pc 0px 24px 10px;*/
                ;
                margin-left:auto;
                margin-right:auto;
                margin-top:auto;
                margin-bottom:auto;
                width: 1300px;
                height: 600px;
                background-color: white;
                border: 1px solid black;
                left: 0px;
                top: 0px;
                
                
            }
    
        .Panel_Center {
                margin-left: auto;
                margin-right: auto;
                height: 200px;
                width: 800px;
                overflow: auto;
            }
    
          .modalbackground
            {
                background-color:black;
                filter:alpha(opacity=90);
                opacity:0.6;
                z-index:20;
            }
    
         
    
        .flex-container {
      display: flex;
      justify-content: center;
    }
    
         
    
        .auto-style3 {
            margin-right: 0px;
        }
    
         
    
    </style>
    
        <asp:UpdatePanel ID="UpdatePanel1"  runat="server">
        <ContentTemplate>
    
    
         <asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server"></asp:ScriptManager>
    
    
      <%--   <asp:Panel ID="Panel_QA_QualityAssuranceDetail" runat="server" CssClass="Panel_CreateQA" style="display:none;">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
    
    
    
         </ContentTemplate>
         </asp:UpdatePanel>
         </asp:Panel>
    
            ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender_QA_QualityAssuranceDetail" runat="server" BackgroundCssClass="modalbackground" PopupControlID="Panel_QA_QualityAssuranceDetail" TargetControlID="QA_Contract_CreateReview_Btn" CancelControlID="NewQA__Close_Btn"></ajaxToolkit:ModalPopupExtender>--%>
        
    
        
    
        <div class="Center">
    
    <asp:Label ID="Label35" runat="server" Text="Quality Assurance Detail" Font-Size="30" Font-Bold="True" ForeColor="#3366FF"></asp:Label>
    
    </div>
    
    
    
     <div>
    
    
         <table class="Center">
    
    <tr>
            <td>
    
         <asp:Label ID="QA_Detail_Contract_Lbl" runat="server" Text="Contract Legal Name"></asp:Label>
    
                        
            </td> 
    </tr>
    
    
    <tr>
            <td> 
         
                <asp:Label ID="QA_Detail_ContractName_Lbl" runat="server" Width="200px" Text="Label"></asp:Label>
    
            </td> 
     </tr>
    
             <tr>
            <td> 
    
    
         
                <asp:DropDownList ID="QA_Detail_QAnumber_DropDown" Width="500px" runat="server" DataTextField="Dropdown" DataValueField="Client_ID" AutoPostBack="true" OnSelectedIndexChanged="QA_Detail_QAnumber_DropDown_SelectedIndexChanged"></asp:DropDownList>
    
                <asp:SqlDataSource runat="server" ID="QA_Detail_QAnumber_DropDown_DataSource" ConnectionString='<%$ ConnectionStrings:KaizenSvr01ConnectionString %>' SelectCommand="SELECT [Client_ID], [Dropdown] FROM [vw_DropDown_Quality_Assurance]"></asp:SqlDataSource>
            </td> 
     </tr>
    
             </table>
    
     </div>
    
    
    
        <div class="flex-container">
    
            <div>
           
                <asp:Label ID="Label3" runat="server" Text="History"></asp:Label>
             
    <br />
                
    
                <asp:GridView ID="QA_Detail_QualityAssurance_GridView"  AutoPostBack="true" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" runat="server" AutoGenerateColumns="False" Style="margin-left: auto; " DataKeyNames="QA_Desc,QA_Resolution" OnSelectedIndexChanged="QA_Detail_QualityAssurance_GridView_SelectedIndexChanged" OnRowDataBound="QA_Detail_QualityAssurance_GridView_RowDataBound"  CssClass="auto-style3">
    
                 <Columns>
                     <asp:BoundField DataField="QA_ID" HeaderText="QA_ID" SortExpression="QA_ID"></asp:BoundField>
                     <asp:BoundField DataField="Created_By" HeaderText="Created_By" ReadOnly="True" SortExpression="Created_By"></asp:BoundField>
                     <asp:BoundField DataField="Date_Created" HeaderText="Date_Created" SortExpression="Date_Created"></asp:BoundField>
                 
                 
                 <asp:BoundField DataField="QA_Desc" HeaderText="QA_Desc" SortExpression="QA_Desc" />
                 <asp:BoundField DataField="QA_Resolution" HeaderText="QA_Resolution" SortExpression="QA_Resolution" />
                 </Columns>
             </asp:GridView>
    
             <asp:SqlDataSource runat="server" ConnectionString='<%$ ConnectionStrings:Risk_AssessmentConnectionString %>' SelectCommand="SELECT [QA_ID], [Created_By], [Date_Created] ,[QA_Desc] ,[QA_Resolution] FROM [vw_Quality_Assurance_Detail] ORDER BY [Date_Created] DESC"></asp:SqlDataSource>
    
    
    
    </div>
                     
    
             <div style="margin-left:10px;">
                 <div>
                         <asp:Label ID="Label2" runat="server" Text="Description"></asp:Label> <br />
                         <asp:TextBox ID="QA_Detail_Description_TxtBox" Width="700px" Height="200px" runat="server" TextMode="MultiLine"></asp:TextBox>
    </div>
                 <div>
                         <asp:Label ID="Label1" runat="server" Text="Resolution"></asp:Label> <br />
                         <asp:TextBox ID="QA_Detail_Resolution_TxtBox" Width="700px" Height="200px" runat="server" TextMode="MultiLine"></asp:TextBox>
                 </div>
                 
                 
                 </div>
                    
        
    
    
    
             </div>
    
            </ContentTemplate>
         </asp:UpdatePanel>
            
             
    </asp:Content>
    

    -------------------------------------- C# -----------------------------------------------------------------

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Data;
    using System.Drawing;
    
    namespace TestDropdowns
    {
        public partial class Quality_Assurance_Detail : System.Web.UI.Page
        {
    
            readonly SqlConnection DBconnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Risk_AssessmentConnectionString"].ToString());
    
    
            public void Populate_QA_GridView(int QA_id)
            {
    
                using (DBconnection)
    
                {
    
                    string SQL = "SELECT [QA_ID], [Created_By], [Date_Created],[QA_Desc] ,[QA_Resolution] FROM [vw_Quality_Assurance_History] WHERE [QA_ID] = " + QA_id + " ORDER BY [Date_Created] DESC";
    
    
    
                    DataSet ds = new DataSet();
                    DataTable PStable = new DataTable();
    
    
                    SqlDataAdapter Adapter = new SqlDataAdapter(SQL, DBconnection);
    
    
    
                    DBconnection.Open();
                    Adapter.Fill(ds, "QA_Detail");
    
    
    
                    PStable = ds.Tables[0];
    
    
    
                    DataView view = new DataView(PStable.DefaultView.ToTable());
    
    
    
    
                    QA_Detail_QualityAssurance_GridView.DataSource = null;
                    QA_Detail_QualityAssurance_GridView.DataSource = view;
                    QA_Detail_QualityAssurance_GridView.DataBind();
    
                    QA_Detail_QualityAssurance_GridView.Columns[3].Visible = false;
                    QA_Detail_QualityAssurance_GridView.Columns[4].Visible = false;
    
    
                    Adapter.Dispose();
                }
    
            }
    
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
                if (!this.IsPostBack)
    
                {
    
    
    
                        if (Request.QueryString["Quality_Assurance_QAId"] != null) // This calls the ID param from the  Horizon_Property_ID HyperLinkField on GridView from the CreateOrSearch.aspx
    
                    {
    
                        int QA_ID = Convert.ToInt32(Request.QueryString["Quality_Assurance_QAId"].ToString());
    
                        string QA_Detail_GridView = "SELECT [QA_ID], [Created_By], [Date_Created],[QA_Desc] ,[QA_Resolution] FROM [vw_Quality_Assurance_History] WHERE [QA_ID] = " + QA_ID + " ORDER BY [Date_Created] DESC";
    
    
                        using (DBconnection)
    
                        {
    
    
    
                            DataSet ds = new DataSet();
                            DataTable PStable = new DataTable();
                            String SqlQuery = QA_Detail_GridView;
    
                            SqlDataAdapter Adapter = new SqlDataAdapter(SqlQuery, DBconnection);
    
    
    
                            DBconnection.Open();
                            Adapter.Fill(ds, "QA_Detail");
    
    
    
                            PStable = ds.Tables[0];
    
    
    
                            DataView view = new DataView(PStable.DefaultView.ToTable());
    
    
    
    
                            QA_Detail_QualityAssurance_GridView.DataSource = null;
                            QA_Detail_QualityAssurance_GridView.DataSource = view;
                            QA_Detail_QualityAssurance_GridView.DataBind();
    
                            QA_Detail_QualityAssurance_GridView.Columns[3].Visible = false;
                            QA_Detail_QualityAssurance_GridView.Columns[4].Visible = false;
    
    
                            Adapter.Dispose();
    
                            ////////////////////////////////////////////////////////////////////////////
                            ///
    
                            string QA_Detail = "SELECT * FROM [dbo].[vw_Quality_Assurance_Detail] WHERE [ID] = " + QA_ID + "";
    
                            //DBconnection.Open();
    
                            SqlCommand command_QA_Detail = new SqlCommand(QA_Detail, DBconnection);
                            
    
                            SqlDataReader reader_QA_Detail = command_QA_Detail.ExecuteReader();
                            reader_QA_Detail.Read();
    
                            QA_Detail_ContractName_Lbl.Text = reader_QA_Detail["Legal_Name"].ToString();
    
                            int Client_ID = Convert.ToInt32(reader_QA_Detail["Client_ID"].ToString());
    
                            reader_QA_Detail.Close();
    
                            /////////////////////////////////////////////////////////////////////////////////////
                            ///
    
                            DataTable DT_QA_Detail_QAnumber = new DataTable();
    
                            string SQL_QA_Detail_QAnumber = "SELECT * FROM vw_DropDown_Quality_Assurance WHERE [Client_ID] = " + Client_ID;
    
                            SqlDataAdapter Adapter_QA_Detail_QAnumber = new SqlDataAdapter(SQL_QA_Detail_QAnumber, DBconnection);
                            Adapter_QA_Detail_QAnumber.Fill(DT_QA_Detail_QAnumber);
    
    
                            QA_Detail_QAnumber_DropDown.DataSource = DT_QA_Detail_QAnumber;
                            QA_Detail_QAnumber_DropDown.DataTextField = "DropDown";
                            QA_Detail_QAnumber_DropDown.DataValueField = "Client_ID";
                            QA_Detail_QAnumber_DropDown.DataBind();
    
                            Adapter_QA_Detail_QAnumber.Dispose();
    
    
    
    
                        }
    
                        
    
                    }
    
                }
    
            }
    
    
    
    
            protected void QA_Detail_QualityAssurance_GridView_SelectedIndexChanged(object sender, EventArgs e)
            {
    
    
                foreach (GridViewRow row in QA_Detail_QualityAssurance_GridView.Rows)
    
                {
    
                    if (row.RowIndex == QA_Detail_QualityAssurance_GridView.SelectedIndex)
    
                    {
    
                        QA_Detail_Description_TxtBox.Text = Convert.ToString(QA_Detail_QualityAssurance_GridView.DataKeys[row.RowIndex].Values[0]);
                        QA_Detail_Resolution_TxtBox.Text = Convert.ToString(QA_Detail_QualityAssurance_GridView.DataKeys[row.RowIndex].Values[1]);
    
    
    
    
    
                        row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
    
                        
    
    
                    }
    
                    else
    
                    {
    
                        row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
    
                        row.ToolTip = "Click to select this row.";
    
                    }
    
                }
    
            }
    
            protected void QA_Detail_QualityAssurance_GridView_RowDataBound(object sender, GridViewRowEventArgs e)
            {
    
                if (e.Row.RowType == DataControlRowType.DataRow)
    
                {
    
                    e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(QA_Detail_QualityAssurance_GridView, "Select$" + e.Row.RowIndex);
    
                    e.Row.ToolTip = "Click to select this row.";
    
                }
    
            }
    
            protected void QA_Detail_QAnumber_DropDown_SelectedIndexChanged(object sender, EventArgs e)
            {
    
                string test = "test";
    
                //Populate_QA_GridView(Convert.ToInt32(QA_Detail_QAnumber_DropDown.SelectedValue.ToString()));
    
            }
    }
    }

    Any help would be much appreciated 

    Regards Rob

    Friday, October 11, 2019 8:57 PM

Answers

  • User409696431 posted

    Do you have any duplicates in the "Value" fields of the dropdownlist?  It looks like you do, if they are all the same Client_ID. That will break the SelectedIndexChanged event.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 12, 2019 8:02 AM

All replies

  • User409696431 posted

    Do you have any duplicates in the "Value" fields of the dropdownlist?  It looks like you do, if they are all the same Client_ID. That will break the SelectedIndexChanged event.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 12, 2019 8:02 AM
  • User2037455357 posted
    O god. yes i do.
    just realised i’m using the wrong ID.

    Thank you. I shall come to you once i have corrected the problem.
    Saturday, October 12, 2019 8:28 AM
  • User409696431 posted

    Please mark my post as the answer.

    (Look at the source of your page and you'll see why the value must be unique.  The value is what is used to identify the <select> option.)

    Saturday, October 12, 2019 9:13 AM
  • User2037455357 posted
    I changed my view to include distinct values

    and that did the trick. Thank you

    School boy error !!!
    Saturday, October 12, 2019 10:41 AM