C# 数据绑定后如何设置所选值
我不知道如何在数据绑定后设置所选值。我将该值存储在一个临时变量中,然后在绑定后再次设置它,但它不起作用 代码隐藏C# 数据绑定后如何设置所选值,c#,asp.net,bind,selectedvalue,C#,Asp.net,Bind,Selectedvalue,我不知道如何在数据绑定后设置所选值。我将该值存储在一个临时变量中,然后在绑定后再次设置它,但它不起作用 代码隐藏 protected void InsertButton_Click(object sender, EventArgs e) { var ctrl = (Control)sender; var lvl = (ListViewItem)ctrl.NamingContainer; var formSectionListBox = (ListBox)lvl.FindC
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>
do
您还可以使用SelectedValue属性通过temp的值进行设置。不过,这两种方法都应该有效
编辑:因为在您的情况下,temp是项目的值。我会使用
formSectionListBox.SelectedValue = temp;
一般来说,请记住,这些属性中有很多都是Get和Set,而不仅仅是Get:)您可以尝试在中绑定DropDownList
从
itemscreated
方法中进行选择。查看详细信息。您的代码隐藏甚至与ASPX不匹配。请不要发布不必要的代码-例如CSS、formSectionListBox和ListView。相反,请发布您访问这些代码的事件。我尝试了formSectionListBox.SelectedValue=temp,但它没有保留数据后面的值bind@user3339242那么在数据绑定之后temp是空的?temp前后的值是多少,如果您不介意,没有一个temp实际上保持不变,即值“87”,绑定后的值也是“87”,如果有帮助,则所选项目文本为评估,并在列表框中显示给用户。该值仅用于将数据插入数据库。@user3339242不确定这是否会导致问题,但从asp代码中删除SelectedValue属性,您应该不需要它。因此,当您说它没有保留数据绑定后的值时,您的意思是它没有显示所选项目(您正在将数据绑定后的所选项目设置为正确吗?)删除了“所选值”属性,但仍然没有显示任何内容。是的,我在数据绑定后设置所选值。当我选择一个要插入的值,然后单击“插入”,数据绑定后,该值将不再被选择。这就像是有什么东西再次绑定它,但当调试完成后,我确信我一直在努力让它工作了一整晚,从你的建议。然而,我似乎找不到formsection总是空的原因。它找不到控件,尽管它事先绑定并通过插入模板。我更新了答案。如果是insert template,则需要将ItemType与InsertItemTemplate和EditItemTemplate进行比较。我试图将其与ListViewItemType进行比较。然而,InsertItem从不进入if语句内部。出于某种原因,它总是一个数据项。
formSectionListBox.SelectedValue = temp;
<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
}
}