Results 1 to 13 of 13

Thread: [RESOLVED] Using hyperlinks and buttons load table from MSSQL

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    407

    Resolved [RESOLVED] Using hyperlinks and buttons load table from MSSQL

    Hi Guys,
    Got a problem that I spent all last night working on and still couldn't get it. I create this table dynamically. Then I have a text box and a button. You enter the name of a table in the text box and then click the button and that will call the MSSQL server and load the corresponding table. That works fine no problem.

    Then I decided to create another table that lists the tables that are on the SQL server and create a hyperlink on each name of the table. So now all the user has to do is click the name of the table save the name of the table they clicked in the querystring and it will call the same procedure as the button.

    The problem is that for some reason I'm getting a Findcontrol error that's telling me that two id's have the same id's when I click the hyperlink and then try to load through the button. It's duplicating the controls.

    Is there anyone who's had this type of problem before and how did you get around it. Maybe I'm just not understanding what's happening in the background. I've tried clearing the viewstate, and session states and that still doesn't help me. Has something to do with how hyperllinks and buttons work.

    thanks.
    My Websites
    SharpMP3 - MP3 Design Articles www.sharpmp3.com
    Yobbers - Job Search www.yobbers.com
    Lets Trend - Methods For Riding Stock Trends www.letstrend.com

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    407

    Re: Using hyperlinks and buttons load table from MSSQL

    a linkbutton is probably what I should be using instead of a hyperlink...
    My Websites
    SharpMP3 - MP3 Design Articles www.sharpmp3.com
    Yobbers - Job Search www.yobbers.com
    Lets Trend - Methods For Riding Stock Trends www.letstrend.com

  3. #3
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Using hyperlinks and buttons load table from MSSQL

    Had this problem when i tried to manipulate controls inside a formview edit-delete-standard.
    Try to give a different .id on every control you create.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    407

    Re: Using hyperlinks and buttons load table from MSSQL

    Actually what I ended up doing was this
    Code:
                blahtest = new LinkButton();
                blahtest.ID = "blah"+j;
                blahtest.CommandName = port_names[i];
                blahtest.CommandArgument = "123346";
                blahtest.Text = port_names[i];
                blahtest.Click += new EventHandler(blahClick);
    
    
               // blahtest.PostBackUrl = "portfolio.aspx?port=" + port_names[i];
                acell.Controls.Add(blahtest);
    
    Then I added this procedure to deal with the dynamic click
        private void blahClick(object sender, EventArgs e)
        {
            LinkButton bttn = sender as LinkButton;
            string buttonClicked = bttn.CommandName;
            portfolioLoader(buttonClicked);
        }
    So basically I created a linkbutton dynamically then added it to a cell. Then that got added to a row and then a table on the page, but you could just add it to the placeholder controls. This makes it so when the user clicks on the linkbutton control it fires the click event for the linkbutton which then creates a new linkbutton. This makes it so I can pull the commandtext which holds the name of the table I want to load from the SQL server. Then I pass the name into my loadtable procedure and call a sql command to get the table and load the table. Seems to work pretty good.
    My Websites
    SharpMP3 - MP3 Design Articles www.sharpmp3.com
    Yobbers - Job Search www.yobbers.com
    Lets Trend - Methods For Riding Stock Trends www.letstrend.com

  5. #5
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Using hyperlinks and buttons load table from MSSQL

    So, hold on...

    You are dynamically creating one LinkButton, which then dynamically creates another one....

    Is that right?

    Off the top of my head, that doesn't sound like the best approach.

    Gary

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    407

    Re: [RESOLVED] Using hyperlinks and buttons load table from MSSQL

    Oh I see what your saying. Your right it's not really that efficient. I guess you could find control it. But you need to pull the bttn.CommandName out of it some how. I haven't played around with the sender object that much to see how else I could get it.

    If you have a better idea I'd love to hear it. This is just what I tried first seems to work.
    My Websites
    SharpMP3 - MP3 Design Articles www.sharpmp3.com
    Yobbers - Job Search www.yobbers.com
    Lets Trend - Methods For Riding Stock Trends www.letstrend.com

  7. #7
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Using hyperlinks and buttons load table from MSSQL

    Hello,

    The sender object is the instance of the object that raised the event, and this can be cast to the type, in this case LinkButton. From there, you can grab the CommandName property in the same way as you normally would.

    Gary

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    407

    Re: [RESOLVED] Using hyperlinks and buttons load table from MSSQL

    Thats what I thought this did
    Code:
       private void blahClick(object sender, EventArgs e)
        {
            LinkButton bttn = sender as LinkButton;
            string buttonClicked = bttn.CommandName;
            portfolioLoader(buttonClicked);
        }
    how would you write your code?
    My Websites
    SharpMP3 - MP3 Design Articles www.sharpmp3.com
    Yobbers - Job Search www.yobbers.com
    Lets Trend - Methods For Riding Stock Trends www.letstrend.com

  9. #9
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Using hyperlinks and buttons load table from MSSQL

    Yes, that is what that code does.

    It was the creation of another LinkButton that you mentioned that confused me.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    407

    Re: [RESOLVED] Using hyperlinks and buttons load table from MSSQL

    Wait a minute. Before you said I was dynamically creating a linkbutton which then created another one when I posted this code:
    Code:
                blahtest = new LinkButton();
                blahtest.ID = "blah"+j;
                blahtest.CommandName = port_names[i];
                blahtest.CommandArgument = "123346";
                blahtest.Text = port_names[i];
                blahtest.Click += new EventHandler(blahClick);
    
    
               // blahtest.PostBackUrl = "portfolio.aspx?port=" + port_names[i];
                acell.Controls.Add(blahtest);
    
    Then I added this procedure to deal with the dynamic click
        private void blahClick(object sender, EventArgs e)
        {
            LinkButton bttn = sender as LinkButton;
            string buttonClicked = bttn.CommandName;
            portfolioLoader(buttonClicked);
        }
    You said that was inefficient because I had to create two linkbuttons.

    Now your saying thats the way to do it?

    I agreed with you that it was inefficient before when you thought I had two dynamically created link buttons because I had a dynamically created link button "blahtest" and then I had to recreate it again from the sender with bttn. I am type casting here, but you still need to use two link buttons if you want to do it this way. Is there a way to do it with just one? without making a linkbutton global?
    My Websites
    SharpMP3 - MP3 Design Articles www.sharpmp3.com
    Yobbers - Job Search www.yobbers.com
    Lets Trend - Methods For Riding Stock Trends www.letstrend.com

  11. #11
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Using hyperlinks and buttons load table from MSSQL

    No, no, no.

    You said that you were creating another LinkButton, not me Or at least, that is what I read in your post #4. If that is not what you are doing (I had assumed that you were doing other code elsewhere):

    This makes it so when the user clicks on the linkbutton control it fires the click event for the linkbutton which then creates a new linkbutton.
    Ok, I think this was just a misunderstanding based on wording

    When you used the word "new", I thought you meant you were creating another LinkButton elsewhere in your code. Now I see that you were referring to the casting. This doesn't create a new instance of the button, but rather it takes the reference that you already have to the LinkButton, i.e. sender, and casting it to the correct type. This technique is perfectly valid.

    Gary

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    407

    Re: [RESOLVED] Using hyperlinks and buttons load table from MSSQL

    Oh ok good. First time with link buttons. Thanks for verifying I'm not writing sloppy code.
    My Websites
    SharpMP3 - MP3 Design Articles www.sharpmp3.com
    Yobbers - Job Search www.yobbers.com
    Lets Trend - Methods For Riding Stock Trends www.letstrend.com

  13. #13
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Using hyperlinks and buttons load table from MSSQL

    Not a problem at all, happy to try to help

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width