2013-09-25 1 views
0

동일한 페이지에 다중 데이터 목록이 있습니다. 사용자가 레코드를 클릭하면 정보를 볼 수있는보기 페이지로 리디렉션됩니다. . ID를 보내려면 쿼리 문자열을 사용하십시오. 페이지를 보려면 클릭 한 레코드 ... 문제는 ID EX : 17로 레코드를 선택할 때입니다. 때로는 정보가 정확하지만 항상 다른 ID가있는 다른 테이블에 다른 레코드가 있기 때문에 항상 그렇기 때문에 : 17 그것은 클릭 한 것 대신에 그것을 대체합니다!어떻게이 문제를 저장 프로 시저 및 queryString으로 해결할 수 있습니까?

임 사용하여 C#을/Asp.net 4.0 프레임 워크/SQL 서버 여기에 2008

내 코드

StartPage.aspx합니다 (DataLists 페이지)입니다 :

public partial class MainMasterStartPage : System.Web.UI.Page 
{ 
    protected SqlConnection _connection; 
    protected SqlCommand _command; 
    protected SqlDataAdapter _adp; 
    protected System.Data.DataTable _tbl; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!(this.IsPostBack)) 
     { 
      // For News DataList 
      prepareConnection(); 
      _command.CommandText = "select top 5 * from News ORDER BY id DESC"; 
      _adp = new SqlDataAdapter(); 
      _tbl = new System.Data.DataTable(); 
      _adp.SelectCommand = _command; 
      _adp.Fill(_tbl); 

      dlNews.DataSource = _tbl; 
      dlNews.DataBind(); 


      // For Sports DataList 
      prepareConnection(); 
      _command.CommandText = "select top 5 * from Sports ORDER BY id DESC"; 
      _adp = new SqlDataAdapter(); 
      _tbl = new System.Data.DataTable(); 
      _adp.SelectCommand = _command; 
      _adp.Fill(_tbl); 

      dlSports.DataSource = _tbl; 
      dlSports.DataBind(); 



      // For Technology DataList 
      prepareConnection(); 
      _command.CommandText = "select top 5 * from Technology ORDER BY id DESC"; 
      _adp = new SqlDataAdapter(); 
      _tbl = new System.Data.DataTable(); 
      _adp.SelectCommand = _command; 
      _adp.Fill(_tbl); 

      dlTechnology.DataSource = _tbl; 
      dlTechnology.DataBind(); 



      // For Articles DataList 
      prepareConnection(); 
      _command.CommandText = "select top 5 * from Articles ORDER BY id DESC"; 
      _adp = new SqlDataAdapter(); 
      _tbl = new System.Data.DataTable(); 
      _adp.SelectCommand = _command; 
      _adp.Fill(_tbl); 

      dlArticles.DataSource = _tbl; 
      dlArticles.DataBind(); 



      // For Islamics DataList 
      prepareConnection(); 
      _command.CommandText = "select top 5 * from Islamics ORDER BY id DESC"; 
      _adp = new SqlDataAdapter(); 
      _tbl = new System.Data.DataTable(); 
      _adp.SelectCommand = _command; 
      _adp.Fill(_tbl); 

      dlIslamics.DataSource = _tbl; 
      dlIslamics.DataBind(); 



     } 

    } 


    protected void prepareConnection() 
    { 
     _connection = new SqlConnection(@"Data Source=SONIC-PC\SQLEXPRESS;Initial Catalog=BrainStorms;User ID=sa;Password=gg123"); 
     _connection.Open(); 
     _command = new SqlCommand(); 
     _command.Connection = _connection; 

    } 

및 표시하는 Viewpage은 이미 클릭 된 레코드 :

public partial class View : System.Web.UI.Page 
{ 

    protected SqlCommand News_command; 
    protected SqlCommand Sports_command; 
    protected SqlCommand Technology_command; 
    protected SqlCommand Articles_command; 
    protected SqlCommand Islamics_command; 

    protected SqlDataAdapter News_adp; 
    protected SqlDataAdapter Sports_adp; 
    protected SqlDataAdapter Technology_adp; 
    protected SqlDataAdapter Articles_adp; 
    protected SqlDataAdapter Islamics_adp; 

    protected System.Data.DataTable News_tbl; 
    protected System.Data.DataTable Sports_tbl; 
    protected System.Data.DataTable Technology_tbl; 
    protected System.Data.DataTable Articles_tbl; 
    protected System.Data.DataTable Islamics_tbl; 

