0

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.

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>

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"/>

relevant 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
}

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.

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?

dudledok
  • 2,800
  • 5
  • 24
  • 36

2 Answers2

2

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.

The server-side Click event will fire on the server, but doesn't allow you to pass a parameter. You will only get the button instance and an empty EventArgs instance.

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

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

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));
}
Richard Deeming
  • 29,830
  • 10
  • 79
  • 151
  • I knew I shouldn't have used OnClientClick, but when searching so many things at once I lost track. Haven't used OnCommand before. Works like a charm right now. Thanks. – dudledok Jul 26 '13 at 15:19
1

Well, that's the common problem which I think every asp.net developer deals some time. The common part of it, that asp.net event system doesn't work, as windows forms. Page object, and all controls on that page, have lifecycle events, that are triggered during any request, even when it's from update panel. As you create those controls by code, you have to keep in mind, that all events for those controls should work as part of Page object. That's why you have to create those object in Page_Init event, before all other control's event would be triggered. Please also keep in mind that you have to create those controls as asp.net objects:

var btn = new Button();

But not by simply adding html markup. And you have to recreate them on each request, following that one, when they were created.

Please take a look on my another answer.

Community
  • 1
  • 1
Johnny_D
  • 4,592
  • 3
  • 33
  • 63
  • Well the filtering works with the hard coded button, but not with programmatically html like you say. I'll have to look into how to create the buttons. – dudledok Jul 26 '13 at 15:53