Listbox Returns an empty string on the SelectedIndexChanged event

The above error occurs when the clickIndexChanged event is listed.

when debugging, the return value is "", however, when you look at the source code of a web page, there are certain values.

Here is my list:

<asp:ListBox ID="lstBxMyList" runat="server" CssClass="binorderlst1"
     DataTextField="myName" 
     DataValueField="myID" 
     OnSelectedIndexChanged ="lstBxMyList_SelectedIndexChanged"
     AutoPostBack="true" Rows="10">
</asp:ListBox>

Here's the event:

protected void lstBxMyList_SelectedIndexChanged(object sender, EventArgs e)
{
    myID = Convert.ToInt32(lstBxSiteList.SelectedValue.ToString());

    ... rest of code
}

For completeness, data binding is here:

private void BindLstBxMyList(int myOtherID)
    {
        DataTable dt = new DataTable();
        SqlConnection conn;
        SqlCommand comm;

        using (conn = new SqlConnection(aSHconns.aconn))
        {
            comm = new SqlCommand("myStoredProc", conn);
            comm.CommandType = CommandType.StoredProcedure;
            comm.Parameters.Add(new SqlParameter("@myOtherID", SqlDbType.Int));
            comm.Parameters["@myOtherID"].Value = myOtherID;
            SqlDataAdapter sqlDa = new SqlDataAdapter(comm);

            try
            {
                conn.Open();
                 sqlDa.Fill(dt);
                 if (dt.Rows.Count > 0)
                 {
                     lstBxMyList.DataSource = dt;
                     lstBxMyList.DataTextField = "myName";
                     lstBxMyList.DataValueField = "myID";
                     lstBxMyList.DataBind();
                 }

            }

            finally 
            {
                conn.Close();
            }
        }

    }

If I go back to SqlDataSource, the list will display the values. Bt I need to re-populate the list, so I need code for data binding (unless, of course, there is no better way)

So why does the selected listbox value return an empty string? Any help would be greatly appreciated.

+3
source share
1 answer

AutoPostBack = "True", . , , , , . , , . , .

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
             //Bind your listbox here
            }
         }
+5

All Articles