Hi,
I am dynamically adding the row to the gridview which contains the dropdownlist and Label controls, I want to add the selected index change event of the dropdownlist. Please suggest me solution for that.
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.
Ramesh MaruthiPosted Aug 8, 2014, 12:16 PM
http://asimsajjad.blogspot.com/2009/09/raising-dropdownlist.html
http://forums.asp.net/t/1410616.aspx?+SelectedIndexChanged+event+of+a+DropDownList+in+gridview+EditItemTemplate
http://p2p.wrox.com/asp-net-2-0-professional/75742-dropdownlist-selectedindexchanged-gridview.html
Santosh KumarPosted Aug 8, 2014, 8:55 AM
My scenarioa is, my grid contains the itemtemplate with some labels which will diplays the search result(I have some text boxes and a Search Button, need to enter the search criteria and click search button), And I am adding the a next row (after the last row) dynamically with dropdownlist and some labels and i want to create a selected index change event for the dynamically created drop down list.
protected void grdProjectEmpDetails_RowDataBound(object sender, GridViewRowEventArgs e) {
try {
EmployeeBillingBO generic = (EmployeeBillingBO) e.Row.DataItem; // Created new Row if (btnAddNewResource.UniqueID == this.GetPostBackControlID())
{
EmployeeBillingService gs = new EmployeeBillingService(this.ContextBO); //
List < EmployeeBillingBO > gList = var list = gs.GetEmployeeProjectDetails(28, 25855, sonStartDate1.Date, sonEndDate1.Date);
if (DataControlRowType.DataRow == e.Row.RowType && e.Row.RowState != DataControlRowState.Edit && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
Label lblOrbitProjectID = new Label();
lblOrbitProjectID.ID = "lblOrbitProjectIDIns";
lblOrbitProjectID.Attributes.Add("runat", "server");
Label lblEmployeeNameIns = new Label();
lblEmployeeNameIns.ID = "lblEmployeeNameIns";
lblEmployeeNameIns.Attributes.Add("runat", "server");
Label lblPoNumberIns = new Label(); //here i m adding a control.
lblPoNumberIns.ID = "lblPoNumberIns";
lblPoNumberIns.Attributes.Add("runat", "server");
DropDownList ddlEmployeeCode = new DropDownList();
ddlEmployeeCode.ID = "ddlEmployeeCode";
ddlEmployeeCode.Attributes.Add("runat", "server");
ddlEmployeeCode.AutoPostBack = true;
DropDownList ddlEmployeeRole = new DropDownList();
ddlEmployeeRole.ID = "ddlEmployeeRole";
ddlEmployeeRole.Attributes.Add("runat", "server");
int y = grdProjectEmpDetails.Rows.Count - list.Count;
for (int i = 0; i < y + 1; i++)
{
e.Row.Cells[0].Controls.Add(lblOrbitProjectID); e.Row.Cells[1].Controls.Add(ddlEmployeeCode); e.Row.Cells[2].Controls.Add(lblEmployeeNameIns); e.Row.Cells[3].Controls.Add(ddlEmployeeRole); //textbox is added as last column of grid EmployeeService employeeService = new EmployeeService(this.ContextBO);
List < EmployeeBO > employeeList = new List < EmployeeBO > ();
lblOrbitProjectID.Text = "25855";
lblPoNumberIns.Text = "PO12345"; //
ddlEmployeeCode.Attributes.Add("onClick", "EmployeeName();"); ddlEmployeeCode.AppendDataBoundItems = true; employeeList = employeeService.GetEmployeeList();
ddlEmployeeCode.DataSource = employeeList;
// ddlEmployeeCode.AutoPostBack = true; ddlEmployeeCode.SelectedIndexChanged += new System.EventHandler(this.ddlEmployeeCode_SelectedIndexChanged); //ddlEmployeeCode.SelectedIndexChanged +=ddlEmployeeCode_SelectedIndexChanged; ddlEmployeeCode.DataTextField = "EmployeeOrbitCode";
ddlEmployeeCode.DataValueField = "EmployeeOrbitCode";
ddlEmployeeCode.DataBind();
LookupService lookUpService = new LookupService(this.ContextBO);
List < LookupBO > empRoleList = new List < LookupBO > (); ddlEmployeeRole.AppendDataBoundItems = true;
empRoleList = lookUpService.GetLookUpValuesByLookupTypeID((int) LookupTableType.EmployeeRole);
ddlEmployeeRole.DataSource = empRoleList; ddlEmployeeRole.DataTextField =
ddlEmployeeRole.DataValueField = "LookupDescription"; ddlEmployeeRole.DataBind();
}
}
}
}
}
void ddlEmployeeCode_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlEmployeeCode = (DropDownList) sender;
EmployeeService employeeService = new EmployeeService(this.ContextBO);
List < EmployeeBO > employeeList = new List < EmployeeBO > ();
foreach(GridViewRow row in grdProjectEmpDetails.Rows)
{
Label lblEmpname = (Label) row.FindControl("lblEmployeeNameIns") as Label; lblEmpname.Text = Convert.ToString(employeeService.GetEmployeeNameByEmpCode(Convert.ToInt32(ddlEmployeeCode.SelectedItem.Text))); }
}
Ankur JainPosted Aug 8, 2014, 8:47 AM
Try like this...
ddl.SelectedIndexChanged += new System.EventHandler(ddl_SelectedValueChanged);
privatevoid ddl_SelectedValueChanged(object sender, EventArgs e)
{ //Do something here.
}