绑定一个DropDownList一个DetailsView内 [英] Binding a DropDownList inside a DetailsView

查看:155
本文介绍了绑定一个DropDownList一个DetailsView内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题想填充从数据库中一个DropDownList。当我试图设置数据源我找不到下拉控件,它在一个DetailsView所以我认为它可能是与当它在编辑模式下只被创建。它还说,这是在目前的模式下,当我编辑的,所以不知道这是怎么回事那里。

下面是从aspx文件的code:

 < ASP:DetailsView控件ID =DetailsView1=服务器AutoGenerateRows =假的DataSourceID =myMySqlDataSrcDataKeyNames =IDAutoGenerateDeleteButton =真AutoGenerateEditButton属性=真AutoGenerateInsertButton =FALSE>
     <场>
        < ASP:模板列HEADERTEXT =区域>
            <的ItemTemplate><%#的eval(的region_name)%>< / ItemTemplate中>
            < EditItemTemplate中>
                < ASP:DropDownList的ID =RegionDropdownList=服务器的SelectedValue ='<%#绑定(REGION_ID)%> />
            < / EditItemTemplate中>
        < / ASP:的TemplateField>
     < /场>
< / ASP:DetailsView控件>
 

这是从后面的code:

  ArrayList的regionsList = BPBusiness.getRegions();
如果(DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
    DropDownList的ddlRegions =(DropDownList的)DetailsView1.FindControl(RegionDropdownList);
    如果(ddlRegions!= NULL)
    {
        ddlRegions.DataSource = regionsList;
        ddlRegions.DataBind();
    }
}
 

解决方案

如果是没有准备好,将样品从背后DetailsView1_ModeChanged或DetailsView1_DataBound方法在你的code。如果它是在DetailsView1_ModeChanging方法,所述模式还没有实际改变爱好。

编辑:同时,请确保您设置了DataTextField和DataValueField像这样:

  DropDownList1.DataTextField =TextFieldName;
DropDownList1.DataValueField =ValueFieldName;
 

另外取出的SelectedValue绑定;它什么都不做,除了引发错误。

编辑2:如果您真的需要选择DropDownList中的一个特定的值时,它首先是数据绑定,你可以做这样的事情:

 如果(DropDownList1.Items.Contains(DropDownList1.Items.FindByValue(值)))
{
    DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(值));
}
 

I'm having problems trying to populate a dropdownlist from the database. When I'm trying to set the datasource I can't find the dropdown control, it's in a DetailsView so I think it might have something to do with it only being created when it's in edit mode. It still says it's in current mode when I'm editing though, so not sure what's going on there.

Here's the code from the aspx file:

<asp:DetailsView id="DetailsView1" runat="server" AutoGenerateRows="false" DataSourceID="myMySqlDataSrc"  DataKeyNames="id" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateInsertButton="False" >
     <Fields>
        <asp:TemplateField HeaderText="Region">
            <ItemTemplate><%# Eval("region_name") %></ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="RegionDropdownList" runat="server" SelectedValue='<%# Bind("region_id")%>' />
            </EditItemTemplate>
        </asp:TemplateField>        
     </Fields>
</asp:DetailsView>

And this is from the code behind:

ArrayList regionsList = BPBusiness.getRegions();
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
    DropDownList ddlRegions = (DropDownList)DetailsView1.FindControl("RegionDropdownList");
    if (ddlRegions != null)
    {
        ddlRegions.DataSource = regionsList;
        ddlRegions.DataBind();
    }
}

解决方案

If it isn't already, place the sample from your code behind inside the DetailsView1_ModeChanged or DetailsView1_DataBound method. If it is in the DetailsView1_ModeChanging method, the mode has not actually changed yet.

EDIT: Also, make sure you set up the DataTextField and DataValueField like so:

DropDownList1.DataTextField = "TextFieldName";
DropDownList1.DataValueField = "ValueFieldName";

Also remove the SelectedValue bind; it does nothing except throw errors.

EDIT 2: If you really need to select a particular value of the dropdownlist when it first is databind, you could do something like this:

if(DropDownList1.Items.Contains(DropDownList1.Items.FindByValue("Value")))
{
    DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("Value));
}

这篇关于绑定一个DropDownList一个DetailsView内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