1

I cannot figure out how to set my selected value after the databind. I store the value in a temp variable and then set it again after the binding but it is not working.

Code Behind

protected void InsertButton_Click(object sender, EventArgs e)
{
    var ctrl = (Control)sender;
    var lvl = (ListViewItem)ctrl.NamingContainer;
    var formSectionListBox = (ListBox)lvl.FindControl("formsection");
    var temp = formSectionListBox.SelectedValue;

    // Update ListView
    ListView1.DataSource = SqlDataSource1;                   
    ListView1.DataBind();
    formSectionListBox.Items.FindByValue(temp).Selected = true;
}

ASP.net

 <asp:ListView ID="ListView1" runat="server" InsertItemPosition="FirstItem" OnPagePropertiesChanged="ListView1_PagePropertiesChanged" OnItemEditing="ListView1_OnItemEditing" DataKeyNames="FormTitle" OnSelectedIndexChanged="ListView1_SelectedIndexChanged" OnItemCanceling="ListView1_OnItemCanceling" OnItemUpdating="ListView1_ItemUpdating" OnItemInserting="ListView1_ItemInserting" OnItemDeleting="ListView1_ItemDeleting">
    <InsertItemTemplate>
        <tr>
            <td>

                <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" OnClick="InsertButton_Click" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" CausesValidation="False" />

            </td>
            <td>
                <div style="height: auto; width: 250px; overflow: auto; border: solid; border-color: ActiveBorder">
                    <asp:ListBox ID="formsection" runat="server" DataSourceID="FormSectionDataSource" DataTextField="FormSection" DataValueField="FormSectionID" AppendDataBoundItems="True" SelectedValue='<%# Bind("FormSectionID") %>' Height="150px">
                        <asp:ListItem Value=""><- please select -></asp:ListItem>
                   </asp:ListBox>

                </div>
            </td>
        </tr>
     </InsertItemTemplate>
</asp:ListView>
user3339242
  • 631
  • 2
  • 15
  • 32
  • Do the selection from the `ItemCreated` method. Look at [this SO Question](http://stackoverflow.com/questions/570801/programmatically-select-item-in-asp-net-listview) for details. – Icemanind Aug 25 '14 at 19:20
  • http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedvalue(v=vs.110).aspx – Dalorzo Aug 25 '14 at 21:30
  • Your CodeBehind does not even match up with ASPX. Please do not post the unnecessary codes - such as **CSS**, **formSectionListBox**, and **ListView**. Instead, please post from which event you are accessing those codes. – Win Aug 25 '14 at 21:33

2 Answers2

1

do

formSectionListBox.SelectedItem = temp;

you can also use the SelectedValue property to set it by the value of temp. Either one should work though.

EDIT: since in your case temp was the value of the item. I would use the

formSectionListBox.SelectedValue = temp;

In general, remember that a lot of these properties are both Get AND Set, not just Get :)

psoshmo
  • 1,490
  • 10
  • 19
  • I tried formSectionListBox.SelectedValue = temp but it did not keep the value after the data bind – user3339242 Aug 25 '14 at 19:34
  • @user3339242 so temp was null after the databind? what was the value of temp before and after, if you don't mind – psoshmo Aug 25 '14 at 19:36
  • no temp actually stays the same which is the value "87" and after the bind the value is also "87", if it helps the selected item text is Assessment which is shown to the user in the list box. The value is just used to insert data into the database. – user3339242 Aug 25 '14 at 19:43
  • @user3339242 not sure if this is causing issues, but delete the SelectedValue attribute from your asp code, you should not need it. so when you say it did not keep the value after the databind, you mean it is not showing the selected item (you are setting the selected item after the databind right?)? – psoshmo Aug 25 '14 at 19:47
  • Removed the selected value property but still nothing. And yes i am setting the selected value after the data bind. And when I select a value to insert and then click insert, after the data binds then that value is no longer selected. It is like something is binding it again but when debugging I am sure it finishes after that – user3339242 Aug 25 '14 at 19:54
  • @user3339242 is a postback (or callback) occurring after the databind? if so you will have to set the SelectedValue after the postback – psoshmo Aug 25 '14 at 19:55
  • How do I find that out? I am not sure where to look? In the code behind or asp.net code? – user3339242 Aug 25 '14 at 19:57
  • @user3339242 check to see if its hitting the page init or page load event after it does the databind. – psoshmo Aug 25 '14 at 20:00
  • @user3339242 I unfortunately can not chat since my work has the chat feature blocked. I am sorry I couldn't help you further on this one :( – psoshmo Aug 25 '14 at 20:04
  • No its not. It hits page load before going into that particular section of the code behind. Also, if I set the listitem property of selected to true, then after databind it will always select that value after the databind. I dont know if that helps – user3339242 Aug 25 '14 at 20:05
  • I am really not sure why it is not working then, and I am about to leave for the day :( I wish you the best of luck though, I am confident youll figure it out! – psoshmo Aug 25 '14 at 20:10
0

You can try binding DropDownList in ItemDataBound

<asp:ListView ID="ListView1" runat="server" 
    ...
    OnItemDataBound="ListView1_ItemDataBound">
    <InsertItemTemplate>
        ...
        <asp:ListBox ID="formsection" 
            runat="server"
            DataTextField="FormSection"
            DataValueField="FormSectionID"
            AppendDataBoundItems="True">
            <asp:ListItem Value="">please select</asp:ListItem>
        </asp:ListBox>
    </InsertItemTemplate>
</asp:ListView>

Code Behind

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        var item = e.Item.DataItem as DataRowView;

        var formsection = e.Item.FindControl("formsection") as ListBox;
        formsection.DataSourceID = FormSectionDataSource;
        formsection.DataBind();
        formsection.SelectedValue = item["FormSectionID"].ToString();
    }
    else if (e.Item.ItemType == ListViewItemType.InsertItemTemplate|| 
       e.Item.ItemType == ListViewItemType.EditItemTemplate)
    {   
       ... // Updated
    }
}
Win
  • 61,100
  • 13
  • 102
  • 181
  • I have been trying to get this working all night from your advice. However, I cant seem to find the reason why formsection is always null. It can't find the control although it binds beforehand and goes through the insert template. – user3339242 Aug 26 '14 at 13:15
  • I updated the answer. If it is insert template, you need to compare **ItemType** to **InsertItemTemplate** and **EditItemTemplate**. – Win Aug 27 '14 at 17:17
  • I tried to compare it to ListViewItemType. InsertItem however it never goes inside the if statement. It is always a dataitem for some reason. – user3339242 Aug 27 '14 at 17:40