Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.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# 在Gridview的不同页面中显示选定列_C#_Asp.net_Gridview - Fatal编程技术网

C# 在Gridview的不同页面中显示选定列

C# 在Gridview的不同页面中显示选定列,c#,asp.net,gridview,C#,Asp.net,Gridview,有人能帮我吗?我在ASP.NET中有一个名为:info的表,有3列:id、name、code 我希望在一个名为Default.aspx的页面上有一个显示表列的复选框列表,当用户选择要查看的列时,他按下一个按钮将其重定向到另一个页面并显示在Gridview中(Gridview结果将显示在另一个页面中,这非常重要) 第1页: <form id="form1" runat="server"> <div> <asp:Panel ID="Panel1" runat

有人能帮我吗?我在ASP.NET中有一个名为:info的表,有3列:
id、name、code
我希望在一个名为Default.aspx的页面上有一个显示表列的复选框列表,当用户选择要查看的列时,他按下一个按钮将其重定向到另一个页面并显示在Gridview中(Gridview结果将显示在另一个页面中,这非常重要)

第1页:

  <form id="form1" runat="server">
<div>
    <asp:Panel ID="Panel1" runat="server" BackColor="#FFFFCC" Height="165px" Width="155px">
         <asp:CheckBoxList ID="CheckBoxList1" runat="server"></asp:CheckBoxList>
         <br />
         &nbsp;
    </asp:Panel>      
</div>
    <br />
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 26px" Text="Button" />
    <br />
</form>
第2页:

    <asp:Panel ID="Panel2" runat="server" Height="302px" Width="278px">
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" style="margin-left: 0px">

        </asp:GridView>
    </asp:Panel>

你需要跟着,这个

  • 按钮1\u单击
    事件,获取选中复选框的选中列表,并将其作为查询字符串发布到第二页。我知道querystring有长度限制,还有其他传递值的方法(post数据、会话…),但如果列列表较少,则最好使用这种方法
  • 下面的代码将获得所选列的列表,并重定向到第二页

    protected void Button1_Click(object sender, EventArgs e)
    {
        List<string> selected = CheckBoxList1.Items.Cast<ListItem>()
            .Where(li => li.Selected)
            .Select(li => li.Text)
            .ToList();
        string sel = string.Join(",", selected);
        Response.Redirect("Second.aspx?cols=" + sel);
    }
    
    受保护的无效按钮1\u单击(对象发送者,事件参数e)
    {
    所选列表=CheckBoxList1.Items.Cast()
    .Where(li=>li.Selected)
    .Select(li=>li.Text)
    .ToList();
    string sel=string.Join(“,”,选中);
    响应.重定向(“Second.aspx?cols=“+sel”);
    }
    
  • 在第二页中,
    page\u load
    事件读取querystring
    cols
    ,并使用
    gridview
    的Row\u数据绑定事件隐藏除作为querystring传入的列之外的其余列
  • 对于这种情况,您可以参考-或


    同样,有一些选项可以实现这一点,比如生成动态SQL以仅获取所需数据并将其绑定到gridview。但上面的问题似乎很简单,这就是我这样解释的原因。

    到目前为止,你试过什么吗?如果是这样,请将代码张贴在您面临问题的地方!我输入了我一直尝试到现在的代码,但仍然不起作用。首先,您必须将选定的列传递到第二页,然后在第二页中,使用动态SQL选择数据并绑定到gridview,或者简单地在gridview中打开所需的列,并将其余列隐藏起来。对于您的答案,我将尝试使用一些代码,如果有一天您有时间的话。Thx非常模糊的答案。试试这个解决方案。在第二页我必须修改一些东西吗?因为它还是不起作用(@tenebrez123,上面的评论是很好的建议,只需使用
    “选择”+sel+“from Info”
    ,@naveen,你是对的!
    SqlConnection con = new SqlConnection("Server=.\\SQLEXPRESS;Database = ProiectWeb; Integrated Security=True");
    SqlCommand cmd = new SqlCommand();
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
    
        if (!this.IsPostBack)
        {
            cmd.Connection = con;
            loadGrid("SELECT * FROM info");
        }
    }
    
    public void loadGrid(string query)
    {
        DataSet data = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter(query,con);
        adapter.Fill(data);
        GridView1.DataSource = data;
        GridView1.DataBind();
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        List<string> selected = CheckBoxList1.Items.Cast<ListItem>()
            .Where(li => li.Selected)
            .Select(li => li.Text)
            .ToList();
        string sel = string.Join(",", selected);
        Response.Redirect("Second.aspx?cols=" + sel);
    }