views:

4712

answers:

5

This is my third time asking this question. I am not getting good answers regarding this. I wish I could get some help but I will keep asking this question because its a good question and SO experts should not ignore this...

So I have about 10 dropdownlist controls that I add manually in the DetailsView control manually like follows. I should be able to add this programmatically. Please help and do not ignore...

          <asp:DetailsView ID="dvProfile" runat="server" 
                AutoGenerateRows="False" DataKeyNames="memberid" DataSourceID="SqlDataSource1" 
                OnPreRender = "_onprerender"

                Height="50px" onm="" Width="125px">

                <Fields>


                   <asp:TemplateField HeaderText="Your Gender">
                            <EditItemTemplate>
                                    <asp:DropDownList ID="ddlGender" runat="server" 
                                                    DataSourceid="ddlDAGender"
                                                    DataTextField="Gender" DataValueField="GenderID"
                                                    SelectedValue='<%#Bind("GenderID") %>'
                                        >

                                     </asp:DropDownList>
                            </EditItemTemplate>

                            <ItemTemplate >
                                        <asp:Label Runat="server" Text='<%# Bind("Gender") %>' ID="lblGender"></asp:Label>
                            </ItemTemplate>

so on and so forth...



                        <asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
                    </Fields>
                </asp:DetailsView>

=======================================================

Added on 5/3/09

This is what I have so far and I still can not add the drop down list programmatically.

private void PopulateItemTemplate(string luControl)
{
    SqlDataSource ds = new SqlDataSource();
    ds = (SqlDataSource)FindControl("ddlDAGender");
    DataView dvw = new DataView();
    DataSourceSelectArguments args = new DataSourceSelectArguments();

    dvw = (DataView)ds.Select(args);
    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);
    tf.EditItemTemplate = new ProfileItemTemplate("Gender", ListItemType.EditItem);
    dv.Fields.Add(tf);
}


    public class ProfileItemTemplate : ITemplate
    {
        private string ctlName;
        ListItemType _lit;
        private string _strDDLName;
        private string _strDVField;
        private string _strDTField;
        private string _strSelectedID;
        private DataTable _dt;


        public ProfileItemTemplate(string strDDLName, 
                                                            string strDVField, 
                                                            string strDTField,
                                                            DataTable dt
                                                            )
        {
            _dt = dt;
            _strDDLName = strDDLName;
            _strDVField = strDVField;
            _strDTField = strDTField;
        }

        public ProfileItemTemplate(string strDDLName,
                                                        string strDVField,
                                                        string strDTField,
                                                        string strSelectedID,
                                                        DataTable dt
                                                        )
        {
            _dt = dt;
            _strDDLName = strDDLName;
            _strDVField = strDVField;
            _strDTField = strDTField;
            _strSelectedID = strSelectedID;
        }

        public ProfileItemTemplate(string ControlName, ListItemType lit)
        {
            ctlName = ControlName;
            _lit = lit;


        }



        public void InstantiateIn(Control container)
        {


            switch(_lit)
            {
                case ListItemType.Item : 
                    Label lbl = new Label();
                    lbl.DataBinding += new EventHandler(this.ddl_DataBinding_item);
                    container.Controls.Add(lbl);
                    break;
                case ListItemType.EditItem :
                    DropDownList ddl = new DropDownList();
                    ddl.DataBinding += new EventHandler(this.lbl_DataBinding);
                    container.Controls.Add(ddl);

                    break;
            }
        }

        private void ddl_DataBinding_item(object sender, EventArgs e)
        {

            DropDownList ddl = (DropDownList)sender;
            ddl.ID = _strDDLName;
            ddl.DataSource = _dt;
            ddl.DataValueField = _strDVField;
            ddl.DataTextField = _strDVField;
        }



        private void lbl_DataBinding(object sender, EventArgs e)
        {
            Label lbl = (Label)sender;
            lbl.ID = "lblGender";

            DropDownList ddl = (DropDownList)sender;
            ddl.ID = _strDDLName;
            ddl.DataSource = _dt;
            ddl.DataValueField = _strDVField;
            ddl.DataTextField = _strDTField;
            for (int i = 0; i < _dt.Rows.Count; i++)
            {
                if (_strSelectedID == _dt.Rows[i][_strDVField].ToString())
                {
                    ddl.SelectedIndex = i;
                }
            }

            lbl.Text = ddl.SelectedValue;
        }
    }

Please help me....

A: 

What have you tried? What problem are you having? If you haven't answered these questions before, then it's no surprise that you haven't received a good answer.

Obviously, you have to locate the TemplateFields for which you want to add dropdowns, and you have to set their EditItemTemplate property to an instance of a class that implements ITemplate. That instance will have it's InstantiateIn method called to add controls to a parent control. In this case, this is where you would configure and add your DropDownList.

If this description is not adequate, then you'll have to say in what way it's not adequate, so that I or someone else can answer.

John Saunders
How do I locate the very first field to which I should add the template field...
dotnet-practitioner
The DetailsView has a Fields property, which has an indexer, so view.Fields[0].
John Saunders
A: 

This is what I have done so far... Please help... It appears that I am unable to reference DetailsViewControl...

