我有Panel
这个结构
<asp:Panel ID="Custom" Visible="false" runat="server">
Search:
<asp:TextBox ID="TextBox1" AutoPostBack="true" runat="server">
</asp:TextBox>
<asp:ListBox ID="ListBox1" Visible="true" runat="server" DataSourceID="UserSearchSource" SelectionMode="Multiple" DataTextField="username" DataValueField="username"></asp:ListBox>
<asp:SqlDataSource ID="UserSearchSource" runat="server" ConnectionString="<%$ ConnectionStrings:fscauth %>" SelectCommand="SELECT [user] FROM [table] WHERE ([user] LIKE '%' + @username + '%') ORDER BY [username]">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="user" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<br /> <br /> <asp:Button ID="Button2" CausesValidation="false" runat="server" Text="Add" OnClick="Button2_Click" />
</asp:Panel>
所以,我有一个TextBox
在数据库中扮演用户搜索者的角色,结果显示在ListBox
上。在添加Button_Click
时,它会将结果填充到DataTable
中,该Gridview
用作DataTable
数据源。我想要实现的是,每次单击“添加”按钮时,不仅会将结果添加到protected void Button2_Click(object sender, EventArgs e)
{
List<string> lista = getUserList(ListBox1);
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("user", typeof(string)));
for (int i = 0; i < lista.Count;i++ )
{
dr = dt.NewRow();
dr["user"] = lista[i].ToString();
dt.Rows.Add(dr);
}
ViewState["CurrentTable"] = dt;
GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
Button3.Visible = true;
Button1.Visible = true;
MessagePanel.Visible = true;
}
,而且不会删除以前的结果列表。目前我一直在使用的是:
getuserlist
和protected List<string> getUserList(ListBox lb)
{
List<string> li = new List<string>();
// List<string> lista = (List<string>)Session["list"];
foreach (ListItem l in lb.Items)
{
if (l.Selected)
{
li.Add(l.Text);
}
}
Session["list"] = li;
List<string> lista = (List<string>)Session["list"];
return lista;
}
函数:
<form name="myForm" novalidate ng-controller="MainController as vm">
<div>
<input type="text" ng-model="vm.id" name="idInput" required>
<input type="email" ng-model="vm.email" name="emailInput" required>
<input type="text" ng-model="vm.output" name="output">
</div>
</form>
我不太确定我能想达到的目标是什么,但是如果有人可以帮助我,那就太棒了!
答案 0 :(得分:1)
您需要更改此行
DataTable dt = new DataTable();
而不是创建新表,你应该得到现有的表
DataTable dt = GridView1.DataSource as DataTable;
表示获取已分配的gridview的数据源并在其中添加行而不是每次都创建新表