Thursday, May 29, 2014

Working With __doPostBack Jquery Function In The Asp.Net C#

Introduction

In this article we are going to learn the topic of how to post back the dropdown selected change without changing the property of Autopostback=”true” .

For this we are going to call the method at the time of onchange event OnChange="dropDownChange();"

In this method we write the call back method with parameter condition like
__doPostBack('pnlGrid', strUser)

Coding

Write this code

In Aspx Source File

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">





    <title></title>
    <style type="text/css">
        .style1
        {
            width100%;
        }
    </style>
    <script type="text/javascript">
        function dropDownChange() {
            var e = document.getElementById("<%=DropDownList1.ClientID%>");
            var strUser = e.options[e.selectedIndex].value;
            __doPostBack('pnlGrid', strUser)
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:UpdatePanel ID="asdasd" runat="server">
            <ContentTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" OnChange="dropDownChange();">
                </asp:DropDownList>
                <asp:Panel ID="pnlGrid" runat="server" OnLoad="pnlGrid_Load">
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                        <Columns>
                            <asp:BoundField DataField="UserId" />
                            <asp:BoundField DataField="UserName" />
                            <asp:BoundField DataField="Education" />
                        </Columns>
                    </asp:GridView>
                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>


Write this code in the .CS file



using System;
using System.Data;
using System.Web.UI.WebControls;

namespace SampleTestingCodes
{
    public partial class BindTheGridFromDropDownWithOutPostBack : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList1.Items.Add(new ListItem("One""1"));
                DropDownList1.Items.Add(new ListItem("Two""2"));
                DropDownList1.Items.Add(new ListItem("Three""3"));
                DropDownList1.Items.Add(new ListItem("Four""4"));
                DropDownList1.Items.Add(new ListItem("Five""5"));
            }
        }

        protected void pnlGrid_Load(object sender, EventArgs e)
        {
            string parameter = Request["__EVENTARGUMENT"];
            if (!string.IsNullOrEmpty(parameter))
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("UserId"typeof(int));
                dt.Columns.Add("UserName"typeof(string));
                dt.Columns.Add("Education"typeof(string));
                dt.Rows.Add(1, "Lokesh""B.Tech");
                dt.Rows.Add(2, "Vijay""Msc");
                dt.Rows.Add(3, "Balaji""MS");
                dt.Rows.Add(4, "Charan""B.Tech");
                dt.Rows.Add(5, "Durga""MD");
                dt.Rows.Add(6, "Shiva""B.Tech");
                dt.Rows.Add(7, "Venkat""CA");

                var results = (from myRow in dt
                                  .AsEnumerable()
                               where myRow.Field<int>("UserId") == Convert.ToInt16(parameter)
                               select myRow).CopyToDataTable();

                GridView1.DataSource = results;
                GridView1.DataBind();
            }
        }
    }
}




No comments:

Post a Comment