Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My problem is in asp.net Button control and DropDownList.

I have a Button called ApplyButton and a DropDownList called FilterCombo.
ASP.NET
<asp:Button ID="ApplyButton" runat="server" Text="Apply Filter" OnClick="ApplyButton_Click" />

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

I want to call a method which accept a int as a parameter using my DropDownList's (FilterCombo) SelectedIndex In ApplyButton's OnClick event. But Onclick event of Button is not firing when I click on the Button. But it works if I set Button's UseSubmitBehavior="false".
ASP.NET
<asp:Button ID="ApplyButton" runat="server" Text="Apply Filter" OnClick="ApplyButton_Click" UseSubmitBehavior="false" />

Now the OnClick method is firing well. But the problem is FilterCombo.SelectedIndex always returns 0. Why can't I fire the Onclick event without setting UseSubmitBehavior="false" and How can I get the correct SelectedIndex of FilterCombo ?

Here is the backend code for Page_Load,
C#
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)
    {

    }
}


Code for LoadFilters() - Used to load data to the FilterCombo
C#
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();
}


OnClick event of ApplyButton
C#
protected void ApplyButton_Click(object sender, EventArgs e)
{
    SearchUtils util = new SearchUtils();
    int Id = int.Parse(FilterCombo.SelectedValue);
    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 + "");
}


Update : I think i found the reason. But don't know a solution.. My Button and DropDownList are in a Div which is working as a jQuery Dialog which is invoke by a JavaScript function.

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


Here is the JavaScript which invokes the Dialog
JavaScript
//Display JQuery Dialog 
function showDialog() {
    $("#loadFilterDialog").dialog({
		draggable: true,
		resizable: false,
		width: 350,
		height: 150,
		minHeight: 10,
		minwidth: 10
	});
    return false;
}
Posted
Updated 1-Aug-14 9:58am
v4
Comments
member33 1-Aug-14 10:42am    
is your dropdown databind inside or outside the Ispostback , that might be the issue

move databind inside the not ispostback
CR2411 1-Aug-14 10:47am    
It is inside the,
if (!IsPostBack){}
member33 1-Aug-14 10:49am    
show us the code behind for button click n page load
CR2411 1-Aug-14 10:59am    
Check the code added..
member33 1-Aug-14 11:07am    
Everything seems to be right. You will have to make UseSubmitBehavior True or completely take out that property ..button click shoud work either way
Think that is causing the issue

1 solution

I found the solution. That Javascript code should be like this..

JavaScript
//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;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900