How to access the selected item in a Droplist in Code Behind? [duplicate]

随声附和 提交于 2020-08-11 17:34:34

问题


I have a Droplist in one of my templates which is populated with some content items. In Code Behind I want to access the selected Item in the Droplist. I searched Google, but haven't found anything.

Anyone can show me how to access a Droplist and get its selected Item in C#?


回答1:


The answer isn't quite as straight-forward as you think. Sitecore has two field types that allow users to select an item from a list of items: Droplist and Droplink.

Droplist fields store the name of the item selected by the user, but not a reference to the item itself. This is useful only for when you have an incredibly basic selection and know you will never need to provide more information.

Droplink fields store the ID of the item selected by the user, and can be accessed by doing the following:

public Item GetSelectedItemFromDroplinkField(Item item, string fieldName)
{
    ReferenceField field = item.Fields[fieldName];
    if (field == null || field.TargetItem == null)
    {
        return null;
    }

    return field.TargetItem;
}

My suggestion would be changing the field type to a Droplink, if it's a simple change for you to make, and it won't impact existing content. If you can't do this, then the following code might help you out:

public Item GetSelectedItemFromDroplistField(Item item, string fieldName)
{
    Field field = item.Fields[fieldName];
    if (field == null || string.IsNullOrEmpty(field.Value))
    {
        return null;
    }

    var fieldSource = field.Source ?? string.Empty;
    var selectedItemPath = fieldSource.TrimEnd('/') + "/" + field.Value;
    return item.Database.GetItem(selectedItemPath);
}

It works by taking the name of the selected item, and appending it to the source attribute (which you set on the template). It's far from perfect, but it's along the right lines and should get you on the right track.




回答2:


You can select by dropdown ID

dropdownID.SelectedValue.ToString()



回答3:


You can test

var selectedOption = select1.SelectedValue;

Visit Getting HTML drop down list value from code behind




回答4:


page.aspx:

<asp:DropDownList ID="ddlTipoDocumento" runat="server">
  <asp:ListItem Selected="True" Value="0">Select</asp:ListItem>
  <asp:ListItem Value="1">Element 1</asp:ListItem>
  <asp:ListItem Value="1">Element 2</asp:ListItem>
</asp:DropDownList>

page.aspx.cs(code behind):

ddlTipoDocumento.SelectedItem.Value;



回答5:


you can access the value with the property SelectedValue. for example: var Value = DropDownList1.SelectedValue but you nned to enable autopostback on the control like this

<asp:DropDownList ID="DropDownList1" runat="server" autopostback="true">



回答6:


The value of a #Sitecore Droplist field type is a string, and it is the name of the item that is selected.

To get the value out, you can simply do item["MyField"]

This may be a duplicate question - How to use Droplist types in Sitecore template fields




回答7:


Something like:

myDropDownList.SelectedIndex = 1;

or if you want to read the value:

myDropDownList.Text == "test"

ASP:

                   <asp:DropDownList ID="myDropDownList" runat="server" ViewStateMode="Enabled" class="sys_small">
                            <asp:ListItem Value="test1" Text="test1"></asp:ListItem>
                            <asp:ListItem Value="test2" Text="test2"></asp:ListItem>
                            <asp:ListItem Value="test3" Text="test3"></asp:ListItem>
                            <asp:ListItem Value="test4" Text="test4"></asp:ListItem>
                        </asp:DropDownList>



回答8:


mark up

<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddListTest" runat="server">
            <asp:ListItem Value="item1" Selected="True">Item 1</asp:ListItem>
            <asp:ListItem Value="item2">Item 2</asp:ListItem>
            <asp:ListItem Value="item3">Item 3</asp:ListItem>
        </asp:DropDownList>
    </div>
    </form>
</body>

C# code

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ddListTest.ClearSelection();
        ddListTest.Items.FindByValue("item2").Selected = true;
    }
}

this may help you

for more info




回答9:


You can access sitecore field as:
Item item = Sitecore.Context.Item;
item.Fields["Droplink"].Value;



来源:https://stackoverflow.com/questions/29541846/how-to-access-the-selected-item-in-a-droplist-in-code-behind

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!