在Gridview中显示逗号分隔值

时间:2013-04-15 05:32:21

标签: c# asp.net gridview updatepanel

我有什么

我有一个网格视图绑定到一些数据源。在内部我已经添加了另一个列(“资源”)明确限制到另一个数据源。

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.我想在用户点击复选框列表时立即显示所有资源的逗号分隔名称。为此,我创建了onselectedindexchanged事件。

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;*/
    }

真正的问题是我无法在onselectedindexchanged事件中访问CheckBoxList。

How to access textbox, label inside update panel from code behind using asp.net web forms似乎不那么有用。

任何帮助将不胜感激。感谢!!!

2 个答案:

答案 0 :(得分:2)

最后我明白了:

代码背后

 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;
        }
    }

答案 1 :(得分:0)

 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);
            }

你可以使用此代码作为逗号分隔的参考代码