LinkButtons in repeater
-
hey, I have a problem with my repeater. Im trying to place a link button within a repeater and then write code to handle its events but i cant get it to work... <%# Container.DataItem("ad_id") %> <%# Container.DataItem("ad_title")%>
<%#Container.DataItem("ad_body") %> <%# Container.DataItem("ad_price") %> <%# Container.DataItem("ad_city") %> <%# getSource(Container.DataItem("ad_source")) %> I want to do two things: 1) I must give to the checkbox an id so i know which checkbox in the repeater was checked. -- for that im now doing it with the tooltip as you can see in the code. Is there any other way? 2) When a checkbox is checkeded or a linkbutton is click i want to receive the event in the code behing and handle it there. It will be something like : Private Sub lbNewSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbNewSearch.Click getSelectedAds() Response.Redirect("keywordSearch.aspx") End Sub Though for some reason something like that doesnt work. It works for link buttons that are not within the repeater (as the code i paste which is a link button outside the repeater) but not for the ones inside the repeater. Any suggestions? Thank you very much in advance!!! -
hey, I have a problem with my repeater. Im trying to place a link button within a repeater and then write code to handle its events but i cant get it to work... <%# Container.DataItem("ad_id") %> <%# Container.DataItem("ad_title")%>
<%#Container.DataItem("ad_body") %> <%# Container.DataItem("ad_price") %> <%# Container.DataItem("ad_city") %> <%# getSource(Container.DataItem("ad_source")) %> I want to do two things: 1) I must give to the checkbox an id so i know which checkbox in the repeater was checked. -- for that im now doing it with the tooltip as you can see in the code. Is there any other way? 2) When a checkbox is checkeded or a linkbutton is click i want to receive the event in the code behing and handle it there. It will be something like : Private Sub lbNewSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbNewSearch.Click getSelectedAds() Response.Redirect("keywordSearch.aspx") End Sub Though for some reason something like that doesnt work. It works for link buttons that are not within the repeater (as the code i paste which is a link button outside the repeater) but not for the ones inside the repeater. Any suggestions? Thank you very much in advance!!!kakomalis wrote: Is there any other way? Because you can't directly assign to the id property, so you can make use of another property to keep that value such as the tooltip as you are using for the moment, or you can also use the name property and you can get the value in the code-behind like this:
string ad_id = chkBox.Attributes["name"];
In addition, you can use a hidden element to keep the ad_id value. kakomalis wrote: When a checkbox is checkeded or a linkbutton is click i want to receive the event in the code behing and handle it there. For the LinkButton control, you only need to provide code to hanlde the ItemCommand event of the Repeater control, and make use of the CommandArgument property to keep the ad_id value then it can be used later to do your bisuness:
<asp:LinkButton CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ad_id") %>'
Runat="server"><%# DataBinder.Eval(Container.DataItem, "ad_title") %></asp:LinkButton>private void Repeater1_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
string ad_id = e.CommandArgument;//TODO: Your code here.
}For the Checkbox control, you need to provide code to handle the CheckedChanged event of each individual checkbox. You should remember to set the AutoPostBack to true, otherwise the page does not post back to the server. The sample code is something like this:
<asp:CheckBox name='<%# DataBinder.Eval(Container.DataItem, "ad_id") %>'
Runat="server" OnCheckedChanged="CheckBox_CheckedChanged" AutoPostBack="True"></asp:CheckBox>protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkBox = sender as CheckBox;
string ad_id = chkBox.Attributes["name"];//TODO: your code here.
} -
kakomalis wrote: Is there any other way? Because you can't directly assign to the id property, so you can make use of another property to keep that value such as the tooltip as you are using for the moment, or you can also use the name property and you can get the value in the code-behind like this:
string ad_id = chkBox.Attributes["name"];
In addition, you can use a hidden element to keep the ad_id value. kakomalis wrote: When a checkbox is checkeded or a linkbutton is click i want to receive the event in the code behing and handle it there. For the LinkButton control, you only need to provide code to hanlde the ItemCommand event of the Repeater control, and make use of the CommandArgument property to keep the ad_id value then it can be used later to do your bisuness:
<asp:LinkButton CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ad_id") %>'
Runat="server"><%# DataBinder.Eval(Container.DataItem, "ad_title") %></asp:LinkButton>private void Repeater1_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
string ad_id = e.CommandArgument;//TODO: Your code here.
}For the Checkbox control, you need to provide code to handle the CheckedChanged event of each individual checkbox. You should remember to set the AutoPostBack to true, otherwise the page does not post back to the server. The sample code is something like this:
<asp:CheckBox name='<%# DataBinder.Eval(Container.DataItem, "ad_id") %>'
Runat="server" OnCheckedChanged="CheckBox_CheckedChanged" AutoPostBack="True"></asp:CheckBox>protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkBox = sender as CheckBox;
string ad_id = chkBox.Attributes["name"];//TODO: your code here.
}Thanks for your reply. Still though i cant get it to work. I have the following: <%# DataBinder.Eval(Container.DataItem, "ad_title") %> In the code behind: Private Sub rptResults_ItemCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Dim ad_id As String ad_id = e.CommandArgument End Sub It reloads the page which means that is doing the postback but it never gets in the code behind sub. When doing the ViewSource code from the web browser it looks like that: [fj40](javascript:__doPostBack('rptResults$_ctl4$Linkbutton1','')) Any ideas why it doesnt go through the code? Thanks in advance,
-
Thanks for your reply. Still though i cant get it to work. I have the following: <%# DataBinder.Eval(Container.DataItem, "ad_title") %> In the code behind: Private Sub rptResults_ItemCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Dim ad_id As String ad_id = e.CommandArgument End Sub It reloads the page which means that is doing the postback but it never gets in the code behind sub. When doing the ViewSource code from the web browser it looks like that: [fj40](javascript:__doPostBack('rptResults$_ctl4$Linkbutton1','')) Any ideas why it doesnt go through the code? Thanks in advance,
Hi there, From what I see in your code, the reason it doesn't work is that you didn't declare the handler for the ItemCommand event of the Repeater properly. The correct one should be like this:
Private Sub rptResults_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs)
Handles rptResults.ItemCommand
Dim ad_id As String
ad_id = e.CommandArgument
End SubThe
Handles rptResults.ItemCommand
is missing. For more information, see http://msdn.microsoft.com/library/en-us/vbcon/html/vbtskCreatingEventHandlersInWebFormsPages.asp[^]