如何设置数据绑定后,选定值 [英] How to set selected value after data bind
本文介绍了如何设置数据绑定后,选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法弄清楚如何我所选择的值数据绑定后设置。我存储在一个临时变量的值,然后绑定后再设置它,但它不工作。
code背后
保护无效InsertButton_Click(对象发件人,EventArgs的发送)
{
VAR CTRL =(控制)发送;
VAR LVL =(ListViewItem的)ctrl.NamingContainer;
VAR formSectionListBox =(列表框)lvl.FindControl(formsection);
变种临时= formSectionListBox.SelectedValue; //更新的ListView
ListView1.DataSource = SqlDataSource1;
ListView1.DataBind();
formSectionListBox.Items.FindByValue(临时).Selected = TRUE;
}
ASP.net
< ASP:ListView控件ID =ListView1的=服务器InsertItemPosition =与firstItemOnPagePropertiesChanged =ListView1_PagePropertiesChangedOnItemEditing =ListView1_OnItemEditing的DataKeyNames =FormTitleOnSelectedIndexChanged = ListView1_SelectedIndexChangedOnItemCanceling =ListView1_OnItemCancelingOnItemUpdating =ListView1_ItemUpdatingOnItemInserting =ListView1_ItemInsertingOnItemDeleting =ListView1_ItemDeleting>
<&InsertTemplate则GT;
&所述; TR>
&所述; TD> < ASP:按钮的ID =InsertButton=服务器的CommandName =插入文本=插入的OnClick =InsertButton_Click/>
< ASP:按钮的ID =CancelButton=服务器的CommandName =取消文本=清除的CausesValidation =FALSE/> < / TD>
&所述; TD>
< DIV的风格=高度:汽车;宽度:250像素;溢出:汽车;边界:固体;边框颜色:ActiveBorder>
< ASP:列表框ID =formsection=服务器的DataSourceID =FormSectionDataSourceDataTextField =FormSectionDataValueField =FormSectionIDAppendDataBoundItems =真的SelectedValue ='<%#绑定(FormSectionID)%&GT ;' HEIGHT =150像素>
< ASP:ListItem的值=>< - 请选择 - >< / ASP:ListItem的>
< / ASP:列表框> < / DIV>
< / TD>
< / TR>
< / InsertTemplate则>
< / ASP:的ListView>
解决方案
做
formSectionListBox.SelectedItem =温度;
您也可以使用SelectedValue属性由温度值来设置。无论是一个人应该工作,虽然。
编辑:既然你的情况气温是该项目的价值。我会用
formSectionListBox.SelectedValue =温度;
在一般情况下,请记住,很多这些属性都get和set,而不仅仅是获取:)
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>
解决方案
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 :)
这篇关于如何设置数据绑定后,选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文