0

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

4

2 回答 2

1

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

于 2013-09-16T18:54:50.483 回答
0

编辑:此代码适用于 Webforms。它在 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();
//    }
//}

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

于 2013-09-16T21:15:59.020 回答