SqlDatasource选择参数 [英] SqlDatasource select parameters

查看:157
本文介绍了SqlDatasource选择参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的sql数据源细节

 < asp:SqlDataSource ID =dsMoodleQuizrunat =server
ConnectionString =<%$ ConnectionStrings:OnlineMeetingConnectionString%>
ProviderName =<%$ ConnectionStrings:OnlineMeetingConnectionString.ProviderName%>

SelectCommand =选择姓名,用户名,等级,名字,姓氏,电子邮件,TimeModified,IDNumber从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();
}

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

解决方案

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



网格视图绑定.....

 < ; asp:GridView ID =Gridview1runat =serverAutoGenerateColumns =FalseAllowPaging =True
AllowSorting =trueDataSourceID =dsGridviewWidth =540pxPageSize =10>
<列>
< asp:BoundField DataField =idHeaderText =IDSortExpression =id/>
< asp:TemplateField HeaderText =First NameSortExpression =FirstName>
< ItemStyle Width =120pxHorizo​​ntalAlign =Left/>
< ItemTemplate>
< asp:Label ID =lblFirstnameText ='<%#HighlightText(Eval(FirstName))%>'
runat =server/>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =Last NameSortExpression =LastName>
< ItemStyle Width =120pxHorizo​​ntalAlign =Left/>
< ItemTemplate>
< asp:Label ID =lblLastnameText ='<%#HighlightText(Eval(LastName))%>'
runat =server/>
< / ItemTemplate>
< / asp:TemplateField>
< asp:BoundField DataField =DepartmentHeaderText =Department
SortExpression =DepartmentItemStyle-Width =130px/>
< asp:BoundField DataField =LocationHeaderText =Location
SortExpression =LocationItemStyle-Width =130px/>
< /列>
< / asp:GridView>

和sql数据源是这样的...

 < asp:SqlDataSource ID =SqlDataSource1runat =serverSelectCommand =SELECT * FROM People
ConnectionString =<%$ ConnectionStrings:ConnectionString% >中
FilterExpression =名字如'%{0}%'或姓氏如'%{1}%'>
< FilterParameters>
< asp:ControlParameter Name =firstnameControlID =txtSearchPropertyName =Text/>
< asp:ControlParameter Name =lastnameControlID =txtSearchPropertyName =Text/>
< / FilterParameters>
< / asp:SqlDataSource>

添加搜索文本框。

 < asp:TextBox ID =txtSearchrunat =server/> 
< asp:ImageButton ID =btnSearchImageUrl =images / searchbutton.pngrunat =server/>
< asp:ImageButton ID =btnClearImageUrl =images / clearbutton.pngrunat =server/>

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

 使用System.Text.RegularExpressions; 
部分;
class GridviewwithHighlightedSearch:System.Web.UI.Page {

//创建一个字符串来存储我们的搜索结果
private string SearchString =;

字符串HighlightText(字符串InputTxt){
//无论何时在我们的数据库的FirstName和LastName
字段中显示文本,都会调用此函数。 ((SearchString ==)){
return InputTxt;如果我们没有搜索,那么只需返回原始的
//输入,这会加速一些

}
else {
//否则,创建一个新的正则表达式并根据我们的搜索字符串评估FirstName和
// LastName字段。
Regex ResultStr;
ResultStr = new Regex(SearchString.Replace(,|),RegexOptions.IgnoreCase);
return ResultStr.Replace(InputTxt,new MatchEvaluator(new System.EventHandler(this.ReplaceWords)));



公共字符串ReplaceWords(Match m){
//这个匹配评估器返回找到的字符串并将它添加到我定义的CSS类
// as'highlight'
return(< span class = highlight>
+(m.ToString +< / span>));
}

protected void btnClear_Click(对象发件人,System.Web.UI.ImageClickEventArgs e){
//简单清理文本以将GridView返回到默认状态
txtSearch.Text =;
SearchString =;
Gridview1.DataBind();
}

protected void btnSearch_Click(object sender,System.Web.UI.ImageClickEventArgs e){
//设置SearchString的值,使其获得
SearchString = txtSearch.Text;


这是hilighting的css风格。



这是上图表示的图像

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

我希望它能帮助你...


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

解决方案

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...

这篇关于SqlDatasource选择参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