【问题标题】:Seeking Advice: Updating a FormView Based on DropdownList Value寻求建议:根据 DropdownList 值更新 FormView
【发布时间】:2009-01-27 23:16:23
【问题描述】:

您好!

我正在寻找一些关于在 FormView 中显示数据的方法的建议,该方法基于 FormView 控件中的 DropDownList 选择。例如,我有一个具有以下内容的 UserControl:

<asp:XmlDataSource ID="xdsMyXmlData" runat="server" EnableCaching="false" XPath="Root/Membership" />
<asp:FormView ID="fvwMyFormView" runat="server" DataSourceID="xdsMyXmlData">
    <ItemTemplate>
        <div>
            <h2><%# XPath("Title") %></h2>

            <fieldset>
                <asp:DropDownList ID="ddlMemberTypes" runat="server" DataSource='<%# XPathSelect("MenuItems/*") %>'></asp:DropDownList>
            </fieldset>
            <table>
                <thead>
                    <tr>
                        <th><%# XPath("Columns/Name") %></th>
                        <th><%# XPath("Columns/Age") %></th>
                        <th><%# XPath("Columns/DateJoined")%></th>
                    </tr>
                </thead>
                <tbody>
                    <asp:Repeater ID="rptMembershipInfo" runat="server" DataSource='<%# XPathSelect("Members/*") %>'>
                        <ItemTemplate>
                            <tr>
                                <th><%# XPath("Data/Name") %></th>
                                <td><%# XPath("Data/Age") %></td>
                                <td><%# XPath("Data/DateJoined") %></td>
                            </tr>
                        </ItemTemplate>
                    </asp:Repeater>
                </tbody>
            </table>
        </div>    
    </ItemTemplate>
</asp:FormView>        

到目前为止,UserControl 的 OnLoad() 看起来像这样:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    string l_XmlData = MyControllerClass.GetMembershipTableXml(0);
    xdsMyXmlData.Data = l_XmlData;
}

我希望能够将 DropDownList 的选定项的值传递到 GetMembershipTableXml() 中,以便检索相应的 XML,然后使用它来填充 FormView 的值。最好的方法是什么?使用选定的 DropDownList 值作为查询字符串变量执行 Response.Redirect 回到当前页面?我希望有更好的方法。你怎么看?

【问题讨论】:

    标签: asp.net data-binding formview xmldatasource


    【解决方案1】:

    您可以在 DropDownList 上为 OnSelectedItemChanged 创建一个事件;发生这种情况时,您可以抓取所选项目,并调用 GetMembershipTableXml 函数。

    最后,不要忘记在 FormView 控件上调用 DataBind 来更新值:)

    认为这就是您所追求的,希望对您有所帮助!

    【讨论】:

    • 我想我需要将当前在 OnLoad 中的代码包装在“if (!IsPostBack)”块中?
    • 嗯,是的,你可能也需要这样做,好点!
    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 2021-11-18
    相关资源
    最近更新 更多