【问题标题】:Asp.net DropDownList isPostBack is alwasy falseAsp.net DropDownList PostBack 总是假的
【发布时间】:2013-09-16 18:32:30
【问题描述】:

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

任何帮助将不胜感激!我很卡住,没有帮助就无法走得更远!谢谢!!

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      如果你仍然不能让它工作,我建议创建一个新表单并添加此示例中的标记和代码。

      【讨论】:

      • 感谢您的建议!我已经尝试了您的要求,但即使在创建新表单之后仍然没有运气!
      猜你喜欢
      • 1970-01-01
      • 2011-08-05
      • 1970-01-01
      • 2013-08-14
      • 1970-01-01
      • 2013-07-18
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      相关资源
      最近更新 更多