08 December 2016

ASP.Net AJAX 2

Örneğimizde DropDownList’e çekeceğimiz Kategori isimlerine tıklandığında Ürün bilgileri GridView’da AJAX tekniği kullanılarak listelenecek.

Kod kısmı:

SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
 protected void Page_Load(object sender, EventArgs e)
 {
 if (!Page.IsPostBack)
 {
 SqlCommand sqlcmd = new SqlCommand("select CategoryID, CategoryName from categories", sqlcon);
 sqlcon.Open();
 SqlDataReader sdr = sqlcmd.ExecuteReader();
while (sdr.Read())
 {
 ListItem li = new ListItem();
 li.Text=sdr[1].ToString();
 li.Value= sdr[0].ToString();
 ddlKategoriler.Items.Add(li);
 }
 ddlKategoriler.DataBind();
 sqlcon.Close();
 }
 }
protected void ddlKategoriler_SelectedIndexChanged(object sender, EventArgs e)
 {
 SqlCommand sqlcmd = new SqlCommand("select * from products where categoryId=@pid", sqlcon);
 sqlcmd.Parameters.AddWithValue("@pid", ddlKategoriler.SelectedValue);
 SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
 DataTable dt = new DataTable();
 da.Fill(dt);
 GridView1.DataSource = dt;
 GridView1.DataBind();
 }

 

Sayfamızda yer alan kontroller:

<asp:ScriptManager ID="ScriptManager1" runat="server">
 </asp:ScriptManager>
 <div>
 <asp:DropDownList ID="ddlKategoriler" runat="server" AutoPostBack="True"
 onselectedindexchanged="ddlKategoriler_SelectedIndexChanged">
 </asp:DropDownList><br />
 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
 <ContentTemplate>
 <asp:GridView ID="GridView1" runat="server">
 </asp:GridView>
 </ContentTemplate>
 <Triggers>
 <asp:AsyncPostBackTrigger ControlID="ddlKategoriler" />
 </Triggers>
 </asp:UpdatePanel>
 </div>