如何将多个SelectParameters添加到SqlDataSource?

时间:2014-02-24 16:23:05

标签: asp.net vb.net gridview sqldatasource

我一直在尝试向我的SqlDataSource添加两个SelectParameters,但没有用。

以下是我在aspx页面中的代码

<table>
        <tr>
            <td colspan="2"><strong>Search By child</strong>
            </td>
        </tr>
        <tr>
            <td>Case Number:
            </td>
            <td>
                <asp:TextBox ID="txtCaseNumber" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>Last Name:
            </td>
            <td>
                <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>First Name:
            </td>
            <td>
                <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:LinkButton ID="lnkSearchChild" runat="server">Search</asp:LinkButton>
            </td>
        </tr>
    </table>

<asp:GridView ID="childListGrid" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True" PageSize="20" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal">
        <Columns>
            <asp:BoundField DataField="child_recordId" HeaderText="Child ID" InsertVisible="False" ReadOnly="True" SortExpression="child_recordId" />
            <asp:BoundField DataField="child_caseNumber" HeaderText="Case Number" SortExpression="child_caseNumber" />
            <asp:BoundField DataField="child_LastName" HeaderText="Last Name" SortExpression="child_LastName" />
            <asp:BoundField DataField="child_FirstName" HeaderText="First Name" SortExpression="child_FirstName" />
            <asp:TemplateField HeaderText=" " InsertVisible="False"
                SortExpression="child_recordId">
                <ItemTemplate>
                    &nbsp;&nbsp;
                        <asp:LinkButton ID="lnkViewForms" runat="server" CommandName="ViewForms" CommandArgument='<%# Bind("child_recordId") %>' OnClick="lnkViewForms_Click">View forms</asp:LinkButton>
                    &nbsp;&nbsp;
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
        <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
        <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F7F7F7" />
        <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
        <SortedDescendingCellStyle BackColor="#E5E5E5" />
        <SortedDescendingHeaderStyle BackColor="#242121" />
    </asp:GridView>

<asp:SqlDataSource ID="SqlDataSource5" runat="server" ConnectionString="<%$ ConnectionStrings:eci_conn %>" SelectCommand="SELECT ... FROM ... WHERE [child_LastName] LIKE @lname + '%' AND [child_FirstName] LIKE @fname + '%' ORDER BY [child_LastName]">
        <SelectParameters>
            <asp:ControlParameter Name="lname" DbType="String" />
            <asp:ControlParameter Name="fname" DbType="String" />
        </SelectParameters>
    </asp:SqlDataSource>

在我的代码后面,我尝试了以下

    Dim lNameParam As New Parameter("@lname", DbType.String)
    Dim fNameParam As New Parameter("@fname", DbType.String)
    lNameParam.DefaultValue = txtLastName.Text.Trim()
    fNameParam.DefaultValue = txtFirstName.Text.Trim()
    SqlDataSource5.SelectParameters.Add(lNameParam)
    SqlDataSource5.SelectParameters.Add(fNameParam)
    childListGrid.DataSourceID = "SqlDataSource5"

还有这个

SqlDataSource5.SelectParameters.Add("@lname", txtLastName.Text.Trim())
SqlDataSource5.SelectParameters.Add("@fname", txtFirstName.Text.Trim())
childListGrid.DataSourceID = "SqlDataSource5"

和这个

SqlDataSource5.SelectParameters("lname").DefaultValue = txtLastName.Text.Trim()
SqlDataSource5.SelectParameters("fname").DefaultValue = txtFirstName.Text.Trim()
childListGrid.DataSourceID = "SqlDataSource5"

似乎什么都没有用,我还能尝试什么?

感谢

2 个答案:

答案 0 :(得分:1)

在向SqlDataSource添加参数时,您只需要执行一个或另一个(标记或代码隐藏)。看看这个:

<SelectParameters>
    <asp:ControlParameter Name="lname" ControlID="txtLastName" PropertyName="Text" />
    <asp:ControlParameter Name="fname" ControlID="txtFirstName" PropertyName="Text" />
</SelectParameters>

此时,两个ControlParameters都已添加到您的SqlDataSource中。没有必要在代码隐藏中再次添加它们。

请注意,我将“ControlID”和“PropertyName”属性添加到标记中。我不确定你是如何在没有它们的情况下得到的,但它们是参数正常工作所必需的。

由于您在回发期间更改了数据源,因此您需要确保在进行更改后调用数据源:

childListGrid.DataSourceID = "SqlDataSource5";
childListGrid.DataBind();

答案 1 :(得分:0)

试试这个

SqlDataSource5.SelectParameters("lname").DefaultValue = txtLastName.Text.Tostring
SqlDataSource5.SelectParameters("fname").DefaultValue = txtFirstName.Text.Tostring
childListGrid.DataSource = "SqlDataSource5"
childListGrid.Databind