Asp.net DropDownList isPostBack是alwasy false

时间:2013-09-16 18:32:31

标签: asp.net drop-down-menu webforms postback ispostback

我是ASP的新手,我遇到了一整天都遇到的问题!我使用a和我的回发不断返回false,因此我的selectedIndexChanged方法永远不会有机会运行!

这是我的代码:

    <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>

和我的代码背后(它与下拉列表在同一个aspx文件中,不确定是否正常)

<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();
    }
}

任何帮助将不胜感激!我很卡住,没有帮助就无法进一步发展!谢谢!

2 个答案:

答案 0 :(得分:1)

在下拉列表中设置AutoPostBack="true"

答案 1 :(得分:0)

编辑:此代码适用于Webforms。它不适用于MVC。

首先,请确保您在页面中拥有该属性:AutoEventWireup = "true"。它可能看起来像:

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

现在从Dropdown中删除OnPreRender和Onload。您清理过的标记可能会显示:

<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>

在代码中删除DropDownList1_PreRender和DropDownList1_Load方法。在page_load中检查它是否是回发,如果没有,则数据绑定dorpdown。您的代码可能如下所示:

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();
//    }
//}

如果仍然无法使其正常工作,我建议您创建一个新表单并添加此示例中的标记和代码。