Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi i have :
page.cs :
C#
public class GetName
{
    public string Name { get; set; }
    public string Family { get; set; }
}


C#
[WebMethod]
public List<GetName> LoadName()
{
    SqlConnection connect = objcon.conect();
    List<GetName> GetNameInfoList = new List<GetName>();
    DataSet ds;
    using (SqlCommand cmd = new SqlCommand("SELECT * FROM TblNames ", connect))
    {
        connect.Open();
        cmd.Connection = connect;
        cmd.CommandType = CommandType.Text;
        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
        {

            ds = new DataSet();
            da.Fill(ds);
        }
    }
    try
    {
        if (ds != null)
        {
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        GetNameInfoList.Add(new GetName()
                        {
                            Name = dr["name"].ToString(),
                            Family= dr["family"].ToString()
                        });
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return GetNameInfoList;
}



page aspx:
XML
<script>
    function BindGridView() {
        $.ajax({
            type: "POST",
            url: "Default.aspx/GetNames",
            data: "{}",
            contentType: "application/json",
            dataType: "json",
            success: function (data) {
                for (var i = 0; i < data.d.length; i++) {
                    //How can bind to grid??????????????????????
                }
            }
        })
    }
</script>


XML
<asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%#Bind("Name") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%#Bind("Family") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>



now i how can bind data to Grid?????????????????
Posted

ASP.NET
<asp:GridView ID="grdDemo" runat="server" AutoGenerateColumns="false" ShowHeader="false">
    <Columns>
        <asp:TemplateField HeaderText="Name" >
            <ItemTemplate>
                <div>
                    <div>
                        name:
                    </div>
                    <div>
                        <asp:TextBox ID="Name" runat="server" Text='<%#Bind("Name") %>' style="width:200px;height:100px;"></asp:TextBox>
                    </div>
                </div>
                <div>
                    <div>
                        Family :
                    </div>
                    <div>
                        <asp:TextBox ID="Family" runat="server" Text='<%#Bind("Family") %>'></asp:TextBox>
                    </div>
                </div>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
Share this answer
 
Comments
nimaaamin 18-Nov-14 3:24am    
plz help me!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
check Bind data to GridView with jQuery or JSON in ASP.Net[^]

sample code:
C#
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindDummyItem();
    }

    private void BindDummyItem()
    {

        DataTable dtGetData = new DataTable();
        dtGetData.Columns.Add("Family");
        dtGetData.Columns.Add("Name");
        dtGetData.Rows.Add();

        grdDemo.DataSource = dtGetData;
        grdDemo.DataBind();
    }
    [WebMethod]
    public static List<GetName> LoadName()
    {
        return new List<GetName>(){new GetName(){ Family ="aaa", Name ="bbbb"},
        new GetName(){ Family ="ccc", Name ="ddd"}};
    }
}

public class GetName
{
    public string Name { get; set; }
    public string Family { get; set; }
}

ASPX
XML
<head runat="server">
    <script src="Scripts/jquery-2.1.1.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            BindGridView();
        });
        function BindGridView() {
            $.ajax({
                type: "POST",
                url: "WebForm1.aspx/LoadName",
                contentType: "application/json;charset=utf-8",
                data: {},
                dataType: "json",
                success: function (data) {
                    if (data.d.length > 0) {
                        var row = $("[id*=grdDemo] tr:last-child").clone(true);
                        $("[id*=grdDemo] tr").not($("[id*=grdDemo] tr:first-child")).remove();
                        for (var i = 0; i < data.d.length; i++) {
                            alert(data.d[i].Name);
                            $("td", row).eq(0).html(data.d[i].Name);
                            $("td", row).eq(1).html(data.d[i].Family);
                            $("#grdDemo").append(row);
                            row = $("[id*=grdDemo] tr:last-child").clone(true);
                        }
                    }
                },
                error: function (xhr, status, error) {
                    alert(xhr.responseText);
                }
            });
        }
    </script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="grdDemo" runat="server" AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField HeaderText="Name" >
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%#Bind("Name") %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Family">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server" Text='<%#Bind("Family") %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
 
Share this answer
 
v2
Comments
nimaaamin 18-Nov-14 0:33am    
i want grid have a TemplateField
DamithSL 18-Nov-14 1:36am    
you need to do some work based on the resources available, check my updated answer.
nimaaamin 18-Nov-14 2:07am    
not working
<asp:GridView ID="grdDemo" runat="server" AutoGenerateColumns="false" ShowHeader="false">
<columns>
<asp:TemplateField HeaderText="Name" >
<itemtemplate>
<div>
<div>
name:
</div>
<div>
<asp:TextBox ID="Name" runat="server" Text='<%#Bind("Name") %>' style="width:200px;height:100px;">
</div>
</div>
<div>
<div>
Family :
</div>
<div>
<asp:TextBox ID="Family" runat="server" Text='<%#Bind("Family") %>'>
</div>
</div>



This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900