how to add data in list view
i have two textboxes that text i have to display in list view
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Upendra Pratap ShahiPosted Oct 1, 2015, 6:58 AM
Design Page-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListViewUsingViewState.aspx.cs" Inherits="ListViewUsingViewState" %>
DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>List View With View State - Upendra Pratap Shahititle>
head>
<body>
<form id="form1" runat="server">
<div>
<%-- Here we add data to bind in ViewState --%>
<div>
<asp:Label ID="Label1" runat="server" Text="Name">asp:Label>
<asp:TextBox ID="TextBox1" runat="server">asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Designation">asp:Label>
<asp:TextBox ID="TextBox2" runat="server">asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="save" OnClick="InsertData" />
div><br />
<%-- end --%>
<%-- listview check --%>
<div>
<asp:ListView ID="ListView1" runat="server" GroupPlaceholderID="groupPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
<LayoutTemplate>
<table border="1">
<tr>
<th>Name
th>
<th>Designation
th>
tr>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1">asp:PlaceHolder>
<tr>
<td colspan="3">
<%-- This part used for paging --%>
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="2">
<Fields>
<asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
ShowNextPageButton="false" />
<asp:NumericPagerField ButtonType="Link" />
<asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />
Fields>
asp:DataPager>
<%-- end --%>
td>
tr>
LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1">asp:PlaceHolder>
tr>
GroupTemplate>
<ItemTemplate>
<td>
<%# Eval("Name") %>
td>
<td>
<%# Eval("Designation") %>
td>
ItemTemplate>
asp:ListView>
div>
<%-- end --%>
div>
form>
body>
html>
Code behind page-
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ListViewUsingViewState : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Designation", typeof(string));
ViewState["DataEntry"] = dt;
BindListView();
}
}
private void BindListView()
{
DataTable dt = new DataTable();
dt = (DataTable)ViewState["DataEntry"];
if (dt.Rows.Count > 0 && dt != null)
{
ListView1.DataSource = dt;
ListView1.DataBind();
}
else
{
ListView1.DataSource = null;
ListView1.DataBind();
}
}
///
/// This function is used to save the data in ViewState
///
/// sender
/// e
protected void InsertData(object sender, EventArgs e)
{
string sName = "";
string sDesg = "";
//here taking data from textbox
if (TextBox1.Text.ToString() == "")
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Enter the name')", true);
return;
}
else
{
sName = TextBox1.Text.ToString();
}
if (TextBox2.Text.ToString() == "")
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Enter the designation')", true);
return;
}
else
{
sDesg = TextBox2.Text.ToString();
}
DataTable dt = new DataTable();
DataRow dr;
//assigning ViewState value in Data Table
dt = (DataTable)ViewState["DataEntry"];
//Adding value in datatable
dr = dt.NewRow();
dr["Name"] = sName;
dr["Designation"] = sDesg;
dt.Rows.Add(dr);
if (dt != null)
{
ViewState["DataEntry"] = dt;
}
//here bind Listview after data save in ViewState
this.BindListView();
//empty the textbox control
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
}
//this is used for paging on ListView
protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
(ListView1.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
this.BindListView();
}
}
Upendra Pratap ShahiPosted Oct 1, 2015, 7:03 AM
Ravi KumarPosted Oct 1, 2015, 6:59 AM