FormView中的ASP.NET DropDownList

时间:2014-10-24 22:16:17

标签: asp.net drop-down-menu webforms

一直致力于ASP.NET表单,并与一个特定部分进行斗争。我正在使用带有Insert函数的SqlDataSource,该函数指向存储过程,带有一些输入参数。我有一个FormView设置。我希望表单的一个输入字段是一个DropDownList,其选项从不同的SqlDataSource填充。作为参考,这是formview:

<asp:FormView runat="server" id="FormView1" DataSourceID="SqlDataSource1" DefaultMode="Insert">
    <EditItemTemplate>
        refund_ach:
        <asp:CheckBox Checked='<%# Bind("ach") %>' runat="server" id="refund_achCheckBox" />
        <br />
        refund_dba_name:
        <asp:TextBox Text='<%# Bind("dbaname") %>' runat="server" id="refund_dba_nameTextBox" />
        <br />
        refund_mid:
        <asp:TextBox Text='<%# Bind("mid") %>' runat="server" id="refund_midTextBox" />
        <br />
        refund_description:
        <asp:TextBox Text='<%# Bind("description") %>' runat="server" id="refund_descriptionTextBox" />
        <br />
        refund_amount:
        <asp:TextBox Text='<%# Bind("amount") %>' runat="server" id="refund_amountTextBox" />
        <br />
        refund_type:
        <asp:TextBox Text='<%# Bind("type") %>' runat="server" id="refund_typeTextBox" />
        <br />
        <asp:LinkButton runat="server" Text="Update" CommandName="Update" id="UpdateButton" CausesValidation="True" />&nbsp;
        <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" id="UpdateCancelButton" CausesValidation="False" />
    </EditItemTemplate>
    <InsertItemTemplate>
        <asp:Table runat="server" id="Table1">
            <asp:TableRow>
                <asp:TableCell>ACH?:
                </asp:TableCell>
                <asp:TableCell><asp:CheckBox Checked='<%# Bind("ach") %>' runat="server" id="refund_achCheckBox" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>DBA Name:
                </asp:TableCell>
                <asp:TableCell><asp:TextBox Text='<%# Bind("dbaname") %>' runat="server" id="refund_dba_nameTextBox" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>MID:
                </asp:TableCell>
                <asp:TableCell><asp:TextBox Text='<%# Bind("mid") %>' runat="server" id="refund_midTextBox" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>Description:
                </asp:TableCell>
                <asp:TableCell><asp:TextBox Text='<%# Bind("description") %>' runat="server" id="refund_descriptionTextBox" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>Amount:
                </asp:TableCell>
                <asp:TableCell><asp:TextBox Text='<%# Bind("amount") %>' runat="server" id="refund_amountTextBox" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>Type:
                </asp:TableCell>
                <asp:TableCell><asp:DropDownList DataSourceID="SqlDataSource2" SelectedValue='<%# Bind("type") %>' DataTextField="Reason" DataValueField="ID" runat="server" id="refund_typeTextBox" />
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
                    <asp:LinkButton runat="server" Text="Insert" CommandName="Insert" id="InsertButton" CausesValidation="True" />&nbsp;
        <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" id="InsertCancelButton" CausesValidation="False" />
    </InsertItemTemplate>
    <ItemTemplate>
        refund_ach:
        <asp:CheckBox Checked='<%# Bind("ach") %>' runat="server" id="refund_achCheckBox" Enabled="false" />
        <br />
        refund_dba_name:
        <asp:Label Text='<%# Bind("dbaname") %>' runat="server" id="refund_dba_nameLabel" />
        <br />
        refund_mid:
        <asp:Label Text='<%# Bind("mid") %>' runat="server" id="refund_midLabel" />
        <br />
        refund_description:
        <asp:Label Text='<%# Bind("description") %>' runat="server" id="refund_descriptionLabel" />
        <br />
        refund_amount:
        <asp:Label Text='<%# Bind("amount") %>' runat="server" id="refund_amountLabel" />
        <br />
        refund_type:
        <asp:Label Text='<%# Bind("type") %>' runat="server" id="refund_typeLabel" />
        <br />
    </ItemTemplate>
</asp:FormView>

这是SqlDataSource:

    <asp:SqlDataSource runat="server" id="SqlDataSource1" ProviderName="System.Data.SqlClient" ConnectionString="Data Source=myserver;Initial Catalog=mydatabase;Persist Security Info=True;User ID=mysvcaccount;Password=NOPE!;" SelectCommand="SELECT [refund_ach], [refund_dba_name], [refund_mid], [refund_description], [refund_amount], [refund_type] FROM [refund]" InsertCommand="EXEC usp_refund_insert @user, @ach, @dbaname, @mid, @description, @amount, @type">
    <InsertParameters>
        <asp:formparameter FormField="refund_achCheckBox" Name="ach" Type="Boolean" />
        <asp:formparameter FormField="refund_amountTextBox" Name="amount" Type="Decimal" />
        <asp:formparameter FormField="refund_dba_nameTextBox" Name="dbaname" Type="String" />
        <asp:formparameter FormField="refund_descriptionTextBox" Name="description" Type="String" />
        <asp:formparameter FormField="refund_midTextBox" Name="mid" Type="String" />
        <asp:formparameter FormField="refund_typeTextBox" Name="type" Type="Int32" />
        <asp:parameter DefaultValue="dfsdf" Name="user" Type="String" />
    </InsertParameters>
</asp:SqlDataSource>

数据在DDL中填充得很好(来自另一个SqlDataSource)。所有期望的选项都出现了。但是在提交表单时,我收到了错误消息。我删除了DDL并将值硬编码为该参数,并且执行正常。我花了最后几个小时在互联网上寻找解决方案,找到吨,尝试它们,没有任何运气。我是一名DBA,所以.NET不是我的强项,但看起来似乎不应该让DDL像这样工作。

1 个答案:

答案 0 :(得分:1)

WOW。在非常沮丧之后,我决定从头开始再给它一次,以确保我没有错过任何东西。我完全建造了所有相同的东西,减少了所有的桌子元素......它完美无缺。我添加了表格元素,停止了工作。再次删除它们:工作。

不知道为什么,也许一个熟悉.NET的人可以解释原因,但它确实解决了我的问题。