SqlDatasource选择参数

发布于 2024-12-11 12:48:26 字数 1139 浏览 0 评论 0原文

这是我的 sql 数据源详细信息

  <asp:SqlDataSource ID="dsMoodleQuiz" runat="server" 
    ConnectionString="<%$ ConnectionStrings:OnlineMeetingConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:OnlineMeetingConnectionString.ProviderName %>" 

    SelectCommand="SELECT Name, UserID, Grade, FirstName, LastName, Email, TimeModified, IDNumber FROM tbMoodleQuiz WHERE (FirstName = @FirstName) AND (LastName = @LastName)" 
    onselecting="dsMoodleQuiz_Selecting">
    <SelectParameters>
        <asp:Parameter Name="FirstName" />
        <asp:Parameter Name="LastName" />
    </SelectParameters>
</asp:SqlDataSource>

名为 gvDetails 的 gridview 附加到 dsMoodleQuiz 。在按钮单击上,我希望

填充gridview。

这是按钮单击的代码

 protected void btnSearch_Click(object sender, EventArgs e)
 {
    dsMoodleQuiz.SelectParameters.Add("@FirstName", System.Data.DbType.String, "Jhon");
    dsMoodleQuiz.SelectParameters.Add("@LastName", System.Data.DbType.String, "Wald");

    GridView1.DataBind();
}

为什么这不起作用......?我是否缺少任何代码...?感谢您的帮助

Here is my sql datasource details

  <asp:SqlDataSource ID="dsMoodleQuiz" runat="server" 
    ConnectionString="<%$ ConnectionStrings:OnlineMeetingConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:OnlineMeetingConnectionString.ProviderName %>" 

    SelectCommand="SELECT Name, UserID, Grade, FirstName, LastName, Email, TimeModified, IDNumber FROM tbMoodleQuiz WHERE (FirstName = @FirstName) AND (LastName = @LastName)" 
    onselecting="dsMoodleQuiz_Selecting">
    <SelectParameters>
        <asp:Parameter Name="FirstName" />
        <asp:Parameter Name="LastName" />
    </SelectParameters>
</asp:SqlDataSource>

A gridview by name gvDetails is attached to dsMoodleQuiz . On button click I would like

gridview to get populated.

Here is the code on button click

 protected void btnSearch_Click(object sender, EventArgs e)
 {
    dsMoodleQuiz.SelectParameters.Add("@FirstName", System.Data.DbType.String, "Jhon");
    dsMoodleQuiz.SelectParameters.Add("@LastName", System.Data.DbType.String, "Wald");

    GridView1.DataBind();
}

Why is this not working ...?? Am I missing any code ...?? Appreciate the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2024-12-18 12:48:26

这是关于如何使用 sqldatasource 搜索网格视图并根据该搜索条件填充结果的示例示例...

网格视图绑定...

     <asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" AllowPaging="True"
AllowSorting="true" DataSourceID="dsGridview" Width="540px" PageSize="10">
<Columns>
    <asp:BoundField DataField="id" HeaderText="ID" SortExpression="id" />
    <asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
        <ItemStyle Width="120px" HorizontalAlign="Left" />
        <ItemTemplate>
            <asp:Label ID="lblFirstname" Text='<%# HighlightText(Eval("FirstName")) %>' 
                runat="server" />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
        <ItemStyle Width="120px" HorizontalAlign="Left" />
        <ItemTemplate>
            <asp:Label ID="lblLastname" Text='<%# HighlightText(Eval("LastName")) %>' 
            runat="server" />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Department" HeaderText="Department" 
        SortExpression="Department" ItemStyle-Width="130px" />
    <asp:BoundField DataField="Location" HeaderText="Location" 
        SortExpression="Location" ItemStyle-Width="130px" />
</Columns>
</asp:GridView>

并且 sql 数据源就像这样...

   <asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT * FROM People"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    FilterExpression="firstname like '%{0}%' or lastname like '%{1}%'">
    <FilterParameters>
        <asp:ControlParameter Name="firstname" ControlID="txtSearch" PropertyName="Text" />
        <asp:ControlParameter Name="lastname" ControlID="txtSearch" PropertyName="Text" />
    </FilterParameters>
  </asp:SqlDataSource>

添加用于搜索的文本框..

    <asp:TextBox ID="txtSearch" runat="server" />
<asp:ImageButton ID="btnSearch" ImageUrl="images/searchbutton.png" runat="server" />
<asp:ImageButton ID="btnClear" ImageUrl="images/clearbutton.png" runat="server" />

这是用于绑定和在文本框中输入文本的代码

    using System.Text.RegularExpressions;
Partial;
class GridviewwithHighlightedSearch : System.Web.UI.Page {

    //  Create a String to store our search results
    private string SearchString = "";

    string HighlightText(string InputTxt) {
        //  This function is called whenever text is displayed in the FirstName and LastName 
        //  fields from our database. If we're not searching then just return the original 
        //  input, this speeds things up a bit
        if ((SearchString == "")) {
            return InputTxt;
        }
        else {
            //  Otherwise create a new regular expression and evaluate the FirstName and 
            //  LastName fields against our search string.
            Regex ResultStr;
            ResultStr = new Regex(SearchString.Replace(" ", "|"), RegexOptions.IgnoreCase);
            return ResultStr.Replace(InputTxt, new MatchEvaluator(new System.EventHandler(this.ReplaceWords)));
        }
    }