    protected SqlConnection _connection; 
    protected string _ID; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if ((Request.QueryString["ID"] != null)) 
     { 
      _ID = Request.QueryString["ID"].ToString(); 
     } 

     //for The News dataList 
     prepareConnection(); 

     News_command.CommandText = "select * from News where [email protected]"; 
     News_command.Parameters.AddWithValue("ID", _ID); 
     News_adp = new SqlDataAdapter(); 
     News_tbl = new System.Data.DataTable(); 
     News_adp.SelectCommand = News_command; 
     News_adp.Fill(News_tbl); 


     if (News_tbl.Rows.Count > 0) 
     { 
      lblID.Text = News_tbl.Rows[0]["ID"].ToString(); 
      lblTitle.Text = News_tbl.Rows[0]["Title"].ToString(); 
      lblContent.Text = News_tbl.Rows[0]["Contect"].ToString(); 

     } 


     //For The Sports DataList 
     prepareConnection(); 

     Sports_command.CommandText = "select * from Sports where [email protected]"; 
     Sports_command.Parameters.AddWithValue("ID", _ID); 
     Sports_adp = new SqlDataAdapter(); 
     Sports_tbl = new System.Data.DataTable(); 
     Sports_adp.SelectCommand = Sports_command; 
     Sports_adp.Fill(Sports_tbl); 


     if (Sports_tbl.Rows.Count > 0) 
     { 
      lblID.Text = Sports_tbl.Rows[0]["ID"].ToString(); 
      lblTitle.Text = Sports_tbl.Rows[0]["Title"].ToString(); 
      lblContent.Text = Sports_tbl.Rows[0]["Contect"].ToString(); 
     } 



     //for The Technology DataList 
     prepareConnection(); 

     Technology_command.CommandText = "select * from Technology where [email protected]"; 
     Technology_command.Parameters.AddWithValue("ID", _ID); 
     Technology_adp = new SqlDataAdapter(); 
     Technology_tbl = new System.Data.DataTable(); 
     Technology_adp.SelectCommand = Technology_command; 
     Technology_adp.Fill(Technology_tbl); 


     if (Technology_tbl.Rows.Count > 0) 
     { 
      lblID.Text = Technology_tbl.Rows[0]["ID"].ToString(); 
      lblTitle.Text = Technology_tbl.Rows[0]["Title"].ToString(); 
      lblContent.Text = Technology_tbl.Rows[0]["Contect"].ToString(); 
     } 


     //For The Articles DataList 
     prepareConnection(); 

     Articles_command.CommandText = "select * from Articles where [email protected]"; 
     Articles_command.Parameters.AddWithValue("ID", _ID); 
     Articles_adp = new SqlDataAdapter(); 
     Articles_tbl = new System.Data.DataTable(); 
     Articles_adp.SelectCommand = Articles_command; 
     Articles_adp.Fill(Articles_tbl); 


     if (Articles_tbl.Rows.Count > 0) 
     { 
      lblID.Text = Articles_tbl.Rows[0]["ID"].ToString(); 
      lblTitle.Text = Articles_tbl.Rows[0]["Title"].ToString(); 
      lblContent.Text = Articles_tbl.Rows[0]["Contect"].ToString(); 
     } 


     //For The Islamics DataList 
     prepareConnection(); 

     Islamics_command.CommandText = "select * from Islamics where [email protected]"; 
     Islamics_command.Parameters.AddWithValue("ID", _ID); 
     Islamics_adp = new SqlDataAdapter(); 
     Islamics_tbl = new System.Data.DataTable(); 
     Islamics_adp.SelectCommand = Islamics_command; 
     Islamics_adp.Fill(Islamics_tbl); 


     if (Islamics_tbl.Rows.Count > 0) 
     { 
      lblID.Text = Islamics_tbl.Rows[0]["ID"].ToString(); 
      lblTitle.Text = Islamics_tbl.Rows[0]["Title"].ToString(); 
      lblContent.Text = Islamics_tbl.Rows[0]["Contect"].ToString(); 
     } 



    } 

    protected void prepareConnection() 
    { 
     _connection = new SqlConnection(@"Data Source=SONIC-PC\SQLEXPRESS;Initial Catalog=BrainStorms;User ID=sa;Password=gg123"); 
     _connection.Open(); 
     News_command = new SqlCommand(); 
     News_command.Connection = _connection; 
     Sports_command = new SqlCommand(); 
     Sports_command.Connection = _connection; 
     Technology_command = new SqlCommand(); 
     Technology_command.Connection = _connection; 
     Articles_command = new SqlCommand(); 
     Articles_command.Connection = _connection; 
     Islamics_command = new SqlCommand(); 
     Islamics_command.Connection = _connection; 

    } 

} 

