Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. LinkButtons in repeater

LinkButtons in repeater

Scheduled Pinned Locked Moved ASP.NET
dockerhelpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kakomalis
    wrote on last edited by
    #1

    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!!!

    M 1 Reply Last reply
    0
    • K kakomalis

      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!!!

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

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

      A 1 Reply Last reply
      0
      • M minhpc_bk

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

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        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,

        M 1 Reply Last reply
        0
        • A Anonymous

          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,

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          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 Sub

          The Handles rptResults.ItemCommand is missing. For more information, see http://msdn.microsoft.com/library/en-us/vbcon/html/vbtskCreatingEventHandlersInWebFormsPages.asp[^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups