Event binding to each LinkButton of Repeater not working












0















I need set a button for each item of repeater in C#(asp.net).



<com:Repeater runat="server" ID="list_repeater">
<ItemTemplate>
<tr>
<td>
<asp:LinkButton runat="server" ID="btnCancel" CommandArgument='<%# Eval("id") %>'>Cancel</asp:LinkButton>
</td>
...
</tr>
</ItemTemplate>
</com:Repeater>


I bind data in PageLoad.



protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
list_repeater.DataSource = ... getData ..;
list_repeater.DataBind();
...
}
}


Of course, i bind RepeaterItemEventHandler on the list.



protected override void OnInit(EventArgs e)
{
base.OnInit(e);
base.LoadingControlAdd();
...
list_repeater.ItemDataBound += new RepeaterItemEventHandler(list_repeater_ItemDataBound);
...
}


and then i bind EventHandler to each btnCancel.



protected void list_repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton btnCancel = e.Item.FindControl("btnCancel") as LinkButton;
btnCancel.Click += new EventHandler(btnCancel_Click);
}
}


But, it does not work.
When i click the btnCancel of each item, btnCancel_Click is not triggered. I think i does well, and can't find any flaw. Is there any fault on the code above please tell me what's wrong. Thanks.
(id on CommnadArgument is set properly, don't mind it)










share|improve this question



























    0















    I need set a button for each item of repeater in C#(asp.net).



    <com:Repeater runat="server" ID="list_repeater">
    <ItemTemplate>
    <tr>
    <td>
    <asp:LinkButton runat="server" ID="btnCancel" CommandArgument='<%# Eval("id") %>'>Cancel</asp:LinkButton>
    </td>
    ...
    </tr>
    </ItemTemplate>
    </com:Repeater>


    I bind data in PageLoad.



    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    list_repeater.DataSource = ... getData ..;
    list_repeater.DataBind();
    ...
    }
    }


    Of course, i bind RepeaterItemEventHandler on the list.



    protected override void OnInit(EventArgs e)
    {
    base.OnInit(e);
    base.LoadingControlAdd();
    ...
    list_repeater.ItemDataBound += new RepeaterItemEventHandler(list_repeater_ItemDataBound);
    ...
    }


    and then i bind EventHandler to each btnCancel.



    protected void list_repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
    LinkButton btnCancel = e.Item.FindControl("btnCancel") as LinkButton;
    btnCancel.Click += new EventHandler(btnCancel_Click);
    }
    }


    But, it does not work.
    When i click the btnCancel of each item, btnCancel_Click is not triggered. I think i does well, and can't find any flaw. Is there any fault on the code above please tell me what's wrong. Thanks.
    (id on CommnadArgument is set properly, don't mind it)










    share|improve this question

























      0












      0








      0








      I need set a button for each item of repeater in C#(asp.net).



      <com:Repeater runat="server" ID="list_repeater">
      <ItemTemplate>
      <tr>
      <td>
      <asp:LinkButton runat="server" ID="btnCancel" CommandArgument='<%# Eval("id") %>'>Cancel</asp:LinkButton>
      </td>
      ...
      </tr>
      </ItemTemplate>
      </com:Repeater>


      I bind data in PageLoad.



      protected void Page_Load(object sender, EventArgs e)
      {
      if (!IsPostBack)
      {
      list_repeater.DataSource = ... getData ..;
      list_repeater.DataBind();
      ...
      }
      }


      Of course, i bind RepeaterItemEventHandler on the list.



      protected override void OnInit(EventArgs e)
      {
      base.OnInit(e);
      base.LoadingControlAdd();
      ...
      list_repeater.ItemDataBound += new RepeaterItemEventHandler(list_repeater_ItemDataBound);
      ...
      }


      and then i bind EventHandler to each btnCancel.



      protected void list_repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
      {
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
      {
      LinkButton btnCancel = e.Item.FindControl("btnCancel") as LinkButton;
      btnCancel.Click += new EventHandler(btnCancel_Click);
      }
      }


      But, it does not work.
      When i click the btnCancel of each item, btnCancel_Click is not triggered. I think i does well, and can't find any flaw. Is there any fault on the code above please tell me what's wrong. Thanks.
      (id on CommnadArgument is set properly, don't mind it)










      share|improve this question














      I need set a button for each item of repeater in C#(asp.net).



      <com:Repeater runat="server" ID="list_repeater">
      <ItemTemplate>
      <tr>
      <td>
      <asp:LinkButton runat="server" ID="btnCancel" CommandArgument='<%# Eval("id") %>'>Cancel</asp:LinkButton>
      </td>
      ...
      </tr>
      </ItemTemplate>
      </com:Repeater>


      I bind data in PageLoad.



      protected void Page_Load(object sender, EventArgs e)
      {
      if (!IsPostBack)
      {
      list_repeater.DataSource = ... getData ..;
      list_repeater.DataBind();
      ...
      }
      }


      Of course, i bind RepeaterItemEventHandler on the list.



      protected override void OnInit(EventArgs e)
      {
      base.OnInit(e);
      base.LoadingControlAdd();
      ...
      list_repeater.ItemDataBound += new RepeaterItemEventHandler(list_repeater_ItemDataBound);
      ...
      }


      and then i bind EventHandler to each btnCancel.



      protected void list_repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
      {
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
      {
      LinkButton btnCancel = e.Item.FindControl("btnCancel") as LinkButton;
      btnCancel.Click += new EventHandler(btnCancel_Click);
      }
      }


      But, it does not work.
      When i click the btnCancel of each item, btnCancel_Click is not triggered. I think i does well, and can't find any flaw. Is there any fault on the code above please tell me what's wrong. Thanks.
      (id on CommnadArgument is set properly, don't mind it)







      c# asp.net eventhandler






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 7:30









      Aaron RyuAaron Ryu

      73




      73
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I Solved.



          Refer: https://forums.asp.net/t/1680429.aspx?bind+event+handler+for+dynamic+controls+in+repeater




          As far as I know, "itemDataBound" event is used when you call DataBind(). However, a databind process can usually be done in the body of if (!IsPostBack){……},So you can never call DataBind() any more, I guess the reason is this.



          To solve the problem, You can try to put the codes like this:




          protected void Page_Load(object sender, EventArgs e)
          {
          if (!IsPostBack)
          {
          list_repeater.DataSource = ... getData ..;
          list_repeater.DataBind();
          ...
          }
          ...
          foreach(RepeaterItem item in list_repeater.Items) {
          if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
          {
          LinkButton btnCancel = item.FindControl("btnCancel") as LinkButton;
          btnCancel.Click += new EventHandler(btnCancel_Click);
          }
          }


          It's caused by my poor knowledge for asp.net. Sorry for self-answering in minutes after post the question.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993779%2fevent-binding-to-each-linkbutton-of-repeater-not-working%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I Solved.



            Refer: https://forums.asp.net/t/1680429.aspx?bind+event+handler+for+dynamic+controls+in+repeater




            As far as I know, "itemDataBound" event is used when you call DataBind(). However, a databind process can usually be done in the body of if (!IsPostBack){……},So you can never call DataBind() any more, I guess the reason is this.



            To solve the problem, You can try to put the codes like this:




            protected void Page_Load(object sender, EventArgs e)
            {
            if (!IsPostBack)
            {
            list_repeater.DataSource = ... getData ..;
            list_repeater.DataBind();
            ...
            }
            ...
            foreach(RepeaterItem item in list_repeater.Items) {
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
            LinkButton btnCancel = item.FindControl("btnCancel") as LinkButton;
            btnCancel.Click += new EventHandler(btnCancel_Click);
            }
            }


            It's caused by my poor knowledge for asp.net. Sorry for self-answering in minutes after post the question.






            share|improve this answer




























              0














              I Solved.



              Refer: https://forums.asp.net/t/1680429.aspx?bind+event+handler+for+dynamic+controls+in+repeater




              As far as I know, "itemDataBound" event is used when you call DataBind(). However, a databind process can usually be done in the body of if (!IsPostBack){……},So you can never call DataBind() any more, I guess the reason is this.



              To solve the problem, You can try to put the codes like this:




              protected void Page_Load(object sender, EventArgs e)
              {
              if (!IsPostBack)
              {
              list_repeater.DataSource = ... getData ..;
              list_repeater.DataBind();
              ...
              }
              ...
              foreach(RepeaterItem item in list_repeater.Items) {
              if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
              {
              LinkButton btnCancel = item.FindControl("btnCancel") as LinkButton;
              btnCancel.Click += new EventHandler(btnCancel_Click);
              }
              }


              It's caused by my poor knowledge for asp.net. Sorry for self-answering in minutes after post the question.






              share|improve this answer


























                0












                0








                0







                I Solved.



                Refer: https://forums.asp.net/t/1680429.aspx?bind+event+handler+for+dynamic+controls+in+repeater




                As far as I know, "itemDataBound" event is used when you call DataBind(). However, a databind process can usually be done in the body of if (!IsPostBack){……},So you can never call DataBind() any more, I guess the reason is this.



                To solve the problem, You can try to put the codes like this:




                protected void Page_Load(object sender, EventArgs e)
                {
                if (!IsPostBack)
                {
                list_repeater.DataSource = ... getData ..;
                list_repeater.DataBind();
                ...
                }
                ...
                foreach(RepeaterItem item in list_repeater.Items) {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                LinkButton btnCancel = item.FindControl("btnCancel") as LinkButton;
                btnCancel.Click += new EventHandler(btnCancel_Click);
                }
                }


                It's caused by my poor knowledge for asp.net. Sorry for self-answering in minutes after post the question.






                share|improve this answer













                I Solved.



                Refer: https://forums.asp.net/t/1680429.aspx?bind+event+handler+for+dynamic+controls+in+repeater




                As far as I know, "itemDataBound" event is used when you call DataBind(). However, a databind process can usually be done in the body of if (!IsPostBack){……},So you can never call DataBind() any more, I guess the reason is this.



                To solve the problem, You can try to put the codes like this:




                protected void Page_Load(object sender, EventArgs e)
                {
                if (!IsPostBack)
                {
                list_repeater.DataSource = ... getData ..;
                list_repeater.DataBind();
                ...
                }
                ...
                foreach(RepeaterItem item in list_repeater.Items) {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                LinkButton btnCancel = item.FindControl("btnCancel") as LinkButton;
                btnCancel.Click += new EventHandler(btnCancel_Click);
                }
                }


                It's caused by my poor knowledge for asp.net. Sorry for self-answering in minutes after post the question.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 1 at 7:49









                Aaron RyuAaron Ryu

                73




                73
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993779%2fevent-binding-to-each-linkbutton-of-repeater-not-working%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                    How to fix TextFormField cause rebuild widget in Flutter