Gridview,选择,传递“ID”到其他页面 [英] Gridview, Select, pass "ID" to other pages

查看:92
本文介绍了Gridview,选择,传递“ID”到其他页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是非常新的.net和C#



我尝试传递用户在Gridview2上选择的所选项目,使用Session()到其他页面。 />


我认为//这部分缺失/错误。 < -----我假设这部分我对代码发表评论是错误的。



而且我也不确定我应该使用GridView2_SelectedIndexChanged,I单击设计视图上的选择并显示GridView2_SelectedIndexChanged。



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



I am very new to .net and C#

I try to pass this selected item which users choose on Gridview2 using Session() to other page.

I think that "//this part is missing/wrong." <----- I assume that this part I commented on the code is something wrong.

and also I am not sure that I should use GridView2_SelectedIndexChanged, I clicked "select" on Design View and GridView2_SelectedIndexChanged showed up.

So what is the best way to select on this Gridview2 and pass the value to other page.

<pre lang="sql"><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;
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    GenreId = int.Parse(row["GenreID"].ToString());

                }
            }

            if (GenreId != -1)
            {
                Session["genreid"] = GenreId;

                MessageLabel.Text = "Successful";
                Response.Redirect("~/yes.aspx");



            }
            else
            {
              
                MessageLabel.Text = "not";
                MessageLabel.ForeColor = System.Drawing.Color.Red;
                


            }

推荐答案

ConnectionStrings:FleetManagementConnectionString%> SelectCommand = SELECT [GenreID],[Genre] FROM [类型] WHERE([GenreID] = @GenreID) >
< SelectParameters >
< asp:ControlParameter ControlID = GridViewGenre 名称 = GenreID PropertyName = SelectedValue 类型 = Int32 / >
< ; / SelectParameters >
< / asp:SqlDataSource >
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;
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    GenreId = int.Parse(row["GenreID"].ToString());

                }
            }

            if (GenreId != -1)
            {
                Session["genreid"] = GenreId;

                MessageLabel.Text = "Successful";
                Response.Redirect("~/yes.aspx");



            }
            else
            {
              
                MessageLabel.Text = "not";
                MessageLabel.ForeColor = System.Drawing.Color.Red;
                


            }


这篇关于Gridview,选择,传递“ID”到其他页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