Reputation: 91
I am rather new at asp.net.
I´m trying to use ImageButton to link to another page. ImageButton is inside a Repeater and the code is following:
<ul id = "ulMap">
<asp:Repeater ID="Repeater1" runat="server" >
<ItemTemplate>
<li>
<asp:ImageButton ID="ImageButton1"
CommandArgument='<%#Eval("Nav_ID") %>'
runat="server"
ImageUrl="~/Icons/Ny_mappa.png"
onclick="ImageButton1_Click" />
<br />
<asp:LinkButton ID="lnkButton"
CommandArgument='<%#Eval("Nav_ID") %>'
runat="server"
onclick="LinkButton3_Click"
Text='<%#Eval("Nav_Name") %>'></asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
And the code behind is the following:
protected void LinkButton3_Click(object sender, EventArgs e)
{
Guid guid = new Guid(((LinkButton)sender).CommandArgument);
var query = from n in dc.Nemanet_Navigations
where n.UserId == userGuid && n.Nav_pID == guid
orderby n.Nav_Name ascending
select n;
Repeater1.DataSource = query;
Repeater1.DataBind();
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Guid guid = new Guid(((ImageButton)sender).CommandArgument);
var query = from n in dc.Nemanet_Navigations
where n.UserId == userGuid && n.Nav_pID == guid
orderby n.Nav_Name ascending
select n;
Repeater1.DataSource = query;
Repeater1.DataBind();
}
Well, for the LinkButton
this works fine but not for ImageButton
. Protected void ImageButton1_Click
never happens. Can anybody help?
Upvotes: 0
Views: 7822
Reputation: 10941
Instead of OnClick, you should you should OnCommand. Like this.
<ul id = "ulMap">
<asp:Repeater ID="Repeater1" runat="server" >
<ItemTemplate>
<li>
<asp:ImageButton ID="ImageButton1"
CommandArgument='<%#Eval("Nav_ID") %>'
runat="server"
ImageUrl="~/Icons/Ny_mappa.png"
OnCommand="ImageButton1_Click" />
<br />
<asp:LinkButton ID="lnkButton"
CommandArgument='<%#Eval("Nav_ID") %>'
runat="server"
OnCommand="LinkButton3_Click"
Text='<%#Eval("Nav_Name") %>'></asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
And this (notice the CommandEventArgs instead of EventArgs and ImageClickEventArgs):
protected void LinkButton3_Click(object sender, CommandEventArgs e)
{
Guid guid = new Guid(e.CommandArgument);
var query = from n in dc.Nemanet_Navigations
where n.UserId == userGuid && n.Nav_pID == guid
orderby n.Nav_Name ascending
select n;
Repeater1.DataSource = query;
Repeater1.DataBind();
}
protected void ImageButton1_Click(object sender, CommandEventArgs e)
{
Guid guid = new Guid(e.CommandArgument);
var query = from n in dc.Nemanet_Navigations
where n.UserId == userGuid && n.Nav_pID == guid
orderby n.Nav_Name ascending
select n;
Repeater1.DataSource = query;
Repeater1.DataBind();
}
Upvotes: 2
Reputation: 73113
You can't handle server-side events for repeater child controls like this.
Think of it this way - if there is 100 buttons, you need 100 event handlers for each button. ImageButton1_Click
will only work for the ImageButton with the id of 1.
The answer is to wire up the ItemCommand
event for your repeater.
Like this:
<asp:repeater id = "Repeater`" runat = "server" onItemCommand = "SomeEvent">
Then you handle the "SomeEvent" for all buttons that trigger this event:
protected void SomeEvent ( Object src, RepeaterCommandEventArgs e ) {
var whoClickedMe = ((ImageButton) e.CommandSource );
}
Here's a good article on doing this.
Upvotes: 1
Reputation: 1255
I thin you need to handle the Repeater_ItemCommand event to get this working properly. It'll probably save a bit of code as well.
Also, if you're linking to another page do you need to postback to the page then perform some kind of redirection or would it make more sense to have that logic in the new page?
Upvotes: 3