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

发布于 2024-07-18 20:48:38 字数 5066 浏览 10 评论 0原文

这是我第三次问这个问题。 我没有得到关于这个的好的答案。 我希望我能得到一些帮助,但我会继续问这个问题,因为这是一个好问题,所以专家不应该忽略这个...

所以我有大约 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>

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

添加于 5/3/09

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

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

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>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素染倾城色 2024-07-25 20:48:38

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

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

如果这个描述不充分,那么你必须说它在哪方面不充分,以便我或其他人可以回答。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文