Stephen Sugumar
Stephen Sugumar

Reputation: 545

Asp.net DropDownList isPostBack is alwasy false

I'm new to ASP and I've come across a problem that I've been at all day! im using a and my postback is constantly returning false, thus my selectedIndexChanged method never gets a chance to run!

Here is my code:

    <table border="1">
    <tr>
        <th>
            Build version:
        </th>
        <th>
            <%-- Html.DropDownList("Builds", null, new {@onchange = "onChange(this.value);" }) --%>
            <%-- Html.DropDownList("BuildID", (SelectList) ViewBag.Builds, "--Select One--") --%>
            <%-- Html.DropDownList("BuildDD", (IEnumerable<SelectListItem>)ViewBag.Builds, "--Select One--") --%>

            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="false" 
                DataSourceID="SqlDataSource1" DataTextField="Version" 
                onselectedindexchanged="DropDownList1_SelectedIndexChanged" 
                onprerender="DropDownList1_PreRender" onload="DropDownList1_Load">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:DAContext %>" 
                SelectCommand="SELECT [Version] FROM [Builds]" >
                </asp:SqlDataSource>
        </th>
        <th>
            <asp:Label ID="Label1" runat="server" Text= "--Build Version--"></asp:Label>
        </th>
    </tr>
</table>

and my code behind (it's in the same aspx file as the dropdownlist, not sure if thats alright)

<script runat="server">

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    Response.Write((sender as DropDownList).SelectedItem.Text);
    Label1.Text = DropDownList1.SelectedItem.Text;


}

protected void DropDownList1_PreRender(object sender, EventArgs e)
{
    base.OnPreInit(e);
    DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
}

protected void DropDownList1_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        Response.Write("Post Back is False");
        DropDownList1.Items.Clear();
        DropDownList1.DataSourceID = "SqlDataSource1";
        DropDownList1.DataTextField = "Version";
        DropDownList1.DataBind();
    }
}

Any help would be appreciated! im pretty stuck and can't get much further without help! thanks!!

Upvotes: 0

Views: 2934

Answers (2)

afzalulh
afzalulh

Reputation: 7943

EDIT: This code is for Webforms. It will not work in MVC.

First, make sure that you have the attribute in the page: AutoEventWireup = "true". It may look something like:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind= ...

Now remove the OnPreRender and Onload from Dropdown. Your cleaned up markup may look:

<table border="1">
    <tr>
        <th>Build version:
        </th>
        <th>
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
                DataSourceID="SqlDataSource1" DataTextField="Version"
                OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:DAContext %>"
                SelectCommand="SELECT [Version] FROM [Builds]"></asp:SqlDataSource>
        </th>
        <th>
            <asp:Label ID="Label1" runat="server" Text="--Build Version--"></asp:Label>
        </th>
    </tr>
</table>

In the code remove DropDownList1_PreRender and DropDownList1_Load method. In page_load check if it is postback and if not, databind the dorpdown. Your code may look like below:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Response.Write("Post Back is False");
        DropDownList1.Items.Clear();
        DropDownList1.DataSourceID = "SqlDataSource1";
        DropDownList1.DataTextField = "Version";
        DropDownList1.DataBind();
    }
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    Response.Write((sender as DropDownList).SelectedItem.Text);
    Label1.Text = DropDownList1.SelectedItem.Text;
}
//Commented the following methods
//protected void DropDownList1_PreRender(object sender, EventArgs e)
//{
//    base.OnPreInit(e);
//    DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
//}

//protected void DropDownList1_Load(object sender, EventArgs e)
//{
//    if (!this.IsPostBack)
//    {
//        Response.Write("Post Back is False");
//        DropDownList1.Items.Clear();
//        DropDownList1.DataSourceID = "SqlDataSource1";
//        DropDownList1.DataTextField = "Version";
//        DropDownList1.DataBind();
//    }
//}

If you still can not get it work, I would suggest to create a new form and add the markup and code from this example.

Upvotes: 0

Damith
Damith

Reputation: 63065

set AutoPostBack="true" in your dropdown list

Upvotes: 1

Related Questions