Click here to Skip to main content
14,828,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am dynamically adding the row to the gridview which contains the dropdownlist and Label controls, I want the selected index change event of the dropdownlist. Please suggest me solution for that.
Posted

   
Comments
santoshkumar413 8-Aug-14 9:03am
   
@Pawan Thanks for the reply

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))); }
}
C#
protected void dropdown_SelectedIndexChanged(object sender, EventArgs e)
   {
       GridViewRow grdRow = ((TextBox)sender).Parent.Parent as GridViewRow;
       DropDownList dropdown = (DropDownList)grd.Rows[grdRow .RowIndex].FindControl("dropdown");
  }
   
Comments
santoshkumar413 8-Aug-14 9:05am
   
@Jacobjohn, Thansk for the reply, I have tried in same way, My selected index change event is not firing, please go through the above comment by me, which contains my Code.
   
Comments
santoshkumar413 8-Aug-14 9:09am
   
@jokler thanks for the reply,
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.


Please see above comment by me which contains my source code
jokler.007 8-Aug-14 9:24am
   
check this link please http://myaspsnippets.blogspot.com/2012/10/gridview-dropdownlist-selected-index.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900