    public string ReplaceWords(Match m) {
        //  This match evaluator returns the found string and adds it a CSS class I defined 
        //  as 'highlight'
        return ("<span class=highlight>" 
                    + (m.ToString + "</span>"));
    }

    protected void btnClear_Click(object sender, System.Web.UI.ImageClickEventArgs e) {
        //  Simple clean up text to return the Gridview to it's default state
        txtSearch.Text = "";
        SearchString = "";
        Gridview1.DataBind();
    }

    protected void btnSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e) {
        //  Set the value of the SearchString so it gets 
        SearchString = txtSearch.Text;
    }
}

,这是用于高亮显示的 css 样式..

这是上面网格视图的图像

    <style type="text/css">
   .highlight {text-decoration: none;color:black;background:yellow;}
</style>

希望对您有帮助...

This is sample example on how to search grid view and populating results on that search criteria using sqldatasource.....

grid view binding .....

     <asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" AllowPaging="True"
AllowSorting="true" DataSourceID="dsGridview" Width="540px" PageSize="10">
<Columns>
    <asp:BoundField DataField="id" HeaderText="ID" SortExpression="id" />
    <asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
        <ItemStyle Width="120px" HorizontalAlign="Left" />
        <ItemTemplate>
            <asp:Label ID="lblFirstname" Text='<%# HighlightText(Eval("FirstName")) %>' 
                runat="server" />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
        <ItemStyle Width="120px" HorizontalAlign="Left" />
        <ItemTemplate>
            <asp:Label ID="lblLastname" Text='<%# HighlightText(Eval("LastName")) %>' 
            runat="server" />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Department" HeaderText="Department" 
        SortExpression="Department" ItemStyle-Width="130px" />
    <asp:BoundField DataField="Location" HeaderText="Location" 
        SortExpression="Location" ItemStyle-Width="130px" />
</Columns>
</asp:GridView>

and sql datasource is like this ...

   <asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT * FROM People"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    FilterExpression="firstname like '%{0}%' or lastname like '%{1}%'">
    <FilterParameters>
        <asp:ControlParameter Name="firstname" ControlID="txtSearch" PropertyName="Text" />
        <asp:ControlParameter Name="lastname" ControlID="txtSearch" PropertyName="Text" />
    </FilterParameters>
  </asp:SqlDataSource>

adding text box for searching ..

    <asp:TextBox ID="txtSearch" runat="server" />
<asp:ImageButton ID="btnSearch" ImageUrl="images/searchbutton.png" runat="server" />
<asp:ImageButton ID="btnClear" ImageUrl="images/clearbutton.png" runat="server" />

and this is code for binding and entering text into text box

    using System.Text.RegularExpressions;
Partial;
class GridviewwithHighlightedSearch : System.Web.UI.Page {

    //  Create a String to store our search results
    private string SearchString = "";

    string HighlightText(string InputTxt) {
        //  This function is called whenever text is displayed in the FirstName and LastName 
        //  fields from our database. If we're not searching then just return the original 
        //  input, this speeds things up a bit
        if ((SearchString == "")) {
            return InputTxt;
        }
        else {
            //  Otherwise create a new regular expression and evaluate the FirstName and 
            //  LastName fields against our search string.
            Regex ResultStr;
            ResultStr = new Regex(SearchString.Replace(" ", "|"), RegexOptions.IgnoreCase);
            return ResultStr.Replace(InputTxt, new MatchEvaluator(new System.EventHandler(this.ReplaceWords)));
        }
    }

    public string ReplaceWords(Match m) {
        //  This match evaluator returns the found string and adds it a CSS class I defined 
        //  as 'highlight'
        return ("<span class=highlight>" 
                    + (m.ToString + "</span>"));
    }

    protected void btnClear_Click(object sender, System.Web.UI.ImageClickEventArgs e) {
        //  Simple clean up text to return the Gridview to it's default state
        txtSearch.Text = "";
        SearchString = "";
        Gridview1.DataBind();
    }

    protected void btnSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e) {
        //  Set the value of the SearchString so it gets 
        SearchString = txtSearch.Text;
    }
}

and this is the css style for hilighting..

and this is teh image for the above grid view

    <style type="text/css">
   .highlight {text-decoration: none;color:black;background:yellow;}
</style>

i hope it will helps you...

我三岁 2024-12-18 12:48:26

在databandg中使用以下代码

dsMoodleQuiz.SelectParmeter['FirstName'].DefaultValue = 'John';
dsMoodleQuiz.SelectParmeter['LastName'].DefaultValue = 'Wald';

gridView1.DataBind();

注意:我没有Visual Studio,所以代码不可复制,可粘贴。

Using the following code in databaindg

dsMoodleQuiz.SelectParmeter['FirstName'].DefaultValue = 'John';
dsMoodleQuiz.SelectParmeter['LastName'].DefaultValue = 'Wald';

gridView1.DataBind();

Note: I dont have visual studio with me, so the code is not copy, pastable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文