Asp.net的DropDownList的IsPostBack是alwasy假(Asp.net Dr

2019-10-18 15:20发布

我是新来的ASP和我遇到,我一直在一整天的问题! IM使用我的回发不断返回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();
    }
}

任何帮助,将不胜感激! 即时通讯相当卡住,无法得到更进一步没有帮助! 谢谢!!

Answer 1:

设置AutoPostBack="true"在你的下拉列表



Answer 2:

编辑:该代码可用于Web表单。 它不会在MVC工作。

首先,确保你在页面属性: AutoEventWireup = "true" 。 它可能看起来是这样的:

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

现在,请从下拉列表中的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();
//    }
//}

如果您仍然无法得到它的工作,我建议创建一个新的形式,并从这个例子中添加的标记和代码。



文章来源: Asp.net DropDownList isPostBack is alwasy false