首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SqlDatasource选择参数

SqlDatasource选择参数
EN

Stack Overflow用户
提问于 2011-10-25 02:35:58
回答 2查看 10.7K关注 0票数 1

以下是我的sql数据源的详细信息

代码语言:javascript
复制
  <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>

在dsMoodleQuiz上附加了一个名为gvDetails的网格视图。在button click上,我想

要填充的网格视图。

这是点击按钮的代码

代码语言:javascript
复制
 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();
}

为什么这个不起作用...??我有没有遗漏任何代码...??感谢你的帮助

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-25 04:06:43

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

网格视图绑定.....

代码语言:javascript
复制
     <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数据源是这样的.

代码语言:javascript
复制
   <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>

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

代码语言:javascript
复制
    <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" />

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

代码语言:javascript
复制
    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样式。

这是上面网格视图的图像

代码语言:javascript
复制
    <style type="text/css">
   .highlight {text-decoration: none;color:black;background:yellow;}
</style>

我希望它能帮助你。

票数 1
EN

Stack Overflow用户

发布于 2011-10-26 09:20:11

在databaindg中使用以下代码

代码语言:javascript
复制
dsMoodleQuiz.SelectParmeter['FirstName'].DefaultValue = 'John';
dsMoodleQuiz.SelectParmeter['LastName'].DefaultValue = 'Wald';

gridView1.DataBind();

注:我没有visual studio与我在一起,所以代码不是复制,粘贴。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7880175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档