Jump to content

ASP.Net Adding LinkButtons with Label


bblbailey3

Recommended Posts

Ok here is the scenario. What I want: I click a button it displays text and some new links that run a code when I click it.Language...lol...no clue...I'm pretty sure I'm using VB.Currently the code reads Bodylbl1.text="<font face=arial><h1>WELCOME TO MY SITE</h1><BR>Currently MYSITE is down please go to <a href=somewhereelse.aspx>HERE</a>" It would be cool if I just wanted to send them "there"...but I need to run a tidbit of code first.Specifically something likeSub TidbitOfCode()Context.Items.add("Textbox","Text")Server.Transfer("somewhereelse.aspx", True)End SubI've tried just adding asp:linkbuttons into the label...but it doesn't display them...duh right...I've tried doing a variety of things...it seems like there should be an easy way to do what I want...but I haven't figured it out just yet.

Link to comment
Share on other sites

<asp:linkbutton> should work.....aspx

<asp:LinkButton ID="lbredirect" Runat="server">Redirect</asp:LinkButton>

.aspx.vb

Private Sub lbredirect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbredirect.ClickContext.Items.add("Textbox","Text")Server.Transfer("somewhereelse.aspx", True)End Sub

http://www.w3schools.com/aspnet/control_linkbutton.asp

Link to comment
Share on other sites

Actually...I don't think I explained well enough...I want to click a link/button whatever thingy and it runs code likeLabel.text = "blah blah blah <asp:linkbutton onclick=runcode id=something/> blah blah blah"I.e the label has both text and a link that when clicked can run the sub runcode(). I'm not sure if there is an easy way to do that...End RESULT. I click a button or a link that edits the content of the label. It displays the new information and hopefully part of that new information are a few new links that when clicked can run existing subs.I mean I'm sure if I was writing in java or something like that I could set it all up...but preferably using vb...which is similar.I know there are ways to do it that create the illusion of doing what I want...but I'm not particularly fond of those particular ideas. I would prefer a "dynamic" solution (at least i think dynamic is the word).

Link to comment
Share on other sites

You can use 2 linkbuttons.One is visible and one is invisiblein C# is looks something like this:<script runat="server"> public void disappear(object s, EventArgs e) { LinkButton1.Visible = false; LinkButton2.Visible = true; } public void runcode(object s, EventArgs e) { LinkButton2.Visible = false; Response.Write("bahhh"); }</script> ...<asp:LinkButton ID="LinkButton1" runat="server" OnClick="disappear">LinkButton</asp:LinkButton><asp:LinkButton ID="LinkButton2" runat="server" OnClick="runcode" Visible="false">LinkButton2</asp:LinkButton>... The output of this will be:A. LinkButton 1 showsClick on LinkButton 1B. LinkButton 1 disappears and LinkButton 2 appearsClick on LinkButton 2C. LinkButton 2 disappears and calls runcode()D. "bahhhh" is returned

Link to comment
Share on other sites

Like homiee said, here's it in VB<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton><asp:LinkButton ID="LinkButton2" runat="server" Visible="false">LinkButton2</asp:LinkButton>.aspx.vb

Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click		LinkButton2.Visible = True ' Show the 2nd link when linkbutton1 is clickedEnd Sub

Link to comment
Share on other sites

