在Gridview中显示逗号分隔的值

本文关键字:分隔 Gridview 显示 | 更新日期: 2023-09-27 18:09:15

我有什么

我有一个绑定到某个数据源的网格视图。在其中,我添加了另一列("Resources"(,它显式地绑定到另一个数据源。

Code.aspx

<asp:GridView ID="GridView1"    OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" runat="server" AutoGenerateColumns="False" DataSourceID="EntityDataSource1">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" />
        <asp:BoundField DataField="Description" HeaderText="Description" ReadOnly="True" SortExpression="Description" />
        <asp:BoundField DataField="TFSId" HeaderText="TFSId" ReadOnly="True" SortExpression="TFSId" />
        <asp:CheckBoxField DataField="IsBillable" HeaderText="IsBillable" ReadOnly="True" SortExpression="IsBillable" />
        <asp:BoundField DataField="Estimate" HeaderText="Estimate" ReadOnly="True" SortExpression="Estimate" />
        <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
        <asp:TemplateField HeaderText="Resources">
             <ItemTemplate  >
             <asp:UpdatePanel ID="updatepanel1" runat="server" >
                <ContentTemplate>
                     <asp:TextBox ID="TextBox1" runat="server" Width="100px"></asp:TextBox>
                    <asp:PopupControlExtender ID="TextBox1_PopupControlExtender" runat="server"
                                 Enabled="True"  TargetControlID="TextBox1" 
                                 PopupControlID="Panel1" OffsetY="22">
                     </asp:PopupControlExtender>
                    <asp:Panel ID="Panel1" runat="server" Height="116px" Width="145px" 
                         BorderStyle="Solid" BorderWidth="2px" Direction="LeftToRight"
                         ScrollBars="Auto" BackColor="#CCCCCC" Style="display: none">
                             <asp:CheckBoxList ID="CheckBoxList1" runat="server" 
                                 DataSourceID="SqlDataSource1" DataTextField="UserId"
                                 DataValueField="UserId" AutoPostBack="True"
                                 OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
                             </asp:CheckBoxList>
                            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                 ConnectionString="<%$ ConnectionStrings:42HNetDbConnectionString %>"
                                 SelectCommand="SELECT DISTINCT [UserId] FROM [Resources]">
                            </asp:SqlDataSource>
                    </asp:Panel>
                </ContentTemplate>
        </asp:UpdatePanel>
       </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

我想要什么:

我已经能够在用户单击文本框后立即显示所有资源的名称。用户可以选择他/她想要的任意多的资源。我需要实现以下目标:

1.我想在用户单击复选框列表后立即显示所有资源的逗号分隔名称。为此,我创建了一个选定的已更改的事件。

2.我还想知道如何记住用户上次选择的内容,因为它不记得了。

我尝试了什么:

代码隐藏

   protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        /*
        string name = "";
        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected)
            {
                name += CheckBoxList1.Items[i].Text + ",";
            }
        }
        TextBox1.Text = name;*/
    }

真正的问题是我无法访问选定的已更改的事件中的CheckBoxList。

如何使用asp.net web表单从代码后面访问文本框、更新面板内的标签似乎没有什么帮助。

如有任何帮助,我们将不胜感激。谢谢

在Gridview中显示逗号分隔的值

最后我得到了它:

代码隐藏

 protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        for (int j = 0; j < GridView1.Rows.Count; j++)
        {
            UpdatePanel up1 = GridView1.Rows[j].FindControl("updatepanel1") as UpdatePanel;
            TextBox tb1 = up1.FindControl("TextBox1") as TextBox;
            CheckBoxList cb1 = up1.FindControl("CheckBoxList1") as CheckBoxList;
            string name = "";
            for (int i = 0; i < cb1.Items.Count; i++)
            {
                if (cb1.Items[i].Selected)
                {
                    name += cb1.Items[i].Text + ",";
                }
            }
            tb1.Text = name;
        }
    }
 string strVendorId = string.Empty;
            var vender = new StringBuilder();
            var vendorcollection = cmbVendorId.CheckedItems;
            foreach (var item in vendorcollection)
                vender.Append(item.Value + ",");
            strVendorId = vender.ToString();
            if (strVendorId != "")
            {
                strVendorId = strVendorId.Remove(strVendorId.Length - 1, 1);
            }

U可以将此代码用作逗号分隔的参考代码