如何从WeMethod绑定SQLDatasource [英] How do I bind SQLDatasource from WeMethod

查看:133
本文介绍了如何从WeMethod绑定SQLDatasource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我的项目中的WebServis.asmx文件中有WebMethod。我也有asp.net页面,我有文本框,我输入Query的参数,将作为Gridview的数据源。

我想从WebMethod将数据源绑定到Gridview。

我可以这样做或者我需要使用SqlDataReader从我的Query中导出数据吗?



我需要这样的东西:



mkTabl.DataSource = SqlDataSource1;

mkTabl.DataBind();



Webmethod

Hello all . I have WebMethod in WebServis.asmx file in my project . Also i have asp.net page where i have textboxes where i input parameters for Query that will be datasource for Gridview .
I want to bind datasource to Gridview from WebMethod .
Can I make this or i need to use SqlDataReader to export data from my Query ?

I need something like this :

mkTabl.DataSource = SqlDataSource1;
mkTabl.DataBind();

Webmethod

[System.Web.Services.WebMethod]
public void CheckCat(string fam, string im , string ot )
{
  SqlDataSource SqlDataSource1 = new SqlDataSource();
  SqlDataSource1.ID = "SqlDataSource1";
  SqlDataSource1.ConnectionString =         System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  SqlDataSource1.SelectCommand = "SELECT id , org, code FROM Tabl  where (@fam like Tabl.FAM ) or  (@im like Tabl.IM) or ( @ot like Tabl.OT) " ;
  SqlDataSource1.SelectParameters.Add("@fam", fam);
  SqlDataSource1.SelectParameters.Add("@im", im);
  SqlDataSource1.SelectParameters.Add("@ot", ot);




<asp:TextBox ID="FamBox" runat="server" MaxLength="10" /> 
<asp:TextBox ID="ImBox" runat="server" MaxLength="10" /> 
<asp:TextBox ID="OtBox" runat="server" MaxLength="14" />
    <asp:GridView ID="mkTabl" Visible="true"  CssClass="ts1" runat="server" AutoGenerateColumns="False"  PageSize="5" AllowPaging="True" AutoPostBack="True"  >
 </asp:GridView>    

推荐答案

如果您需要,如下所示



mkTabl.DataSource = SqlDataSource1;

mkTabl.DataBind ();



将webmethod的返回类型从void更改为Dataset或datatable。



还执行查询后填写webmethod中的数据集或数据表。
if you need like below

mkTabl.DataSource = SqlDataSource1;
mkTabl.DataBind();

Change the return type of webmethod from void to Dataset or datatable.

And also after executing the query fill into dataset or datatable inside the webmethod.


你需要做的是在asp:gridview中使用asp:boundfiels将数据绑定到gridview
What u need to do is to use asp:boundfiels inside asp:gridview to bind data to gridview


这篇关于如何从WeMethod绑定SQLDatasource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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