没有设置UseSubmitBehavior =“false”的ASP.NET按钮OnClick没有触发

时间:2014-08-01 13:17:44

标签: c# asp.net onclick postback selectedindex

我的问题出在asp.net Button控件和DropDownList。

我有一个名为ApplyButton的Button和一个名为FilterCombo的DropDownList。

<asp:Button ID="ApplyButton" runat="server" Text="Apply Filter" OnClick="ApplyButton_Click" />

<asp:DropDownList ID="FilterCombo" runat="server" ></asp:DropDownList>

我想调用一个接受int作为参数的方法,使用我的DropDownList&#39; s FilterCombo} SelectedIndex在ApplyButton&#39; s OnClick事件。但是当我点击Button时,Button的Onclick事件没有触发。但是如果我设置了Button UseSubmitBehavior="false",它就有效。

<asp:Button ID="ApplyButton" runat="server" Text="Apply Filter" OnClick="ApplyButton_Click" UseSubmitBehavior="false" />

现在OnClick方法很好用。但问题是FilterCombo.SelectedIndex始终返回0。为什么我无法在未设置Onclick的情况下触发UseSubmitBehavior="false"事件?如何才能获得SelectedIndex FilterCombo的正确{1}}?

以下是Page_Load的后端代码,

protected void Page_Load(object sender, EventArgs e)
{
    LeftSideBarHolder.Controls.Add(Page.LoadControl("~/Pages/Common_wa/LeftPanel.ascx"));
    HeaderHolder.Controls.Add(Page.LoadControl("~/Pages/Common_wa/Header.ascx"));

    try
    {
        string columns = Request["columns"];
        string[] arr = columns.Split(';');
        pkey = bool.Parse(arr[0]);
        leader = bool.Parse(arr[1]);
        type = bool.Parse(arr[2]);
        level = bool.Parse(arr[3]);
        state = bool.Parse(arr[4]);
        dueDate = bool.Parse(arr[5]);
    }
    catch (Exception ex)
    {
        //do nothing : This is the parameterless request
    }

    if (!IsPostBack)
    {
        Owner = int.Parse(Session["userID"].ToString());
        ViewState["PreviousPage"] = Request.UrlReferrer;
        LoadFilters();

        if (pkey) pKeyCheckBox.Checked = true;
        if (leader) LeaderCheckBox.Checked = true;
        if (type) TypeCheckBox.Checked = true;
        if (level) LevelCheckBox.Checked = true;
        if (state) StateCheckBox.Checked = true;
        if (dueDate) DueDateCheckBox.Checked = true;
    }
    try
    {
        DTO.Search.SearchResult SearchResult_new = (DTO.Search.SearchResult)Session["SearchResults"];
        Result = SearchResult_new.Result;
    }
    catch (Exception ex)
    {

    }
}

LoadFilters()代码 - 用于将数据加载到FilterCombo

private void LoadFilters()
{
    SearchUtils util = new SearchUtils();
    int Owner = int.Parse(Session["userID"].ToString());

    DataSet filters = util.GetFiltersOfOwner_AsDataSet(Owner);
    FilterCombo.DataSource = filters;
    FilterCombo.DataValueField = "Id";
    FilterCombo.DataTextField = "Name";
    FilterCombo.DataBind();
}

ApplyButton的OnClick事件

protected void ApplyButton_Click(object sender, EventArgs e)
{
    SearchUtils util = new SearchUtils();
    int Id = int.Parse(FilterCombo.SelectedItem.Value.ToString());
    SearchFilter filter = util.GetFilter(Id);
    string Columns = filter.Columns;
    string[] arr = Columns.Split(';');
    pkey = bool.Parse(arr[0]);
    leader = bool.Parse(arr[1]);
    type = bool.Parse(arr[2]);
    level = bool.Parse(arr[3]);
    state = bool.Parse(arr[4]);
    dueDate = bool.Parse(arr[5]);
    Response.Redirect("SearchResult_new.aspx?columns=" + pkey + ";" + leader + ";" + type + ";" + level + ";" + state + ";" + dueDate + "");
}

更新:我想我找到了原因。但是不知道解决方案...... 我的ButtonDropDownList位于Div,其作为jQuery Dialog工作,由JavaScript函数调用。

<%-- Load Filter Dialog Box --%>
<div id="loadFilterDialog" title="Apply Filter" style="display: none">
    <div class="BodyPanelDiv">
        <asp:DropDownList ID="FilterCombo" runat="server"></asp:DropDownList>
    </div>
    <div class="BottomPanelDiv" align="Right">
        <asp:Button ID="ApplyButton" runat="server" Text="Apply Filter" OnClick="ApplyButton_Click" UseSubmitBehavior="false" />
        <asp:Button ID="CancelButton2" runat="server" Text="Cancel" OnClientClick="return closeDialog(2); return false;" />
    </div>
</div>
<%-- End of Load Filter Dialog Box --%>

这是调用Dialog

的JavaScript
//Display JQuery Dialog 
function showDialog() {
    $("#loadFilterDialog").dialog({
        draggable: true,
        resizable: false,
        width: 350,
        height: 150,
        minHeight: 10,
        minwidth: 10
    });
    return false;
}

1 个答案:

答案 0 :(得分:3)

此答案标记在我的收藏夹中。要通过jQuery对话框使用.Net回发,您必须使用表单。好事是它是一个简单的解决方案;但我保留这个解决方案的书签,因为它是一个有点模糊的。

jQuery UI Dialog with ASP.NET button postback

所以上面的代码变成:

//Display JQuery Dialog 
function showDialog() {
  $("#loadFilterDialog").dialog({
    draggable: true,
    resizable: false,
    width: 350,
    height: 150,
    minHeight: 10,
    minwidth: 10
  });
  $("#loadFilterDialog").parent().appendTo($("form:first"));

  return false;
}