[英]Asp.net update DropDownList selected item
I'm stuck into a problem using a DropDownList item into a form.我遇到了在表单中使用 DropDownList 项目的问题。 The main form is feed by an SQLDataSource ... all form widget are bound to a specific record field etc. I changed one of the form widget (a TextBox) with a DropDownList to allow the user to only select between a number of values... I feed this control trough another SQLDataSource.
主表单由 SQLDataSource 提供……所有表单小部件都绑定到特定的记录字段等。我使用 DropDownList 更改了一个表单小部件(一个 TextBox),以允许用户仅在多个值之间进行选择。 .. 我通过另一个 SQLDataSource 提供此控件。 While the form is in Insert mode there are no problems but when I switch into Edit mode I'm not able to set the DropDownList selected item with the value coming from the main record.
虽然表单处于插入模式,但没有问题,但是当我切换到编辑模式时,我无法使用来自主记录的值设置 DropDownList 所选项目。 Ho can I fix this?
我能解决这个问题吗? Here's a sample of my .aspx code:
这是我的 .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;
OR或者
myDropDown.Items.FindByText("Your Main Record Text").Selected=true;
I CAN JUST use SelectedValue='<%#Bind("field1")%>' property in DDL to achieve the result, just as I was guessing.正如我猜测的那样,我可以在 DDL 中使用 SelectedValue='<%#Bind("field1")%>' 属性来实现结果。 The only issue here is the why Intellisense is not suggesting SelectedValue as a propery for DropDownList ... thnx everyone.
这里唯一的问题是为什么 Intellisense 不建议 SelectedValue 作为 DropDownList 的属性......谢谢大家。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.