Reputation: 1549
I have a page where I show the characters of a user. Using a repeater, I create a div for each character with different informations on the characters taken from my DB. I want to be able to call a method server side that use my characterId as a parameter when I click on one of the divs.
I tried using onclick on the div tag. Using an anchor. Using a display:none button and call it when clicking the div, I tried several weird things that didn't make much sense to me. I'm getting out of ideas. I'm fine with using javascript if needed.
Here the code for my page
<asp:Repeater ID="VK_Character_CharacterNotSelected_rptCharacter" runat="server">
<ItemTemplate>
<div class="divCharacterNotSelected_divCharacterPanel"
onclick='<%# "SelectCharacter_Click("+Eval("CharacterId")+")" %>'
runat="server"
id="VK_Character_CharacterNotSelected_divCharacter">
And the codebehind
protected void SelectCharacter_Click(int characterId)
{
Stuff....
}
Thank you for any help provided.
Edit: After following King A.Majid answer, I can call the server side method with my parameter but I need to call the button onclick when I click on my div.
Edit 2: I thought about using ItemDataBound to set it but dunno how to set a div onclick
Edit 3: Using linkbutton instead but it mix up with the CSS I have on links. Here the image to show:
Edit 4: Problem solved. Just added extra CSS to my page to fix the link.
Upvotes: 1
Views: 4077
Reputation: 62301
You can use LinkButton
. It is rendered as a link on a browser so you can add whatever you want inside that link.
<asp:Repeater ID="VK_Character_CharacterNotSelected_rptCharacter" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="LinkButton1"
OnCommand="LinkButton1_Command"
CommandArgument='<%# Eval("CharacterId") %>'>
<div class="divCharacterNotSelected_divCharacterPanel">...</div>
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
var characterId = e.CommandArgument;
}
Upvotes: 1
Reputation: 3914
Bro,
to achieve your Goal use the Following , Define the Button as the Following inside the Repeater:
<asp:Button Id="SelectCharacter" CommandName="SelectCharachter" runat="server" CommandArgument='<%# Eval("CharachterId") %>' />
In Code Behind :
protected void VK_Character_CharacterNotSelected_rptCharacter_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "SelectCharachter":
string _SelectedCharachter=e.CommandArgument.ToString();
break;
}
}
So you dont Need to Handle the OnClick as long as its inthe repeater the Repeater_Command Event would take care of it:
Regards
Upvotes: 1