및 여기에 The StartPag e.aspx 출처 : 여기에

<%@ Page Title="" Language="C#" MasterPageFile="MainMaster.master" AutoEventWireup="true" 
     CodeFile="StartPage.aspx.cs" Inherits="MainMasterStartPage" %> 

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 
     <div class="MainLatest"> 
      <div id="mainNews" style="clear: both; text-decoration: none; color: Black; text-align: right; 
       direction: rtl; width: auto; height: auto; margin-right: 0px; float: right; margin-top: 5px; 
       border-bottom: 1px solid #7F2423;"> 
       <div id="news" style="border: 1px solid black; margin-bottom: 15px;background-color: #EAE9E4;"> 
        <h3 style="background-color: #35496A; color: white; text-align: center; width: 650px; 
         float: right; border-bottom: 4px solid #7F2423; margin-bottom: 8px; padding-right: 5px;"> 
         آخر الأخبار</h3> 
        <asp:DataList ID="dlNews" runat="server"> 
         <ItemTemplate> 
          <a href='./NewsView.aspx?ID=<%#Eval("ID") %>' style="text-decoration: none;"> 
           <div id="123"> 
            <div id="image" style="clear: both; float: right; margin: 0 5px 10px 10px;"> 
             <asp:Image ID="Image1" runat="server" ImageUrl="~/images/epica.jpg" Style="height: 70px; 
              width: 130px; border: 1px solid black;" /> 
            </div> 
            <div id="title" style="position: relative; float: right; top: -14px; right: 0px; 
             height: 69px;"> 
             <asp:Label ID="lblTitle" runat="server" Text='<%#Eval("Title") %>' Style="font-size: 15px; 
              font-weight: bold; line-height: 110px; "></asp:Label> 
            </div> 
           </div> 
          </a> 
         </ItemTemplate> 
        </asp:DataList> 
       </div> 
       <div id="sports" style="border: 1px solid black; margin-bottom: 15px;background-color: #EAE9E4;"> 
        <h3 style="background-color: #03C0F8; text-align: center; color: white; width: 650px; 
         float: right; border-bottom: 4px solid #7F2423; margin-bottom: 8px; padding-right: 5px;"> 
         الرياضة</h3> 
        <asp:DataList ID="dlSports" runat="server"> 
         <ItemTemplate> 
          <a href='./SportsView.aspx?ID=<%#Eval("ID") %>' style="text-decoration: none;"> 
           <div id="123"> 
            <div id="image" style="clear: both; float: right; margin: 0 5px 10px 10px;"> 
             <asp:Image ID="Image1" runat="server" ImageUrl="~/images/epica.jpg" Style="height: 70px; 
              width: 130px; border: 1px solid black;" /> 
            </div> 
            <div id="title" style="position: relative; float: right; top: -18px; right: -2px; 
             height: 76px;"> 
             <asp:Label ID="lblTitle" runat="server" Text='<%#Eval("Title") %>' Style="font-size: 15px; 
              font-weight: bold; line-height: 110px;"></asp:Label> 
            </div> 
           </div> 
          </a> 
         </ItemTemplate> 
        </asp:DataList> 
       </div> 
       <div id="technology"style="border: 1px solid black; margin-bottom: 15px;background-color: #EAE9E4;"> 
        <h3 style="background-color: #FF9900; text-align: center; color: #7F2423; width: 650px; 
         float: right; border-bottom: 4px solid #7F2423; margin-bottom: 8px; padding-right: 5px;"> 
         أخبار التكنولوجيا</h3> 
        <asp:DataList ID="dlTechnology" runat="server"> 
         <ItemTemplate> 
          <a href='./TechnologyView.aspx?ID=<%#Eval("ID") %>' style="text-decoration: none;"> 
           <div id="123"> 
            <div id="image" style="clear: both; float: right; margin: 0 5px 10px 10px;"> 
             <asp:Image ID="Image1" runat="server" ImageUrl="~/images/epica.jpg" Style="height: 70px; 
              width: 130px; border: 1px solid black;" /> 
            </div> 
            <div id="title" style="position: relative; float: right; top: -18px; right: -2px; 
             height: 76px;"> 
             <asp:Label ID="lblTitle" runat="server" Text='<%#Eval("Title") %>' Style="font-size: 15px; 
              font-weight: bold; line-height: 110px;"></asp:Label> 
            </div> 
           </div> 
          </a> 
         </ItemTemplate> 
        </asp:DataList> 
       </div> 
       <div id="articles"style="border: 1px solid black; margin-bottom: 15px;background-color: #EAE9E4;"> 
        <h3 style="background-color: #7F2423; text-align: center; color: white; width: 650px; 
         float: right; border-bottom: 4px solid #7F2423; margin-bottom: 8px; padding-right: 5px;"> 
         مقالات</h3> 
        <asp:DataList ID="dlArticles" runat="server"> 
         <ItemTemplate> 
          <a href='./ArticlesView.aspx?ID=<%#Eval("ID") %>' style="text-decoration: none;"> 
           <div id="123"> 
            <div id="image" style="clear: both; float: right; margin: 0 5px 10px 10px;"> 
             <asp:Image ID="Image1" runat="server" ImageUrl="~/images/epica.jpg" Style="height: 70px; 
              width: 130px; border: 1px solid black;" /> 
            </div> 
            <div id="title" style="position: relative; float: right; top: -18px; right: -2px; 
             height: 76px;"> 
             <asp:Label ID="lblTitle" runat="server" Text='<%#Eval("Title") %>' Style="font-size: 15px; 
              font-weight: bold; line-height: 110px;"></asp:Label> 
            </div> 
           </div> 
          </a> 
         </ItemTemplate> 
        </asp:DataList> 
       </div> 
       <div id="islamics"style="border: 1px solid black; margin-bottom: 15px;background-color: #EAE9E4;"> 
        <h3 style="background-color: #FF9900; text-align: center; color: #7F2423; width: 650px; 
         float: right; border-bottom: 4px solid #7F2423; margin-bottom: 8px; padding-right: 5px;"> 
         إسلاميات</h3> 
        <asp:DataList ID="dlIslamics" runat="server"> 
         <ItemTemplate> 
          <a href='./IslamicsView.aspx?ID=<%#Eval("ID") %>' style="text-decoration: none;"> 
           <div id="123"> 
            <div id="image" style="clear: both; float: right; margin: 0 5px 10px 10px;"> 
             <asp:Image ID="Image1" runat="server" ImageUrl="~/images/epica.jpg" Style="height: 70px; 
              width: 130px; border: 1px solid black;" /> 
            </div> 
            <div id="title" style="position: relative; float: right; top: -18px; right: -2px; 
             height: 76px;"> 
             <asp:Label ID="lblTitle" runat="server" Text='<%#Eval("Title") %>' Style="font-size: 15px; 
              font-weight: bold; line-height: 110px;"></asp:Label> 
            </div> 
           </div> 
          </a> 
         </ItemTemplate> 
        </asp:DataList> 
       </div> 
      </div> 
     </div> 
    </asp:Content> 

