【问题标题】:How to set selected value after data bind数据绑定后如何设置选定值
【发布时间】:2014-08-25 19:16:18
【问题描述】:

我不知道如何在数据绑定后设置我选择的值。我将值存储在临时变量中,然后在绑定后再次设置它,但它不起作用。

代码背后

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>

【问题讨论】:

  • ItemCreated 方法中进行选择。详情请看this SO Question
  • 您的 CodeBehind 甚至与 ASPX 不匹配。请不要发布不必要的代码 - 例如 CSSformSectionListBoxListView。相反,请发布您从哪个事件访问这些代码。

标签: c# asp.net bind selectedvalue


【解决方案1】:

formSectionListBox.SelectedItem = temp;

您也可以使用 SelectedValue 属性将其设置为 temp 的值。任何一个都应该工作。

编辑:因为在您的情况下 temp 是项目的值。我会使用

formSectionListBox.SelectedValue = temp;

一般来说,请记住,其中很多属性都是 Get 和 Set,而不仅仅是 Get :)

【讨论】:

  • 我试过 formSectionListBox.SelectedValue = temp 但数据绑定后没有保留值
  • @user3339242 所以数据绑定后 temp 为空?如果你不介意的话,temp 之前和之后的值是多少
  • no temp 实际上保持不变,即值“87”,绑定后该值也是“87”,如果它有帮助,则所选项目文本是评估,在列表框。该值仅用于将数据插入数据库。
  • @user3339242 不确定这是否会导致问题,但请从您的 asp 代码中删除 SelectedValue 属性,您应该不需要它。所以当你说它在数据绑定之后没有保留值时,你的意思是它没有显示选定的项目(你是在数据绑定之后设置选定的项目吗?)?
  • 删除了选定的值属性,但仍然没有。是的,我在数据绑定后设置选定的值。当我选择一个要插入的值然后单击插入时,数据绑定后,该值不再被选中。好像有什么东西再次绑定了它,但是在调试时我确信它会在那之后完成
【解决方案2】:

你可以尝试在ItemDataBound绑定DropDownList

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

代码隐藏

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

【讨论】:

  • 根据您的建议,我一直在努力让这个工作整晚。但是,我似乎无法找到 formsection 始终为空的原因。尽管它事先绑定并通过插入模板,但它找不到控件。
  • 我更新了答案。如果是插入模板,则需要将ItemTypeInsertItemTemplateEditItemTemplate进行比较。
  • 我试图将它与 ListViewItemType 进行比较。 InsertItem 但是它永远不会进入 if 语句。由于某种原因,它始终是一个数据项。
猜你喜欢
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 2014-10-16
  • 1970-01-01
  • 2014-08-05
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多