Gridview SqlDatasource选择参数

Gridview SqlDatasource选择参数,gridview,sqldatasource,sqlparameter,Gridview,Sqldatasource,Sqlparameter,以下是我的sql数据源详细信息 <asp:SqlDataSource ID="dsMoodleQuiz" runat="server" ConnectionString="<%$ ConnectionStrings:OnlineMeetingConnectionString %>" ProviderName="<%$ ConnectionStrings:OnlineMeetingConnectionString.ProviderName %>

以下是我的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>

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

这是一个关于如何使用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;
    }
}
使用System.Text.regular表达式;
部分的
使用HighlightedSearch:System.Web.UI.Page对GridView进行分类{
//创建一个字符串来存储搜索结果
私有字符串SearchString=“”;
字符串高亮文本(字符串输入文本){
//只要文本显示在FirstName和LastName中,就会调用此函数
//数据库中的字段。如果我们不搜索,则只返回原始字段
//输入,这会加快速度
如果((SearchString==“”){
返回输入文本;
}
否则{
//否则,创建一个新的正则表达式并计算FirstName和
//针对搜索字符串的LastName字段。
Regex ResultStr;
ResultStr=newregex(SearchString.Replace(“,“|”)、RegexOptions.IgnoreCase);
返回ResultStr.Replace(inputxt,newmatchEvaluator(newsystem.EventHandler(this.ReplaceWords));
}
}
公共字符串替换字(匹配m){
//此匹配计算器返回找到的字符串,并将其添加到我定义的CSS类中
//作为“亮点”
返回(“”)
+(m.ToString+);
}
受保护的无效btnClear\u单击(对象发送者,System.Web.UI.ImageClickEventArgs e){
//简单清理文本,将Gridview返回到默认状态
txtSearch.Text=“”;
SearchString=“”;
Gridview1.DataBind();
}
受保护的无效BTN搜索\单击(对象发送者,System.Web.UI.ImageClickEventArgs e){
//设置SearchString的值,使其
SearchString=txtSearch.Text;
}
}
这是hilighting的css样式

这是上面网格视图的图像

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


.突出显示{文本装饰:无;颜色:黑色;背景:黄色;}

我希望它能帮助您……

这是一个示例,介绍如何使用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;
    }
}
使用System.Text.regular表达式;
部分的
使用HighlightedSearch:System.Web.UI.Page对GridView进行分类{
//创建一个字符串来存储搜索结果
私有字符串SearchString=“”;
字符串高亮文本(字符串输入文本){
//只要文本显示在FirstName和LastName中,就会调用此函数
//数据库中的字段。如果我们不搜索,则只返回原始字段
//输入,这会加快速度
如果((SearchString==“”){
返回输入文本;
}
否则{
//否则,创建一个新的正则表达式并计算FirstName和
//针对搜索字符串的LastName字段。
Regex ResultStr;
ResultStr=newregex(SearchString.Replace(“,“|”)、RegexOptions.IgnoreCase);
返回ResultStr.Replace(inputxt,newmatchEvaluator(newsystem.EventHandler(this.ReplaceWords));
}
}
公共字符串替换字(匹配m){
//此匹配计算器返回找到的字符串,并将其添加到我定义的CSS类中
//作为“亮点”
返回(“”)
+(m.ToString+);
}
受保护的无效btnClear\u单击(对象发送者,System.Web.UI.ImageClickEventArgs e){
//简单清理文本,将Gridview返回到默认状态
txtSearch.Text=“”;
SearchString=“”;
Gridview1.DataBind();
}
受保护的无效BTN搜索\单击(对象发送者,System.Web.UI.ImageClickEventArgs e){
//设置SearchString的值,使其
SearchString=txtSearch.Text;
}
}
这是hilighting的css样式

这是上面网格视图的图像

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


.突出显示{文本装饰:无;颜色:黑色;背景:黄色;}

我希望它能帮助您……

在databaindg中使用以下代码

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

gridView1.DataBind();

注意:我没有带visual studio,因此代码不可复制、可复制。

在databaindg中使用以下代码

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

gridView1.DataBind();

注意:我没有visual studio,因此代码不可复制、可复制。

如何/在何处使用intern附加到gridview的新搜索数据填充SQLDatasource。。。???看到这一行,您可以使用过滤器表达式alsoHow/where用intern附加到gridview的新搜索数据填充SQLDatasource。。。???看到这一行,您也可以使用过滤器表达式