没有设置UseSubmitBehavior =" false" ASP.NET按钮OnClick没有触发 [英] ASP.NET Button OnClick not firing without setting UseSubmitBehavior="false"
问题描述
我的问题出在asp.net Button 控件和 DropDownList 。
我有一个叫做Button的按钮 ApplyButton 和名为 FilterCombo 的DropDownList。
My problem is in asp.net Button control and DropDownList.
I have a Button called ApplyButton and a DropDownList called FilterCombo.
<asp:Button ID="ApplyButton" runat="server" Text="Apply Filter" OnClick="ApplyButton_Click" />
<asp:DropDownList ID="FilterCombo" runat="server" ></asp:DropDownList>
我想要使用我的DropDownList( FilterCombo ) SelectedIndex 在ApplyButton的 OnClick 事件中调用接受 int 作为参数的方法。但是当我点击按钮时,按钮的 Onclick 事件没有触发。但是如果我设置Button的 UseSubmitBehavior =false,它就有效。
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:Button ID="ApplyButton" runat="server" Text="Apply Filter" OnClick="ApplyButton_Click" UseSubmitBehavior="false" />
现在OnClick方法运行良好。但问题是 FilterCombo.SelectedIndex 总是返回 0 。为什么我不能在不设置 UseSubmitBehavior =false的情况下触发 Onclick 事件,如何获得 FilterCombo的正确 SelectedIndex ?
这是Page_Load的后端代码,
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,
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
Code for LoadFilters() - Used to load data to the 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事件
OnClick event of ApplyButton
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 + "");
}
更新:我想我找到了原因。但是不知道解决方案.. My Button和DropDownList在Div中,它作为 jQuery对话工作,由 JavaScript 函数调用。
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.
<%-- 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
Here is the JavaScript which invokes the Dialog
//Display JQuery Dialog
function showDialog() {
$("#loadFilterDialog").dialog({
draggable: true,
resizable: false,
width: 350,
height: 150,
minHeight: 10,
minwidth: 10
});
return false;
}
推荐答案
( #loadFilterDialog)。dialog({
draggable: true ,
可调整大小: false ,
宽度: 350 ,
height: 150 ,
minHeight: 10 ,
minwidth: 10
});
return false ;
}
("#loadFilterDialog").dialog({ draggable: true, resizable: false, width: 350, height: 150, minHeight: 10, minwidth: 10 }); return false; }
我找到了解决方案。那个Javascript代码应该是这样的..
I found the solution. That Javascript code should be like this..
//Display JQuery Dialog
function showDialog() {
( #loadFilterDialog)。dialog({
draggable: true ,
可调整大小: false ,
宽度: 350 ,
height: 150 ,
minHeight: 10 ,
minwidth: 10
});
("#loadFilterDialog").dialog({ draggable: true, resizable: false, width: 350, height: 150, minHeight: 10, minwidth: 10 });
这篇关于没有设置UseSubmitBehavior =" false" ASP.NET按钮OnClick没有触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!