I am unable to edit Gender lookup field. I am having problem with post back edits. Every time I edit, the row YourGender keeps getting repeated. I am getting null values for GenderLookup value field. What is the right way to populate dynamically this look up in detailsview control. How does the get request and post back request with this post. I am confused. Please help.....
In the page init I populate the data source as follows:
PopulateLookupDDL("Gender");
and method
private void PopulateLookupDDL(string luControl)
{
SqlDataSource sqlDS = new SqlDataSource(new Connection().ConnectionString, String.Format("Select {0}ID, {0} From tblLkup{0}", luControl));
sqlDS.ID = String.Format("ddlDA{0}",luControl);
pnlLookups.Controls.Add(sqlDS);
}
In my page Load event, I call this method..
PopulateItemTemplate("Gender");
Method looks like the following:
private void PopulateItemTemplate(string luControl)
{
SqlDataSource ds = (SqlDataSource)pnlLookups.FindControl("ddlDAGender");
DataView dvw = (DataView)ds.Select(DataSourceSelectArguments.Empty);
DataTable dt = dvw.ToTable();
DetailsView dv = (DetailsView)LoginView2.FindControl("dvProfile");
TemplateField tf = new TemplateField();
tf.HeaderText = "Your Gender";
tf.ItemTemplate = new ProfileItemTemplate("Gender", ListItemType.Item,dt);
tf.EditItemTemplate = new ProfileItemTemplate("Gender", ListItemType.EditItem,dt);
dv.Fields.Insert(0, tf);
}
Class ProfileItemTemplate looks as follows:
public class ProfileItemTemplate : ITemplate
{
private string ctlName;
ListItemType _lit;
DataTable _dt;
public ProfileItemTemplate(string ControlName, ListItemType lit,DataTable dt)
{
ctlName = ControlName;
_lit = lit;
_dt = dt;
}
public void InstantiateIn(Control container)
{
switch(_lit)
{
case ListItemType.Item :
Label lbl = new Label();
lbl.ID = "lblGender";
lbl.DataBinding += new EventHandler(this.lbl_DataBinding);
container.Controls.Add(lbl);
break;
case ListItemType.EditItem :
DropDownList ddl = new DropDownList();
ddl.ID = "ddlGender";
ddl.DataSource = _dt;
ddl.DataValueField = "GenderID";
ddl.DataTextField = "Gender";
//for new profie we can not bind data..
ddl.DataBinding += new EventHandler(this.ddl_DataBinding);
container.Controls.Add(ddl);
break;
}
}
private void ddl_DataBinding(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
DetailsView container = (DetailsView)ddl.NamingContainer;
ddl.SelectedValue = ((DataRowView)container.DataItem)["GenderID"].ToString();
}
private void lbl_DataBinding(object sender, EventArgs e)
{
Label lbl = (Label)sender;
DetailsView container = (DetailsView)lbl.NamingContainer;
lbl.Text = ((DataRowView)container.DataItem)["Gender"].ToString();
}
}