2017-01-13 16 views
0

グリッドビューの列の1つのボタンを使用して、新しいページに表示するグリッドビューから行をコピーしようとしています。私は自分のプロジェクトにリンクされているAccessデータベースから、私のgridviewを作成しています。私はいくつかの異なることを試みましたが、プロジェクトが実行されたときに行情報を表示するものはありません。私は、実際のデータビューからしようとしている現在のコードは次のようになります。VBを使用してasp.net gridviewから新しいページにコピー行

例1a

<asp:GridView ID="Grid1" runat="server" Width ="90%" AutoGenerateColumns="false" OnRowDeleting="Grid1_RowDeleting" DataKeyNames="Title"> 
    <Columns> 
     <asp:BoundField DataField="Title" HeaderText="Title" /> 
     <asp:BoundField DataField="Console" HeaderText="Console" /> 
     <asp:BoundField DataField="Year_Released" HeaderText="Year Released" /> 
     <asp:BoundField DataField="ESRB" HeaderText="ESRB Rating" /> 
     <asp:BoundField DataField="Score" HeaderText="Personal Score" /> 
     <asp:BoundField DataField="Publisher" HeaderText="Publisher" /> 
     <asp:BoundField DataField="Developer" HeaderText="Developer" /> 
     <asp:BoundField DataField="Genre" HeaderText="Genre" /> 
     <asp:BoundField DataField="Purchase" HeaderText="Purchase Date" /> 
     <asp:TemplateField ItemStyle-Width="7%" ShowHeader="False"> 
      <ItemTemplate> 
      <asp:LinkButton ID="lnkDetails" runat="server" Text="View" PostBackUrl='<%# "~/ViewDetails.aspx?RowIndex=" & Container.DataItemIndex %>'/> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

そして、私はコードが表示されていしようとしていますページの分離コードのコードは次のとおりです。

例1b

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 
     If Me.Page.PreviousPage IsNot Nothing Then 
      Dim rowIndex As Integer = Integer.Parse(Request.QueryString("RowIndex")) 
      Dim GridView1 As GridView = DirectCast(Me.Page.PreviousPage.FindControl("Grid1"), GridView) 
      Dim row As GridViewRow = GridView1.Rows(rowIndex) 
      lblTitle.Text = row.Cells(0).Text 
      lblConsole.Text = row.Cells(1).Text 
      lblYear.Text = row.Cells(2).Text 
      lblESRB.Text = row.Cells(3).Text 
      lblScore.Text = row.Cells(4).Text 
      lblPublisher.Text = row.Cells(5).Text 
      lblDeveloper.Text = row.Cells(6).Text 
      lblGenre.Text = row.Cells(7).Text 
      lblPurchase.Text = row.Cells(8).Text 

     End If 
    End Sub 

私はまた、GridViewコントロールのボタンだったコードの別のセットを試してみました

例2a分離コードコードが

<asp:Button ID="btnLink" runat="server" Text="View Details" PostBackUrl='<%# Eval("Title", "~/ViewDetails.aspx?Id={0}") %>'/> 

:私は試みる何Iの両方のバリエーション、主に第2を作る試みた2B

Protected Sub Page_Load(sender As Object, e As EventArgs) 
     If Not IsPostBack Then 
      Dim GameTitle As String = Request.QueryString("Id") 

      Dim connString As String = "PROVIDER=Microsoft.ACE.OLEDB.12.0;" + "DATA SOURCE=" + Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "App_Data" + "db1.accdb") 
      Using connection As New OleDbConnection(connString) 
       connection.Open() 
       Dim reader As OleDbDataReader = Nothing 
       Dim command As New OleDbCommand((Convert.ToString("SELECT * from [Video_Games] WHERE Title='") & GameTitle) + "'", connection) 
       reader = command.ExecuteReader() 
       While reader.Read() 
        lblTitle.Text = reader(0).ToString() 
        lblConsole.Text = reader(1).ToString() 
        lblYear.Text = reader(2).ToString() 
        lblESRB.Text = reader(3).ToString() 
        lblScore.Text = reader(4).ToString() 
        lblPublisher.Text = reader(5).ToString() 
        lblDeveloper.Text = reader(6).ToString() 
        lblGenre.Text = reader(7).ToString() 
        lblPurchase.Text = Convert.ToDateTime(reader(8).ToString()).ToShortDateString() 
       End While 
      End Using 
     End If 
    End Sub 
End Class 

例が、ラベルがありません行情報が入力されます。何か助けていただければ幸いです。グリッドビューをどのように配置したかなど、必要なコードを投稿することができます。ありがとうございました。

+0

2番目の例は、ラベルの読み込みを試みているのですか、それとも何かのためのものですか? – CodingYoshi

+0

これも試行です。例2aは、元のgridviewのボタンであり、例2bは、コードを表示したいページのvb.netコードです。 –

+0

それはex2でもしようとすると、なぜあなたは 'ポストバックではない'を持っていますか?それはポストバックでなければなりません。また、 'Request.QueryString(" RowIndex ")'で値と正しい値を取得していますか? – CodingYoshi

答えて

0

私の.aspxファイルでAutoEventWireupを "true"に変更するのと同じくらい簡単でした。

関連する問題