0

I am unable bind value from the list to the textbox after SelectedIndexChanged event. The dropboxlist has AutoPostBack=true. But it doesn't fire this event

<asp:DropDownList ID="ddlRSM" runat="server" AutoPostBack="true">

</asp:DropDownList>

The DropdownList is loaded from C# code method

        private void GetRMS(string _ct, DropDownList ddl)
    {
        SPWeb web = SPContext.Current.Web;
        //  web = SPContext.Current.Site.OpenWeb("myteam");
        SPUser currentUser = web.CurrentUser;
        SPQuery query = new SPQuery();
        SPList list = web.Lists.TryGetList("RSM");
        string strQuery = string.Empty;
        strQuery = "<FieldRef Name='Title'/>";
        strQuery += "<Where><Eq><FieldRef Name='CustomerType'/><Value Type='Choice'>{0}</Value></Eq></Where>";
        strQuery = string.Format(strQuery, _ct);
        query.Query = strQuery;
        DataTable dt = list.GetItems(query).GetDataTable();

        ddl.DataSource = dt;

        ddl.DataTextField = "Title";
        ddl.DataValueField = "Title";
        ddl.DataBind();
        ddl.Items.Insert(0, "<-- Select " + _ct + " -->");
        ddl.SelectedIndex = 0;
        int idx = ddl.SelectedIndex;
    }

and this is the event which is not triggered

        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {

        string _ct = ddlRSM.SelectedItem.Text;
        SPWeb web = SPContext.Current.Web;
        SPQuery query = new SPQuery();
        SPList list = web.Lists.TryGetList("myList");
        string strQuery = string.Empty;
        strQuery = "<FieldRef Name='Address'/>";
        strQuery += "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>{0}</Value></Eq></Where>";
        strQuery = string.Format(strQuery, _ct);
        query.Query = strQuery;
        SPListItemCollection items = list.GetItems(strQuery);
        tbShipTo.Text = items[items.Count - 1]["Address"].ToString();

    }
  • can you also post your markup code? – Mike Apr 8 '14 at 15:17
0

You need to define the method to call on OnSelectedIndexChanged event

Replace your drop-down markup with:

<asp:DropDownList ID="ddlRSM" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
</asp:DropDownList>
  • Excelent! Thanks, this worked. Now I am trying to get field from a list and bind it with Textbox.Text property any recommendations? – laspalmos Apr 8 '14 at 18:43
  • tbShipTo.Text = items[items.Count - 1]["Address"].ToString(); this didn't work ? – Arsalan Adam Khatri Apr 8 '14 at 18:51
  • No it didn't I've got this message "Value does not fall within the expected range". – laspalmos Apr 8 '14 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.