4

我有的

我有一个绑定到某个数据源的gridview。在其中,我添加了另一列(“资源”),明确绑定到另一个数据源。

代码.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。

如何使用 asp.net Web 表单从代码后面的代码访问文本框、更新面板内的标签似乎不太有用。

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

4

2 回答 2

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;
        }
    }
于 2013-04-16T06:45:50.180 回答
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);
            }

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

于 2013-04-15T05:37:59.773 回答