gpt4 book ai didi

c# - Asp.net 更新 DropDownList 选中项

转载 作者:太空宇宙 更新时间:2023-11-03 13:04:37 25 4
gpt4 key购买 nike

我在将 DropDownList 项目用于表单时遇到了问题。主表单由 SQLDataSource 提供...所有表单小部件都绑定(bind)到特定的记录字段等。我用 DropDownList 更改了其中一个表单小部件(文本框),以允许用户仅在多个值之间进行选择...我通过另一个 SQLDataSource 提供此控件。当表单处于插入模式时,没有问题,但是当我切换到编辑模式时,我无法使用来自主记录的值设置 DropDownList 所选项目。我可以解决这个问题吗?这是我的 .aspx 代码示例:

<asp:FormView ID="_frmData" runat="server" DataKeyNames="id" DataSourceID="_sdsTable1" DefaultMode="Insert">
<InsertItemTemplate>
<asp:TextBox ID="_txtField0" runat="server" Text='<%#Bind("field0")%>'/>
<asp:DropDownList ID="_ddlField1" runat="server" DataSourceID="_sdsTable2" DataTextField="field_text" DataValueField="field_value" AppendDataBoundItems="true">
<asp:ListItem Value="" Text="None" Selected="True" />
</asp:DropDownList>
</InsertItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="_txtField0" runat="server" Text='<%#Bind("field0")%>'/>
<asp:DropDownList ID="_ddlField1" CssClass="input_medium" runat="server" DataSourceID="_sdsTable2" DataTextField="field_text" DataValueField="field_value" AppendDataBoundItems="true">
<asp:ListItem Value="" Text="None" Selected="True" />
</asp:DropDownList>
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="_sdsTable1" runat="server" ConnectionString="<%$ ConnectionStrings:_db %>"
ProviderName="<%$ ConnectionStrings:_db.ProviderName %>"
SelectCommand=" SELECT
field0, field1
FROM
table1
WHERE
id=@id;"
UpdateCommand=" UPDATE
table1
SET
`field0` = @field0
`field1` = @field1
WHERE
id = @id;""
InsertCommandType="StoredProcedure"
InsertCommand="create_table1_entry">
<InsertParameters>
<asp:ControlParameter Name="field1" ControlID="_frmData$_ddlField1" Type="String" PropertyName="SelectedItem.Text" />
</InsertParameters>
<UpdateParameters>
<asp:ControlParameter Name="field1" ControlID="_frmData$_ddlField1" Type="String" PropertyName="SelectedItem.Text" />
</UpdateParameters>
</asp:SqlDataSource>

<asp:SqlDataSource ID="_sdsTable2" runat="server" ConnectionString="<%$ ConnectionStrings:_db %>"
ProviderName="<%$ ConnectionStrings:_db.ProviderName %>"
SelectCommand=" SELECT
field_value, field_text
FROM
table2;" />

最佳答案

Try This

myDropDown.Items.FindByValue("Your Main Record Value").Selected=true;

myDropDown.Items.FindByText("Your Main Record Text").Selected=true;

关于c# - Asp.net 更新 DropDownList 选中项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31166289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com