使用webmethod绑定gridview的模板 [英] bind template filed of gridview using webmethod

查看:73
本文介绍了使用webmethod绑定gridview的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net gridview。我正在使用webmethod填充gridview。我能够正确填充boundfield。但我无法填充itemtemplate图像按钮。如何在gridview中绑定Imagebutton。





我的代码是



I have a asp.net gridview. I am populating the gridview using webmethod. I am able to populate the boundfield properly. But I am not able to populated itemtemplate imagebutton. How to bind the Imagebutton in gridview.


My code is

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script src="ASPSnippets_Pager.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            GetCustomers(1);
        });
        $("[id*=txtSearch]").live("keyup", function () {
            GetCustomers(parseInt(1));
        });
        $(".Pager .page").live("click", function () {
            GetCustomers(parseInt($(this).attr('page')));
        });
        function SearchTerm() {
            return jQuery.trim($("[id*=txtSearch]").val());
        };
        function GetCustomers(pageIndex) {
            $.ajax({
                type: "POST",
                url: "CS.aspx/GetCustomers",
                data: '{searchTerm: "' + SearchTerm() + '", pageIndex: ' + pageIndex + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function (response) {
                    alert(response.d);
                },
                error: function (response) {
                    alert(response.d);
                }
            });
        }
        var row;
        function OnSuccess(response) {
            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            var customers = xml.find("Customers");
            if (row == null) {
                row = $("[id*=gvCustomers] tr:last-child").clone(true);
            }
            $("[id*=gvCustomers] tr").not($("[id*=gvCustomers] tr:first-child")).remove();
            if (customers.length > 0) {
                $.each(customers, function () {
                    var customer = $(this);
                    $("td", row).eq(0).html($(this).find("ContactName").text());
                    $("td", row).eq(1).html($(this).find("CustomerID").text());
                    $("td", row).eq(2).html($(this).find("image").text());
                    $("[id*=gvCustomers]").append(row);
                    row = $("[id*=gvCustomers] tr:last-child").clone(false);
                });
                var pager = xml.find("Pager");
                $(".Pager").ASPSnippets_Pager({
                    ActiveCssClass: "current",
                    PagerCssClass: "pager",
                    PageIndex: parseInt(pager.find("PageIndex").text()),
                    PageSize: parseInt(pager.find("PageSize").text()),
                    RecordCount: parseInt(pager.find("RecordCount").text())
                });

                $(".ContactName").each(function () {
                    var searchPattern = new RegExp('(' + SearchTerm() + ')', 'ig');
                    $(this).html($(this).text().replace(searchPattern, "<span class = 'highlight'>" + SearchTerm() + "</span>"));
                });
            } else {
                var empty_row = row.clone(true);
                $("td:first-child", empty_row).attr("colspan", $("td", row).length);
                $("td:first-child", empty_row).attr("align", "center");
                $("td:first-child", empty_row).html("No records found for the search criteria.");
                $("td", empty_row).not($("td:first-child", empty_row)).remove();
                $("[id*=gvCustomers]").append(empty_row);
            }
        };
    </script>
</head>
<body>
    <form id="form1" runat="server">
Search:
<asp:TextBox ID="txtSearch" runat="server" />
<hr />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField HeaderStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name"

            ItemStyle-CssClass="ContactName" />
        <asp:BoundField HeaderStyle-Width="150px" DataField="CustomerID" HeaderText="CustomerID" />

           <asp:TemplateField HeaderText="Image" ItemStyle-HorizontalAlign="center"  HeaderStyle-Height="30px" HeaderStyle-BackColor="Red">
            <ItemTemplate>
                <asp:Image ID="Image2" Height = "100" Width = "100" runat="server"

             ImageUrl='<%# ResolveUrl(Eval("image").ToString()) %>' />

            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>





c#code





private void BindDummyRow()

{

DataTable dummy = new DataTable();

dummy.Columns.Add(CustomerID);

dummy.Colu mns.Add(ContactName);

dummy.Columns.Add(image);

dummy.Rows.Add();

gvCustomers.DataSource = dummy;

gvCustomers.DataBind();

}



[WebMethod]

public static string GetCustomers(string searchTerm,int pageIndex)

{

string query =[GetCustomers_Pager];

SqlCommand cmd = new SqlCommand(query);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue(@ SearchTerm,searchTerm);

cmd.Parameters.AddWithValue(@ PageIndex,pageIndex);

cmd.Parameters.AddWithValue(@ PageSize,PageSize);

cmd.Parameters.Add(@ RecordCount,SqlDbType.Int,4).Direction = ParameterDirection.Output;

返回GetData(cmd,pageIndex).GetXml();

}



c# code


private void BindDummyRow()
{
DataTable dummy = new DataTable();
dummy.Columns.Add("CustomerID");
dummy.Columns.Add("ContactName");
dummy.Columns.Add("image");
dummy.Rows.Add();
gvCustomers.DataSource = dummy;
gvCustomers.DataBind();
}

[WebMethod]
public static string GetCustomers(string searchTerm, int pageIndex)
{
string query = "[GetCustomers_Pager]";
SqlCommand cmd = new SqlCommand(query);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@SearchTerm", searchTerm);
cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
cmd.Parameters.AddWithValue("@PageSize", PageSize);
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
return GetData(cmd, pageIndex).GetXml();
}

推荐答案

( function(){
GetCustomers(1);
});
(function () { GetCustomers(1); });


([id * = txtSearch])。live(keyup,function(){
GetCustomers(parseInt(1)) ;
});
("[id*=txtSearch]").live("keyup", function () { GetCustomers(parseInt(1)); });


(。Pager .page)。live(click,function(){
GetCustomers(parseInt(
(".Pager .page").live("click", function () { GetCustomers(parseInt(


这篇关于使用webmethod绑定gridview的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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