Dynamically add a link that calls an On_Click Method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • denny1824
    New Member
    • Dec 2007
    • 32

    Dynamically add a link that calls an On_Click Method

    I have a problem that I think should be simple.

    In codebehind file in C# I have a loop that gets a string on each pass. I need to display that string on the page. I need it so that when someone clicks the text on the page that it will then call my On_Click method in the C# codebehind file. I will also need a way to determine the text that was clicked.

    I have something like:
    Code:
    protected void Page_Load(object sender, EventArgs e)
    {
        foreach (string s in stringArray)
            {
                HtmlAnchor a = new HtmlAnchor();
                a.Attributes.Add("onclick", "On_Click()");
                a.Attributes.Add("runat", "server");
                a.InnerText = s;
                ExistingTag.Controls.Add(a);
            }
    }
    It gives me an error on page when text is clicked.

    I can't use javascript. It has to be codebehind.

    Thank You for any help that you can give me.
    Denny
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Why do you make it an html anchor? Why not a button? or an aspnet controlled link?

    Comment

    • denny1824
      New Member
      • Dec 2007
      • 32

      #3
      It could be any control that can display a string as text and raises some kind of event when clicked.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        So add an instance of an aspnet control, I am pretty sure you can dynamically add the postback stuff.
        Have them all "postback" to the same event, the event should be able to identify what object triggered the postback, and you can use that information to get the text that was clicked.

        Comment

        • Dreea
          New Member
          • Aug 2007
          • 37

          #5
          I think you should be able to do something like that
          Code:
          protected void Page_Load(object sender, EventArgs e)
          {
              foreach (string s in stringArray)
              {
                      LinkButton something=new LinkButton();
                      something.Click+=new System.EventHandler(LinkButton_Cancel_Click);
          
             	    // set attribute for identifying the button was pressed for
          	   something.Add("someId",s);
          	   something.Text=s;	
                     ExistingTag.Controls.Add(something);
              }
          }
          
          	private void LinkButton_Cancel_Click(object sender, System.EventArgs e)
          	{		
          		// get the text you clicked on 
          		string text=((LinkButton)sender).Attributes["someId"];			                      			
          	}
          Hope it helps

          Comment

          • denny1824
            New Member
            • Dec 2007
            • 32

            #6
            Originally posted by Dreea
            I think you should be able to do something like that
            Code:
            protected void Page_Load(object sender, EventArgs e)
            {
                foreach (string s in stringArray)
                {
                        LinkButton something=new LinkButton();
                        something.Click+=new System.EventHandler(LinkButton_Cancel_Click);
            
               	    // set attribute for identifying the button was pressed for
            	   something.Add("someId",s);
            	   something.Text=s;	
                       ExistingTag.Controls.Add(something);
                }
            }
            
            	private void LinkButton_Cancel_Click(object sender, System.EventArgs e)
            	{		
            		// get the text you clicked on 
            		string text=((LinkButton)sender).Attributes["someId"];			                      			
            	}
            Hope it helps
            Dreea,

            This does help. Thank you.

            Do you know how to make it so that the LinkButton will still postback when javascript is disabled? C#.NET is adding the __doPostBack, but that only works when javascript is enabled.

            Thanks again,
            Denny

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              If javascript is disabled, NOTHING ASP.NET related will function correctly.
              Unless you use all static pages.

              Comment

              Working...