【问题标题】:Select the row from the gridview and pass that value to other page从 gridview 中选择行并将该值传递给其他页面
【发布时间】:2015-11-03 19:41:26
【问题描述】:

我对 .net 和 C# 很陌生

我早上一直在寻找解决方案,但还是找不到。

我尝试将用户在 Gridview2 上使用 Session() 选择的这个选定项目传递到其他页面。

我认为“//这部分缺失/错误。”我认为我在代码上注释的这部分有问题。

而且我不确定我应该使用GridView2_SelectedIndexChanged,我在设计视图上单击“选择”,然后GridView2_SelectedIndexChanged 出现了。

那么在这个 Gridview2 上选择并将值传递给其他页面的最佳方法是什么。

隐藏复制代码

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:FleetManagementConnectionString %>" SelectCommand="SELECT [GenreID], [Genre] FROM [Genres] WHERE ([GenreID] = @GenreID)">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="GridViewGenre" Name="GenreID" PropertyName="SelectedValue" Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>




 protected void GridView2_SelectedIndexChanged(object sender, GridViewSelectEventHandler e)
//this part is missing/wrong.     

{


            String Artists = System.Configuration.ConfigurationManager.ConnectionStrings["FleetManagementConnectionString"].ConnectionString;
            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(Artists);



            SqlCommand cmd = new SqlCommand("Select * from Genres where Genre=@Genre", con);
            cmd.Parameters.AddWithValue("@Genre",GridView2.Rows[e].Cells[2].Text );
                                                 //this part is missing/wrong.


            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            con.Open();
            int i = cmd.ExecuteNonQuery();
            con.Close();



            int GenreId = -1;

【问题讨论】:

    标签: c# asp.net .net gridview


    【解决方案1】:
    GridViewrow row = GridViewGenre.SelectedRow;
    
    string id = row.Cell[2].Text
    cmd.Parameters.AddWithValue(@Genre, id)
    

    取决于您可能需要转换为 bool 或 int 等的数据类型?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-25
      • 2016-08-22
      • 2020-11-08
      • 1970-01-01
      • 2016-09-04
      • 1970-01-01
      • 2016-06-06
      相关资源
      最近更新 更多