Something like this? (C#):

// This is the Click handler for the first LinkButtonprotected void Button1_Click(object sender, EventArgs e){	// Create the LinkButton and set its properties	LinkButton button2 = new LinkButton();	button2.ID = "Button2";	button2.Text = "I am dynamic!";	button2.Click += new EventHandler(Button2_Click);	// Add the LinkButton to the control with an ID of "Label"	Label.Controls.Add(button2);}// This is the Click handler for the second LinkButton (that we added)protected void Button2_Click(object sender, EventArgs e){	Response.Write("You clicked the second, dynamically added, LinkButton!");}

Link to comment
Share on other sites

I really appreciate all the help everyone is trying to give. I'm rather frustrated and this is going to sound sarcastic...I need to explain even more I suppose.On page one I have a sort of index. That index has links. Each link targets the same label. SO a person can view the content desired by clicking each link (which then posts the data to the label). Easy enough.My problem is that I have a second page similar to the first but with a different index. To load that page from the first I use a link kinda similar to an asp:linkbutton. To load the proper data on load...I need to use "variables". Hence when I click the link it needs to run code...to make sure that the variable is set to the correct value.Because the data on my page is often displayed within or by changing the .text property of an asp:label and because part of that data might include a link to my second page...which of course requires code to be run to set the variable to the correct value...I might ask if there is a way to create an asp:linkbutton and text with in an asp:label (really long sentence). I don't want to hide links with the .visible command/property/thingy. What I was hoping is possible is simply this. Clicking on a link POSTS both the viewable data required, AND a link that can trigger a sub. So I click link 1 and it posts "Blah blah blah <asp:linkbutton onclick=sub id=something/>" to label.text (Notice the location of the asp button...it's between the quotes and also notice how that link is part of the .text property of label...)I don't know if this is possible...I certainly am doing what I can to research it. If anyone happens to know if this is truly possible... I would appreciate the help. Again I don't want visible/invisible links that would require me to create a label for all text until I hit a link. Then an invisible link. then another label, then another link....and the problem would compound for every link I need to use to display information to that area. I would be turning 30 links invisible and 45 visible and would have 60 labels...etc. I also don't want a link with changing text. Anyway...I hope I don't offend anyone...because seriously I'm just a bit frustrated and I know you are all trying to help a new person start up...and I really do appreciate it.

Link to comment
Share on other sites

As far as I know, the answer to your question is no. It is not possible to do this to render a LinkButton to the page:

myLabel.Text = "blah blah blah <asp:LinkButton ID=\"SomeLinkButton\" runat=\"server\" />";

Your label will simply read "blah blah blah <asp:LinkButton ID="SomeLinkButton" runat="server" />" and the server tag won't ever be built.One trick I've discovered when I've come across situations like yours is to view the source code that is rendered and see what .NET is doing with my code. The reason I think this would help you is that you can sort of trick .NET into thinking that a PostBack has occurred by writing your own onclick event into the links.Try this. Put the following in a page and render it out to the browser:

<asp:Label id="myLabel" runat="server">blah blah blah <asp:LinkButton id="myLinkButton" OnClick="myLinkButton_Click" runat="sever" Text="Page 2" /> blah blah</asp:Label>

The code that is sent to the browser will look something like this:

<span id="myLabel">blah blah blah <a id="myLinkButton" onclick="__doPostBack('myFormName$myLinkButton');">Page 2</a> blah blah</span>

So, rather than have .NET write the rendered code, you can try to write it yourself. This way, you can dynamically set the Text for your Label like so:

myLabel.Text = "blah blah blah <a id=\"myLinkButton\" onclick=\"__doPostBack('myFormName$myLinkButton');\">Page 2</a> blah blah";

Of course, this is a total hack and, as such, you'll most likely have to play around with it a bit to get it to work correctly for you. I've used it a number of times to great advantage.

Link to comment
Share on other sites

I've been looking into the latest two ideas...neither has worked quite yet...but I'm abandoning the idea for now, and I now plan to use the whole .visible thing. The label would have worked better...but it was more difficult to "see" the formatting. I stumbled across panels in my quest...and they allow me to effectively achieve the results I want.While it isn't what was suggested above...it is close enough that I humbling take my hat off and apologize. Most likely had you known exactly what I was doing you would have suggested the whole panel thing.I can handle dealing with panel visibility rather than every link and label and every other control that will pop up along the way.Again I appreciate all the help everyone has been so active in trying to give...if someone finds an exact answer (I hear panels and placeholders can supposedly do what I want) I would appreciate it.thanks -iii

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...