asp.net如何以编程方式为大约10个下拉列表添加TemplateField

时间:2009-05-03 01:34:03

标签: asp.net detailsview edititemtemplate

这是我第三次提出这个问题。我对此没有得到很好的答案。我希望我能得到一些帮助,但我会继续问这个问题,因为这是一个很好的问题,SO专家不应该忽视这个......

所以我有大约10个下拉列表控件,我手动在DetailsView控件中手动添加,如下所示。我应该能够以编程方式添加它。请帮助,不要忽视......

  <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>

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

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

于2009年5月3日添加

这是我到目前为止所做的,我仍然无法以编程方式添加下拉列表。

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;
    }
}

2 个答案:

答案 0 :(得分:2)

我发现这篇文章http://aspalliance.com/1125关于以编程方式创建模板字段。我希望这对解决这个问题很有帮助。

答案 1 :(得分:1)

你有什么尝试?你有什么问题?如果你之前没有回答过这些问题,那么你没有得到一个好的答案也就不足为奇了。

显然,您必须找到要为其添加下拉列表的TemplateFields,并且必须将其EditItemTemplate属性设置为实现ITemplate的类的实例。该实例将调用它的InstantiateIn方法将控件添加到父控件。在这种情况下,您可以在此处配置和添加DropDownList。

如果这种描述不充分,那么你将不得不说出哪种方式不合适,以便我或其他人可以回答。