과는 ViewPage.aspx 자료 :

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> 
    <p dir="rtl"> 
     <asp:Label ID="lblTitle" runat="server" Text="Label" ></asp:Label> 
    &quot;</p> 
<p dir="rtl"> 
    (<asp:Label ID="lblID" runat="server" Text="Label" ></asp:Label> 
    )</p> 
<p dir="rtl"> 
    :</p> 
<p dir="rtl"> 
    <asp:Label ID="lblContent" runat="server" Text="Label" ></asp:Label> 
</p> 
</asp:Content> 

답변

1

ID에 고유성이 없으면 항상 문제가 될 것입니다. 내가 제안 할 수있는 가장 좋은 것은 당신이 정말로 보여주고 싶은 테이블 행을 유일하게 정의하는 데 도움이 될 몇 가지 더 많은 인수를 전달한다는 것이다.

근본적으로, 쿼리 문자열에 불필요한 복잡성을 추가하는 등 해킹을 피하기 위해 ID 열이 고유하도록 데이터베이스를 변경해야하는지 여부를 묻습니다.

P. 오히려 이것을 주석으로 추가했을 것입니다.하지만 그렇게하기에는 너무 새로운 (명성이 너무 낮습니다) 것 같습니다.

0

당신 만 ID 포함하는 테이블에서 레코드를 끌어해야한다.

HyperLink의 검색어 문자열에 항목 (뉴스, 스포츠, 기술 등)을 제공하면이 작업을 수행 할 수 있습니다.

View 페이지에서이를 사용하여 쿼리 할 적절한 테이블을 선택하십시오. 모두를 쿼리하지 말고 의 오른쪽을 쿼리하십시오.

관련 문제