Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ListView1的选定_indexChanged()上动态绑定ListView2中的数据_C#_Asp.net_.net_Listview - Fatal编程技术网

C# 在ListView1的选定_indexChanged()上动态绑定ListView2中的数据

C# 在ListView1的选定_indexChanged()上动态绑定ListView2中的数据,c#,asp.net,.net,listview,C#,Asp.net,.net,Listview,我在一个项目中工作,我需要在列表视图2中列出数据库子项目谁是父项目列表视图1中的项目。这是我的列表视图1代码 <asp:ListView ID="ListView1"OnSelectedIndexChanged="ListView1_SelectedIndexChanged" runat="server"> <ItemTemplate> <a href='<%# Eval("Module_Redirect") %>'> <

我在一个项目中工作,我需要在列表视图2中列出数据库子项目谁是父项目列表视图1中的项目。这是我的列表视图1代码

 <asp:ListView    
 ID="ListView1"OnSelectedIndexChanged="ListView1_SelectedIndexChanged" runat="server">  
 <ItemTemplate>
 <a href='<%# Eval("Module_Redirect") %>'> <img src="<%# 
 Eval("Module_img") %>" /> </a> 
 </ItemTemplate>
我的观点是:如何在listview1中获取所选项目模板并在listview2中显示相关记录?

您可以尝试一下

            using (SqlConnection con = new SqlConnection("YourConnectioString"))
            {
                using (SqlDataAdapter da2 = new SqlDataAdapter { SelectCommand = new SqlCommand("select * from tbl_Forms where Module_ID = @Module_ID ", con) })
                {
                    // as your using index as the parameter,,
                    da2.SelectCommand.Parameters.AddWithValue("@Module_ID", ListView1.SelectedIndex);

                    // or if your trying to pass parameter Module_ID from ListView1 DataKey ,, you can use SelectedDataKey
                    //da2.SelectCommand.Parameters.AddWithValue("@Module_ID", ListView1.SelectedDataKey);

                    using (DataTable dt2 = new DataTable())
                    {
                        con.Open();

                        da2.Fill(dt2);

                        ListView2.DataSource = dt2;
                        ListView2.DataBind();
                    }
                }
            }

谢谢你的代码。但它不起作用:/。。。。当我选择listview1的项目时,我的listview2没有响应
            using (SqlConnection con = new SqlConnection("YourConnectioString"))
            {
                using (SqlDataAdapter da2 = new SqlDataAdapter { SelectCommand = new SqlCommand("select * from tbl_Forms where Module_ID = @Module_ID ", con) })
                {
                    // as your using index as the parameter,,
                    da2.SelectCommand.Parameters.AddWithValue("@Module_ID", ListView1.SelectedIndex);

                    // or if your trying to pass parameter Module_ID from ListView1 DataKey ,, you can use SelectedDataKey
                    //da2.SelectCommand.Parameters.AddWithValue("@Module_ID", ListView1.SelectedDataKey);

                    using (DataTable dt2 = new DataTable())
                    {
                        con.Open();

                        da2.Fill(dt2);

                        ListView2.DataSource = dt2;
                        ListView2.DataBind();
                    }
                }
            }