Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I bind my dropdownlist in Webpart through SPService like this:

JavaScript
$(document).ready(function () {
       FillDropDown();
   });
 function FillDropDown() {
       var index = 0;
       $("#filterCountry").empty();
 $().SPServices({
           operation: "GetList",
           listName: "Pages",
           async: false,
           completefunc: function (xData, Status) {
               $(xData.responseXML).find("Field[DisplayName='Country'] CHOICE").each(function () {
                   //if (index == 0) {
                   //    $(".filterCountry").append("<option value='0'>--Select--</option>");
                   //}
                   listItem = "<option value=" + $(this).text() + ">" + $(this).text() + "</option>";
                   $("#filterCountry").append(listItem);
                   index++;
               })
 }
       })


dropdownlist design :

ASP.NET
  <div class="country">
            <span class="pull-left mar-top-5">Country</span>
            <asp:DropDownList ID="filterCountry" runat="server" ClientIDMode="Static" CssClass="form-control pull-right country_drop_down"></asp:DropDownList>
           
        </div>
<div class="go">           
            <asp:Button runat="server" CssClass="btn btn-primary" ID="btnSubmit" OnClick="btnSubmit_Click" Text="Go" />
        </div>


Codeside:

C#
 protected void btnSubmit_Click(object sender, EventArgs e)
        {
           
        ViewState["Query"]) = null;
//            BindReapterMain(Convert.ToString(ViewState["Query"]));
        }


The issue is when i click on Button i get dropdownlist selectedindex always -1 instead of getting selectedindex = 2 that i have selected.. Please help to any solution
Posted
Updated 2-Jun-14 3:29am
v2
Comments
Do you have anything inside Page Load at code behind?
KaushalJB 2-Jun-14 9:55am    
This is my PageLoad... i bind a repeatercontrol with CAML Query


protected void Page_Load(object sender, EventArgs e)
{

if (!Page.IsPostBack)
{
ViewState["Query"] = "<where><contains><fieldref name="PublishingPageLayout"><value type="URL">News<orderby><fieldref name="Title" ascending="False">";
BindReapterMain(Convert.ToString(ViewState["Query"]));
}
}
Not sure. But my guess is that something is causing the DropDownList to bind again and it is done just before it is going to the Button Click Event. So, debug and find out the issue.

You can take a look at to a similar issue I described in one of my Blogs - Why DropDownList SelectedValue Does Not Work Inside SelectedIndexChanged Event?

1 solution

I agree with Tadit, just want to add .. have you checked if "listItem" (in Javascript method) contains data before you are binding it to dropdown
 
Share this answer
 
Comments
This should be a comment, not answer. :)

Thanks,
Tadit
KaushalJB 4-Jun-14 5:32am    
Yes it will always contain data in ListItem...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900