Solved

Problems with Dropdownlist and postback

Posted on 2006-06-12
12
398 Views
Last Modified: 2012-06-22
I am populating a page with user controls dynamically. One of the control two dropdownlists. Once you make a selection in the first dropdown it populates the second. Here is the problem it only works on the second try. When you make a selection the first time it posts back, but no results are returned, if you try again, it works perfectly. I will post some code in the hopes that someone can help me find an answer.

as always I appreciate your help.
CJSantora

aspcx:

    <tr>
        <td align="left"><asp:Label ID="lblSearchManufacturers" runat="server" Text="Search By Manufacturer"></asp:Label></td>
        <td style="width: 81px" align="left"><asp:DropDownList ID="cboMakes" DataTextField="name" DataValueField="Makeid" runat="server" AutoPostBack=true Width="200px" OnSelectedIndexChanged="cboMakes_SelectedIndexChanged"></asp:DropDownList></td>
    </tr>
    <tr>
        <td align="left"><asp:Label ID="lblSearchByModel" runat="server" Text="Search By Model" Width="149px"></asp:Label></td>
        <td style="width: 81px" align="left"><asp:DropDownList ID="cboModels" DataTextField="name" DataValueField="modelid" runat="server" Width="200px"></asp:DropDownList></td>
    </tr>

code bhind:

public partial class includes__public_pubQuicktruckLocator : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.SetFocus(cboMakes);
        populateMakes(0);
    }

    protected void cboMakes_SelectedIndexChanged(object sender, EventArgs e)
    {  
        int intmakeid = 0;            
        intmakeid = Convert.ToInt32(cboMakes.SelectedItem.Value);

        Response.Write(intmakeid);
        Response.Write(sender.ToString() + "<br>");
        Response.End();

        populateMakes(intmakeid);
        populateModels(intmakeid);
    }


    private void populateMakes(int intmakeid)
    {        
        cboMakes.DataSource = data.GetUniqueTrucksFromAds();
        cboMakes.DataBind();
        cboMakes.Items.Insert(0,new ListItem("All Makes","0"));
        cboMakes.Items.FindByValue(intmakeid.ToString()).Selected = true;
    }

    private void populateModels(int intmakeid)
    {
        cboModels.DataSource = data.GetUniqueTruckModelsFromAds(intmakeid);
        cboModels.DataBind();
        cboModels.Items.Insert(0, new ListItem("All Models", "0"));
    }

    protected void cmdQuickSearch_Click(object sender, ImageClickEventArgs e)
    {
        int intmakeid = 0;
        int intmodelid = 0;

        if (Convert.ToInt32(e.X) != 0 && Convert.ToInt32(e.Y) != 0)
        {

            if (cboMakes.SelectedIndex != 0)
            {
                intmakeid = Convert.ToInt32(cboMakes.SelectedItem.Value);
            }
            if (cboModels.SelectedIndex != 0)
            {
                intmodelid = Convert.ToInt32(cboModels.SelectedItem.Value);
            }
            Response.Redirect("~/default.aspx?pg=results&make=" + intmakeid + "&model=" + intmodelid);
        }
    }

}
0
Comment
Question by:CJSantora
  • 5
  • 4
12 Comments
 
LVL 25

Expert Comment

by:dstanley9
ID: 16894530
in this case, you don't need to DataBind() the Makes on each call.  In fact, re-binding the data will cause you to lose your SelectedItem value. try:

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.SetFocus(cboMakes);
        if(!IsPostBack)
          populateMakes(0);
    }
0
 

Author Comment

by:CJSantora
ID: 16895567
I really appreciate your feedback dstanley9. I tried your suggestion, however the dropdownlist does not populate under this condition. The control is activated by a tabbed imagemap, so I think when you click on the tab to display the truck search it causes a postback and this is why there was no data bound to the control.


CJSantora
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16895592
Can you move the code:

          populateMakes(0);

to the event hadler fired when the tab is selected?
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:CJSantora
ID: 16895799
I am not sure if that is a possibility or not, I will look into it. I have an additional question, and this may be a strech, but can you determine if the Postback was from a specific user control?

CJSantora
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16895835
I've never tried it myself, but you can try this method:

http://www.eggheadcafe.com/articles/20050609.asp
0
 

Author Comment

by:CJSantora
ID: 16896504
I thought I would post an additional finding that may offer some new ideas. The drowdownlist is using OnSelectedIndexChanged="cboMakes_SelectedIndexChanged to trigger the population of the next dropdownlist. I have determined that this is not happening on the first post, can anyone think of any reasons that the page would not see the OnSelectedIndexChanged="cboMakes_SelectedIndexChanged?

CJSantora
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16896597
IIRC, When you re-bind a control in Page_Load, it "clears" any events that would be triggered by the control (including SelectedIndexChanged).  That is one reason why you don't re-bind unless the request is not a post back.
0
 

Author Comment

by:CJSantora
ID: 16896685
I remmber you stating that in an earlier post and I continued trying to modify the code to eliminate that possibility. The code that is currently being executed does not do a second databind, but still does not return the proper id from the dropdownlist on the first post;

    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.Write(cboMakes.Items.Count + "<br>");
        if (cboMakes.Items.Count == 0)
        {
            populateMakes();
        }
        else
        {
            populateModels();
        }

    }

    private void populateMakes()
    {        
        cboMakes.DataSource = data.GetUniqueTrucksFromAds();
        cboMakes.DataBind();
        cboMakes.Items.Insert(0, new ListItem("All Makes", "0"));
    }

    protected void cboMakes_SelectedIndexChanged(object sender, EventArgs e)  // NOT BEING CALLED AT ALL NOW
    {
        Response.Write("Index Changed");
        populateModels();
    }

    private void populateModels()
    {

        int intmakeid = 0;
        intmakeid = Convert.ToInt32(cboMakes.Selecteditem.Value);  // IS ALWAYS RETURNING ZERO ON FIRST PASS
        Response.Write(intmakeid);

        cboModels.DataSource = data.GetUniqueTruckModelsFromAds(intmakeid);
        cboModels.DataBind();
        cboModels.Items.Insert(0, new ListItem("All Models", "0"));
    }

    protected void cmdQuickSearch_Click(object sender, ImageClickEventArgs e)
    {
        int intmakeid = 0;
        int intmodelid = 0;

        if (Convert.ToInt32(e.X) != 0 && Convert.ToInt32(e.Y) != 0)
        {

            if (cboMakes.SelectedIndex != 0)
            {
                intmakeid = Convert.ToInt32(cboMakes.SelectedItem.Value);
            }
            if (cboModels.SelectedIndex != 0)
            {
                intmodelid = Convert.ToInt32(cboModels.SelectedItem.Value);
            }
            Response.Redirect("~/default.aspx?pg=results&make=" + intmakeid + "&model=" + intmodelid);
        }
    }

I really appreciate your help.
CJSantora
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 500 total points
ID: 16896877
I think you may have PopulateModels() in the wrong spot:  try:

    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.Write(cboMakes.Items.Count + "<br>");
        if (cboMakes.Items.Count == 0)
        {
            populateMakes();
        }
        else
        {
            //do not populate here as it will be populated when the make index changes
            //populateModels();
        }

    }
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Powershell File Sort 8 45
abstract class C# 1 35
ModalPopup  question 22 36
Using/Import statement in code doesn't see reference of dll just added. 3 25
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question