本文介绍了将数据从一个列表视图转移到另一个列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hi pz帮助我,希望通过单击按钮将数据从一个列表视图传输到另一个列表视图.
hi pz help me outi want to transfer data from one list view to another listview on button click
推荐答案
Hi ,
Hope this Example Help You , On this Example
Add new Item programmatically to listview
Move Item from Listview to another Listview
Hope it Help you
背后的代码:
Code behind:
DataTable Dt = new DataTable();
DataTable Dt2 = new DataTable();
//save
protected void Button2_Click(object sender, EventArgs e)
{
if (TextBox1.Text == string.Empty)
{
lblStatus.Text = "Enter value ";
return;
}
if (ViewState["Dt"] == null)
{
DataColumn col = new DataColumn("test");
col.DataType = System.Type.GetType("System.String");
Dt.Columns.Add(col);
}
else
{
Dt = (DataTable)ViewState["Dt"];
}
DataRow dr = Dt.NewRow();
dr[0] = TextBox1.Text;;
Dt.Rows.Add(dr);
ListView1.DataSource = Dt;
ListView1.DataBind();
ViewState.Add("Dt", Dt);
lblStatus.Text = "your Item saved ";
TextBox1.Text = string.Empty;
}
protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
ListView1.SelectedIndex = e.NewSelectedIndex;
lblStatus.Text = "your Item selected ";
Dt = (DataTable)ViewState["Dt"];
ListView1.DataSource = Dt;
}
//move
protected void Button1_Click(object sender, EventArgs e)
{
//retrive the from datatable Stored in Viewstate
Dt = (DataTable)ViewState["Dt"];
//check on the viewstate if not exist i will create strucre for the table
if (ViewState["Dt2"] == null)
{
DataColumn col = new DataColumn("test");
col.DataType = System.Type.GetType("System.String");
Dt2.Columns.Add(col);
}
else
{
Dt2 = (DataTable)ViewState["Dt2"];
}
// this can be useful if you can select more than item thorugh checkbox etc .
// object[] xx = Dt.Rows[1].ItemArray;
//foreach (string item in xx)
//{
// string yy = item[0].ToString();
//}
//stor the value of selected item in local variable
string ItemValue = Dt.Rows[ListView1.SelectedIndex][0].ToString();
//remove the item from Datatable in listview1
Dt.Rows.RemoveAt(ListView1.SelectedIndex);
//create new row in 2th datatable , storeing the value
DataRow dr2 = Dt2.NewRow();
dr2[0] = ItemValue;
Dt2.Rows.Add(dr2);
ListView2.DataSource = Dt2;
ListView2.DataBind();
ViewState.Add("Dt2", Dt2);
ListView1.DataSource = Dt;
ListView1.DataBind();
ViewState.Add("Dt", Dt);
lblStatus.Text = "your Item Moved ";
}
<div>
<asp:ListView ID="ListView1" runat="server"
onselectedindexchanging="ListView1_SelectedIndexChanging">
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="lnkSelect" Text="Select" CommandName="Select" runat="server" />
</td>
<td>
<%# Eval("test")%>
</td> <br />
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr style="background-color: #FFFF66; color: #808000;">
<td>
<asp:LinkButton ID="lnkSelect" Text="Select" CommandName="Select" runat="server"
ForeColor="Red" />
</td>
<td style="background-color: #FFFF66; color: #808000;">
<%# Eval("test")%>
</td> <br />
</tr>
</SelectedItemTemplate>
</asp:ListView>
<br />
<asp:Button ID="Button1" runat="server" Text="Move"
onclick="Button1_Click" style="height: 26px" />
<asp:Label ID="lblStatus" runat="server"></asp:Label>
<br />
<asp:ListView ID="ListView2" runat="server">
<ItemTemplate>
<tr style="background-color: #00FF00">
<td>
<%# Eval("test")%>
</td> <br />
</tr>
</ItemTemplate>
</asp:ListView>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Save" onclick="Button2_Click" />
</div>
Best Regards
M.Mitwalli
这篇关于将数据从一个列表视图转移到另一个列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!