TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
sagar Bhosale
NA
179
176k
How Add DropDownlist Dynamically in Gridview Without Postback Page
Apr 4 2012 11:59 PM
this my aspx page
<%@ Page Language="C#" MasterPageFile="~/Admin/AdminMasterPage.master" AutoEventWireup="true" CodeFile="MailDemo.aspx.cs" Inherits="Admin_MailDemo" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentAdmin" Runat="Server">
<asp:gridview ID="MailGrid" runat="server" ShowFooter="true" HeaderStyle-CssClass="GridHeadCenter"
AutoGenerateColumns="false" OnRowDataBound="MailGrid_RowDataBound">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Sr.No." />
<asp:TemplateField HeaderText="Report">
<ItemTemplate>
<asp:DropDownList ID="ddlReport" runat="server"
CssClass="DropDown">
<asp:ListItem Value="-1">Select</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Time Frame">
<ItemTemplate>
<asp:DropDownList ID="ddl_TimeFrame" runat="server" CssClass="DropDown">
<asp:ListItem Selected="True" Text="Daily Once" Value="D"></asp:ListItem>
<asp:ListItem Text="Weekly Once" Value="W"></asp:ListItem>
<asp:ListItem Text="Forthnightly" Value="F"></asp:ListItem>
<asp:ListItem Text="Monthly Once" Value="M"></asp:ListItem>
<asp:ListItem Text="Quarterly Once" Value="Q"></asp:ListItem>
<asp:ListItem Text="Yearly Once" Value="Y"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type of Attachement">
<ItemTemplate>
<asp:DropDownList ID="ddl_Attachement" runat="server" CssClass="DropDown">
<asp:ListItem Selected="True" Text="XLS" Value="XLS"></asp:ListItem>
<asp:ListItem Text="PDF" Value="PDF"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Template">
<ItemTemplate>
<asp:DropDownList ID="ddlTemplate" runat="server" CssClass="DropDown" DataTextField="TemplateName" DataValueField="TemplateId">
</asp:DropDownList>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server"
Text="Add New Row" CssClass="SaveButton" Width="100"
onclick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
</asp:Content>
----
this is my .cs page
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Admin_MailDemo : System.Web.UI.Page
{
Admin ObjAdmin = new Admin();
Transaction TrObj = new Transaction();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}
//private ArrayList GetDummyData()
//{
// ArrayList arr = new ArrayList();
// arr.Add(new ListItem("Item1", "1"));
// arr.Add(new ListItem("Item2", "2"));
// arr.Add(new ListItem("Item3", "3"));
// arr.Add(new ListItem("Item4", "4"));
// arr.Add(new ListItem("Item5", "5"));
// return arr;
//}
//private void FillDropDownList(DropDownList ddl)
//{
// ArrayList arr = GetDummyData();
// foreach (ListItem item in arr)
// {
// ddl.Items.Add(item);
// }
//}
public void SetInitialRow()
{
//int i;
//i = 0;
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));//for DropDownList selected item
dt.Columns.Add(new DataColumn("Column2", typeof(string)));//for DropDownList selected item
dt.Columns.Add(new DataColumn("Column3", typeof(string)));//for DropDownList selected item
dt.Columns.Add(new DataColumn("Column4", typeof(string)));//for DropDownList selected item
dr = dt.NewRow();
dr["RowNumber"] =1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState for future reference
ViewState["CurrentTable"] = dt;
//Bind the Gridview
MailGrid.DataSource = dt;
MailGrid.DataBind();
//After binding the gridview, we can then extract and fill the DropDownList with Data
//DropDownList ddlTemplate = (DropDownList)MailGrid.Rows[0].Cells[6].FindControl("ddlTemplate");
//DropDownList ddlReport = (DropDownList)MailGrid.Rows[0].Cells[3].FindControl("ddlReport");
//FillDropDownList(ddlTemplate);
//FillDropDownList(ddlReport);
}
private void AddNewRowToGrid()
{
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;
//add new row to DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Store the current data to ViewState for future reference
ViewState["CurrentTable"] = dtCurrentTable;
for (int i = 0; i < dtCurrentTable.Rows.Count - 1; i++)
{
//extract the DropDownList Selected Items
DropDownList ddlReport = (DropDownList)MailGrid.Rows[i].Cells[1].FindControl("ddlReport");
DropDownList ddl_TimeFrame = (DropDownList)MailGrid.Rows[i].Cells[2].FindControl("ddl_TimeFrame");
dtCurrentTable.Rows[i]["Column1"] = ddlReport.SelectedItem.Text;
dtCurrentTable.Rows[i]["Column2"] = ddl_TimeFrame.SelectedItem.Text;
DropDownList ddl_Attachement = (DropDownList)MailGrid.Rows[i].Cells[3].FindControl("ddl_Attachement");
DropDownList ddlTemplate = (DropDownList)MailGrid.Rows[i].Cells[4].FindControl("ddlTemplate");
// Update the DataRow with the DDL Selected Items
dtCurrentTable.Rows[i]["Column3"] = ddl_Attachement.SelectedItem.Text;
dtCurrentTable.Rows[i]["Column4"] = ddlTemplate.SelectedItem.Text;
}
//Rebind the Grid with the current data to reflect changes
MailGrid.DataSource = dtCurrentTable;
MailGrid.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList ddlReport = (DropDownList)MailGrid.Rows[i].Cells[1].FindControl("ddlReport");
DropDownList ddl_TimeFrame = (DropDownList)MailGrid.Rows[i].Cells[2].FindControl("ddl_TimeFrame");
DropDownList ddl_Attachement = (DropDownList)MailGrid.Rows[i].Cells[3].FindControl("ddl_Attachement");
DropDownList ddlTemplate = (DropDownList)MailGrid.Rows[i].Cells[4].FindControl("ddlTemplate");
//Fill the DropDownList with Data
//FillDropDownList(ddl1);
//FillDropDownList(ddl2);
if (i < dt.Rows.Count - 1)
{
//Set the Previous Selected Items on Each DropDownList on Postbacks
ddlReport.ClearSelection();
ddlReport.Items.FindByText(dt.Rows[i]["Column1"].ToString()).Selected = true;
ddl_TimeFrame.ClearSelection();
ddl_TimeFrame.Items.FindByText(dt.Rows[i]["Column2"].ToString()).Selected = true;
ddl_Attachement.ClearSelection();
ddl_Attachement.Items.FindByText(dt.Rows[i]["Column3"].ToString()).Selected = true;
ddlTemplate.ClearSelection();
ddlTemplate.Items.FindByText(dt.Rows[i]["Column4"].ToString()).Selected = true;
}
rowIndex++;
}
}
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
protected void MailGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlTemplate = (DropDownList)e.Row.FindControl("ddlTemplate");
DropDownList ddlReport = (DropDownList)e.Row.FindControl("ddlReport");
//DropDownList ddlTemplate = (DropDownList)MailGrid.Rows[0].Cells[6].FindControl("ddlTemplate");
//DropDownList ddlReport = (DropDownList)MailGrid.Rows[0].Cells[3].FindControl("ddlReport");
//ListItem Ci = new ListItem("Select", "0");
// ddlgdCategory.Items.Insert(0, Ci);
TrObj.FillDropDown(ddlReport, ((DataSet)ObjAdmin.getAdmin("Mail_Report", "")).Tables[0], "MenuName", "MenuId");
TrObj.FillDropDown(ddlTemplate, ((DataSet)ObjAdmin.Fill_Template("", "")).Tables[0], "TemplateName", "TemplateId");
//ds = ObjAdmin.Fill_Template(opt, TemplateId);
//if (ds.Tables[0].Rows.Count > 0)
//{
// ddlTemplate.DataTextField = "TemplateName";
// ddlTemplate.DataValueField = "TemplateId";
// ddlTemplate.DataSource = ds;
// ddlTemplate.DataBind();
}
}
}
------------
here i have added dropdownllist but page is postback every time
ho i will avoid page postback after aading dropdownlist
plz help me
Reply
Answers (
1
)
HTML & Server Controls In ASP.NET
What is Ispostback() method in ASP.Net