In Page_Init method I call the following method:

 PopulateItemTemplate("Gender");

In PopulateItemTemplate I do the following:

private void PopulateItemTemplate(string luControl) {

TemplateField tf = new TemplateField();
tf.HeaderText = "Your Gender";
tf.ItemTemplate = new ProfileItemTemplate("Gender");
tf.EditItemTemplate = new ProfileEditeItemTemplate("Gender");
DetailsView dv = (DetailsView)LoginView2.FindControl("dvProfile");
dv.Fields.Add(tf);  }

My Classes look as follows:

public class ProfileEditeItemTemplate : ITemplate
{
    private string ctlName;
    protected DropDownList ddl;
  public ProfileEditeItemTemplate(string

ControlName) { ctlName = ControlName; }

    public void InstantiateIn(Control container)
    {
        ddl = new DropDownList();
        ddl.ID = "ddlGender";
        ddl.DataSourceID = "ddlDAGender";
        ddl.DataTextField = "Gedner";
        ddl.DataValueField = "GenderID";
        ddl.SelectedValue = "\'<%#Bind(\"GenderID\") %>\'";

        container.Controls.Add(ddl);

    }

}


public class ProfileItemTemplate : ITemplate
{
    private string ctlName;
    protected Label lbl;

  public ProfileItemTemplate(string ControlName)
  {
        ctlName = ControlName;
      //
      // TODO: Add constructor logic here
      //
  }


    public void InstantiateIn(Control container)
    {
        lbl = new Label();
        lbl.ID = "lblGender";
        lbl.Text = "\'<%# Bind(\"Gender\") %>\'" ;
        container.Controls.Add(lbl);
        //            <ItemTemplate >
        //                        <asp:Label Runat="server" Text='<%#

Bind("Gender") %>' ID="lblGender"> //

    }

}

and I get the "Object reference not set to an instance of an object." error at the last line of PopulateItemTemplate method:

dv.Fields.Add(tf);

Please help....

dotnet-practitioner
Delete this and put this information in your original question
Nifle
A: 

I was able to solve the problem "Object reference not set to an instance of an object." by invoking the method in PageLoad event.

Now I am getting the following error when Gender is rendered as follows:

Your Gender '<%# Bind("Gender") %>'

I am trying to get actual Gender value as follows:

Your Gender Male

Any ideas???

dotnet-practitioner
Delete this and put this information in your original question
Nifle
A: 

I have add too many dropdwon in Details view using Template field and binding and getting velue using below code In page load add following code

TemplateField tf = new TemplateField();
tf.HeaderText = "head text";
tf.ItemTemplate = new AddTemplateToDetailsView(ListItemType.Item, dtvProduct.CurrentMode, ID1Att, "");
dtvProduct.Fields.Add(tf);

public class AddTemplateToDetailsView : ITemplate
{
    private ListItemType _ListItemType;
    private DetailsViewMode _DetailsViewMode;
    private String _ControlID;
    private String _ColumnValue;

    public AddTemplateToDetailsView(ListItemType listItemType,DetailsViewMode detailsViewMode, String controlID,String columnValue)
    {
        _ListItemType = listItemType;
        _DetailsViewMode = detailsViewMode;
        _ControlID = controlID;
        _ColumnValue = columnValue;
    }

    public void InstantiateIn(System.Web.UI.Control container)
    {
        if (_ListItemType == ListItemType.Item)
        {
            DropDownList dropdown = new DropDownList();
            dropdown.ID = _ControlID;
            dropdown.AppendDataBoundItems = true;
            dropdown.Width = 160;

            ListItem lt = new ListItem();
            lt.Value = "0";
            lt.Text = "";
            dropdown.Items.Insert(0, lt);

            dropdown.DataSource = dtblLists;
            dropdown.DataTextField = "";
            dropdown.DataValueField = "";

            if (_DetailsViewMode == DetailsViewMode.Edit)
            {
                dropdown.DataBinding += new EventHandler(ddl_DataBinding);
            }

            container.Controls.Add(dropdown);
        }
    }

    void ddl_DataBinding(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        DetailsView container = (DetailsView)ddl.NamingContainer;
        ddl.SelectedValue = ((DataRowView)container.DataItem)["ColumnName"].ToString(); // binding like in my case AttID1
    }
}

In the Detailsview inserting event, u can catch the values using e.Values["AttID1"] = Request.Form["ctl00$cphAdmin$dtvProduct$ID1Att"];
//ctl00$cphAdmin$dtvProduct$ID1Att u can get this string from HTML source //ID1Att this is control id u have supllied from the page load

In the Detailsview updating event, u can catch the values using e.NewValues["AttID1"] = Request.Form["ctl00$cphAdmin$dtvProduct$ID1Att"]; //ctl00$cphAdmin$dtvProduct$ID1Att u can get this string from HTML source //ID1Att this is control id u have supllied from the page load

If u need further help, plz let me know, i will realy appreciate

Muhammad Akhtar
A: 

I found this article http://aspalliance.com/1125 about progrmatically creating template field. I hope this can be helpful to solve this problem.

ajitdh