ASP.NET无法在参数ControlParameter中找到对象

时间:2016-06-07 23:48:33

标签: c# asp.net gridview sqldatasource controlparameter

我试图从DropDownList获取值作为SQL查询的参数。我有一个GridView和SqlDataSource。而且我无法在GridView中访问DropDownList(" DDLName")。当" DDLName"在GridView之外,一切正常,但是当我把它放进去的时候,我得到一个错误,说明找不到" DDLName"。我想在.aspx中这样做。我已经看到很多关于使用" $的提示,但我试图使用" GridView1 $ DDLName"并且它不起作用。

有我的代码:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource2" AutoGenerateEditButton="True" ShowFooter="True">
        <Columns>
            <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
            <asp:BoundField DataField="firstname" HeaderText="firstname" SortExpression="firstname" />
            <asp:BoundField DataField="lastname" HeaderText="lastname" SortExpression="lastname" />
            <asp:TemplateField HeaderText="city">
                <ItemTemplate>
                    <asp:Label ID = "LabelName" runat="server" Text='<%# Eval("name") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID = "DDLName" runat="server" DataSourceID="DDLSource" DataTextField="name" ClientIDMode="Static"></asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    <asp:SqlDataSource ID="DDLSource" runat="server" ConnectionString="<%$ ConnectionStrings:MBase1ConnectionString %>" SelectCommand="SELECT [name] FROM [cities]">
    </asp:SqlDataSource>

    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MBase1ConnectionString %>" SelectCommand="users_select_all_city" SelectCommandType="StoredProcedure" UpdateCommand="users_update_city" UpdateCommandType="StoredProcedure" DeleteCommand="users_delete" DeleteCommandType="StoredProcedure" InsertCommand="users_add" InsertCommandType="StoredProcedure">
        <DeleteParameters>
            <asp:Parameter Name="id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="id" Type="Int32" />
            <asp:Parameter Name="firstName" Type="String" />
            <asp:Parameter Name="lastName" Type="String" />
            <asp:Parameter Name="name" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="id" Type="Int32" />
            <asp:Parameter Name="firstName" Type="String" />
            <asp:Parameter Name="lastName" Type="String" />
            <asp:ControlParameter ControlId="DDLName" Name="name" PropertyName="SelectedValue" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>

1 个答案:

答案 0 :(得分:0)

好问题。给我几个小时的时间来解决这个问题,但我做对了。你需要做两处修改。

1)将您的下拉更改为:

<asp:ControlParameter ControlId="DDLName" Name="name" PropertyName="SelectedValue" Type="String" />

2)替换此行:

<asp:Parameter Name="name" Type="String" />

有了这个:

// let's store the file name
$filename = 'counter.txt';

// create the file if it doesn't exist
if (!file_exists($filename)) {
    $counter_file = fopen($filename, "w");
    fwrite($counter_file, "0");
    $counter = 0
} else {
    $counter_file = fopen($filename, "r");

    // will read the first line
    $counter = fgets($counter_file)
}

// increase $counter
$counter++;

// echo counter
echo $counter

// save the increased counter
fwrite($counter_file, "0");

// close the file
fclose($counter_file);