【问题标题】:Asp.net update DropDownList selected itemAsp.net 更新 DropDownList 选中项
【发布时间】:2015-07-01 15:59:13
【问题描述】:

在表单中使用 DropDownList 项时遇到问题。 主表单由 SQLDataSource 提供...所有表单小部件都绑定到特定的记录字段等。 我用 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;" />

【问题讨论】:

  • 请贴一些代码。
  • 请分享一些代码sn-p
  • 光环@JamieRees。我想尽可能避免代码隐藏。但是当然如果没有其他方法我会使用它......
  • 如果我能在 DDL 中使用 SelectedValue='&lt;%#Bind("field1")%&gt;' 属性就好了

标签: c# asp.net


【解决方案1】:
Try This

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

或

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

【讨论】:

  • 真正的问题不是如何选择下拉列表中的值,而是如何将所选项目绑定到提供整个表单的记录的字段值。 IE。如果我更改我正在查看的记录(移动到表格中),所有文本框提要“绑定”到记录字段都会更新......下拉列表不是因为它只是绑定到它的数据源而不是绑定到记录它的目的。将来自 DDL 的值放入 SQLDataSource 中的 条目是一项简单的任务,但在编辑模式下,这将不起作用,因为 DDL 未绑定。
  • 无论如何.. 你建议如何放置这段代码?我的意思是在哪个事件处理程序中?
【解决方案2】:

正如我猜测的那样,我可以在 DDL 中使用 SelectedValue='' 属性来实现结果。这里唯一的问题是为什么 Intellisense 不建议 SelectedValue 作为 DropDownList 的属性...谢谢大家。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多