Adding Link Column to ASP.NET GridView

前端 未结 5 2114
一向
一向 2020-12-30 01:00

I want to output a list of news headlines that are clickable. So far I can get it to print out a list of headlines because I dragged and dropped the NewsHeadline table in d

5条回答
  •  暖寄归人
    2020-12-30 01:30

    Something like this will work fantastic as a solution in Visual Studio 2010.

    1. Create a GridView in the Designer tab of your webpage in VS.
    2. Hover your mouse over the GridView and click the arrow that appears in the top right.
    3. Go to "Choose Data Source" and select "new data source..."
    4. Create the connection string to your database and choose the NewsHeadline table.
    5. Write the query SELECT News_Id, NewsHeadline FROM NewsHeadline
    6. Finish the setup. Now some code should be generated in the Source tab. This will also create an SqlDataSource which is now the DataSource of your GridView.
    7. Go to where the code is for your GridView in the Source tab and replace with the following code.

    Code:

    
        
        
        
    
    

    And you're all set. This will create a list of all the Headlines as hyperlinks with a dynamically generated unique link to the newsdetails.aspx page compliments of the query string we built using the PRIMARY KEY News_Id corresponding to each NewsHeadline entry in the NewsHeadline table.

    Then when you load the newsdetails.aspx page you use: Request.QueryString["News_Id"] to get the News_Id value from the URL and use it to query the database for the details about the specific NewsHeadline that was clicked. You can then display the result of that query on the webpage.

提交回复
热议问题