I am having a problem dealing with Detailsview InsertItemTemplate. I am following ASP.Net 4 for Beginners by WROX.
I have a dbase with table Employee(EmpId, Name, DoB, Sex, DeptId, Position) I am trying to insert values into this table using Detailsview.
Using the regular detailsview I could insert all the values. It works fine.But when I converted the detailsview to template, there is a problem.
What I did?
-On Smart tag of Detailsview, I clicked Edit Fields. Delete all the avaible fields except EmpID and added Template Field from Available Fields. -Again on Smart Tag of Detailsview, clicked Edit Template and selected InsertItemTemplate in DropDownList. -Using Standard components from Toolbox, I added text box for Name and dropdownlist for other fields. (I have not deleted EmpId, so I do not need one for it). -Here is the rest of Markup view:
Enter following information to register:
Name:
DoB:
Sex:
DeptId:
Position:
-And here is the code behind CS file for Insert event for detailsview
protected void DetailsView1_ItemInserting1(object sender, DetailsViewInsertEventArgs e) { string id = System.Guid.NewGuid().ToString(); TextBox txt_name = (TextBox)DetailsView1.FindControl("EmpId"); e.Values["EmpId"] = id;
DropDownList ddl_month = (DropDownList)DetailsView1.FindControl("DDL_Month");
string month = ddl_month.SelectedValue;
e.Values["DDL_Month"] = month;
DropDownList ddl_day = (DropDownList)DetailsView1.FindControl("DDL_Day");
string day = ddl_day.SelectedValue;
e.Values["DDL_Day"] = day;
DropDownList ddl_year = (DropDownList)DetailsView1.FindControl("DDL_Year");
string year = ddl_year.SelectedValue;
e.Values["DDL_Year"] = year;
DropDownList ddl_sex = (DropDownList)DetailsView1.FindControl("DDL_Sex");
string sex = ddl_sex.SelectedValue;
e.Values["DDL_Sex"] = sex;
DropDownList ddl_pos = (DropDownList)DetailsView1.FindControl("DDL_Pos");
string pos = ddl_pos.SelectedValue;
e.Values["DDL_Pos"] = pos;
DropDownList ddl_deptid = (DropDownList)DetailsView1.FindControl("DDL_DeptId");
string deptid = ddl_deptid.SelectedValue;
e.Values["DDL_DeptId"] = deptid;
}
-Could you also suggest me how to combine the three selected value from dropdownlist for Date of Birth and add it to database.
Thank you!!!