方法被触发页面加载,但不是通过ASP按钮 [英] Method being triggered on page load but not via asp button

查看:136
本文介绍了方法被触发页面加载,但不是通过ASP按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个的asp:商标,其中第一个被替换为几个按钮,并与项目的列表中的第二

I have two asp:Labels, the first of which is replaced with a few buttons and the second with a list of items.

我想点击的按钮来筛选项目。

I want to click on the buttons to filter the items.

按钮的内容是通过HTML替换文本和工作正常编程方式添加。

The contents of the buttons are added programmatically by replacing the text with html and works fine.

ASP:

<form id="form1" runat="server">
  <asp:Label id="filters" runat="server" Text="Filters here"/>
  <asp:Label id="itemList" runat="server" Text="List of items here"/>
</form>

合成器标签的HTML:

resultant html of filters label:

<input type="submit" onclientclick="Load_Items(0)" runat="server" value="First"/>
<input type="submit" onclientclick="Load_Items(1)" runat="server" value="Second"/>
<input type="submit" onclientclick="Load_Items(2)" runat="server" value="Third"/>

有关C#:

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    Load_Items(0);
  }
}
public void Load_Items(int filterType)
{
  //code to load items (pseudo below)
  for each row in list
    if filterType = itemType
      build string
  replace second label with string
}

在页面加载一切发生,就像我希望它与第一项被过滤的内容(因此 Load_Items(0)),如果我手动更改0在Page_Load中另一个号码,它过滤了其他类型的,但如果我点击它以编程方式添加的按钮,没有任何反应比什么样子的页面清新的其他。

On page load everything happens just as I want it to with the contents being filtered by the first item (hence Load_Items(0)), and if I manually change the 0 to another number in Page_Load, it filters by the other types, but if I click the buttons which are programmatically added, nothing happens other than what looks like the page refreshing.

我知道回发检查之前和它里面添加一个文本替换工作。

I know the post back check is working by adding a text replacement before and inside it.

我还添加了一个asp:按钮,以确保它不是做的按钮添加如下的方式(与搜索推荐一些额外的东西):

I've also added an asp:button to make sure it's not something to do with the way the buttons are added as below (with some extra things recommended from searches):

<asp:Button runat="server" CausesValidation="False" onclientclick="Load_Items(2); return false;" text="Submit" />

所以,可能是什么问题?

So what could be the issue?

推荐答案

指定的JavaScript单击该按钮时,在浏览器中运行的OnClientClick 属性。因为你可能没有所谓的JavaScript函数 Load_Items ,这会生成一个脚本错误,然后按钮,将导致窗体回发。

The OnClientClick property specifies the javascript to run in the browser when the button is clicked. Since you probably don't have a javascript function called Load_Items, this will generate a script error, and the button will then cause the form to post back.

服务器端 点击事件将触发服务器上,但不会让你传递参数。您只有按钮实例和一个空的 EventArgs的实例。

您可能会更好使用的 命令事件,用的 CommandArgument 属性

You might be better off using the Command event, combined with the CommandArgument property.

<asp:Button runat="server" CommandArgument="2" OnCommand="Load_Items" ...

该事件处理程序将使用的 CommandArgument 属性 CommandEventArgs 从点击的按钮访问参数:

The event handler would use the CommandArgument property of the CommandEventArgs to access the argument from the clicked button:

protected void Load_Items(object sender, CommandEventArgs e)
{
    Load_Items(Convert.ToInt32(e.CommandArgument));
}

这篇关于方法被触发页面加载,但不是通过ASP按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