【问题标题】:hyperlink to retrieve data from a database in asp.net从 asp.net 的数据库中检索数据的超链接
【发布时间】:2014-01-02 01:00:07
【问题描述】:

我正在开发工作门户,它显示存储在数据库(SQL Server)中的不同工作。我正在使用 ASP.NET 和 HTML。

我在数据库表中有job_titleunique job_id。我为从数据库中检索到的job_title 创建了超链接。现在我想通过单击该作业的job_title 在不同的页面上显示该特定作业的详细信息。

我需要 job_id 来处理我想在下一个超链接页面上显示其详细信息的特定工作。不使用job_title 而不是job_id 的原因是job_title 不是唯一的,但job_id 是唯一的。请告诉我如何做到这一点,或者您是否有其他解决方案?

这里有一些示例代码:

(jobs.aspx)

      <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:JobPortalConnectionString2 %>" SelectCommand="SELECT [job_title], [description] FROM [jobs]"></asp:SqlDataSource>
      <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
          <AlternatingItemTemplate>
              <strong>Job Title:</strong>
              <span style="">
              <asp:Label ID="job_titleLabel" runat="server" Text='<%# Eval("job_title") %>' />
              <br />
              <strong>Description:</strong>
              <asp:Label ID="descriptionLabel" runat="server" Text='<%# Eval("description") %>' />
              <br />
              <br />
              </span>
          </AlternatingItemTemplate>
          <EditItemTemplate>
              <span style="">job_title:
              <asp:TextBox ID="job_titleTextBox" runat="server" Text='<%# Bind("job_title") %>' />
              <br />
              description:
              <asp:TextBox ID="descriptionTextBox" runat="server" Text='<%# Bind("description") %>' />
              <br />
              <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
              <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
              <br /><br /></span>
          </EditItemTemplate>
          <EmptyDataTemplate>
              <span>No data was returned.</span>
          </EmptyDataTemplate>
          <InsertItemTemplate>
              <span style="">job_title:
              <asp:TextBox ID="job_titleTextBox" runat="server" Text='<%# Bind("job_title") %>' />
              <br />description:
              <asp:TextBox ID="descriptionTextBox" runat="server" Text='<%# Bind("description") %>' />
              <br />
              <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
              <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
              <br /><br /></span>
          </InsertItemTemplate>
          <ItemTemplate>
              <strong>Job Title:</strong>
              <span style="">
              <asp:Label ID="job_titleLabel" runat="server" Text='<%# Eval("job_title") %>'></asp:Label>
              <br />
              <strong>Description:</strong>
              <asp:Label ID="descriptionLabel" runat="server" Text='<%# Eval("description") %>'></asp:Label>
              <br />  
              <br/>
              </span>
          </ItemTemplate>
          <LayoutTemplate>
              <div id="itemPlaceholderContainer" runat="server" style="">
                  <span runat="server" id="itemPlaceholder" />
                  &nbsp;</span></div>
              <asp:Button ID="Button1" runat="server" Height="30px" OnClick="Button1_Click1" Text="Apply Now" Width="100px" />
              <div style="">
                  <asp:DataPager ID="DataPager1" runat="server">
                      <Fields>
                          <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
                          <asp:NumericPagerField />
                          <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
                      </Fields>
                  </asp:DataPager>
              </div>
          </LayoutTemplate>
          <SelectedItemTemplate>
              <span style="">job_title:
              <asp:Label ID="job_titleLabel" runat="server" Text='<%# Eval("job_title") %>' />
              <br />
              description:
              <asp:Label ID="descriptionLabel" runat="server" Text='<%# Eval("description") %>' />
              <br />


  </div>

【问题讨论】:

  • 最好显示一些示例代码。真的不确定您是否需要有关 SQL、ASP.Net 的帮助?
  • 我已经添加了.aspx页面的代码请查看....

标签: c# html asp.net


【解决方案1】:

看起来你想要这样的网址:

blabla-your-website.com/fisrt-job-title
blabla-your-website.com/second-job-title
blabla-your-website.com/other-job-title

但你说:

job_title 不是唯一的,但 job_id 是

所以这是不可能的。常见的解决方案是在您的 url 中同时使用 id 和标题:

blabla-your-website.com/1-fisrt-job-title
blabla-your-website.com/22-second-job-title
blabla-your-website.com/333-other-job-title

其中 1、22 和 333 是特定作业的唯一 ID。

如果您的标题是独一无二的,这很容易做到。看看RacoonBlog's SlugConverter。它用于将您的标题转换为这样的 url(来自 RacoonBlog 测试的代码):

        var result = SlugConverter.TitleToSlug("Document based modeling: Auctions & Bids");
        Assert.Equal("document-based-modeling-auctions-bids", result);

        result = SlugConverter.TitleToSlug("Hiring Questions–The phone book");
        Assert.Equal("hiring-questions-the-phone-book", result);

【讨论】:

    猜你喜欢
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 2013-06-04
    • 2020-03-09
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多