c# – 在Gridview中显示逗号分隔值
作者:互联网
是)我有的
我有一个gridview绑定到一些数据源.在里面我已经添加了另一列(“资源”)明确限制到另一个数据源.
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似乎没什么帮助.
任何帮助,将不胜感激.谢谢!!!
解决方法:
终于我明白了:
代码背后
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;
}
}
标签:c,asp-net,gridview,updatepanel 来源: https://codeday.me/bug/20190703/1370303.html