نمایش اطلاعات Gridview در Popup با استفاده از RowData Bound در ASP.Net
چهارشنبه 3 تیر 1394این مقاله چگونگی پرکردن اطلاعات در Grid View و چگونگی نمایش اطلاعات از Grid View در pop-up زمانی که کاربر روی دکمه کلیک می کند را نمایش می دهد .
مراحل اولیه
مرحله 1
Visual Studio 2010 را باز کنید و یک Empty Website ایجاد کنید ، یک نام مناسب انتخاب کنید (RowDataBoundPopUp_demo).
مرحله 2
در Solution Explorer یک empty website دارید سپس یک وبسایت به شرح زیر اضافه کنید :
در Web Form:
RowDataBoundPopUp _demo نام وبسایت خالی شما می باشد سپس روی نام وبسایت کلیک راست کرده و در گزینه Add New Item گزینه Web Form را انتخاب می کنیم و نام آن را به RowDataBoundPopUp _demo.aspx تغییر می دهیم .
در اینجا از SQL Server برای ایجاد جدول یا Stored Procedure استفاده نمی کنیم،جدول ها را در سمت سرور ایجاد می کنیم طوریکه می توانیم ردیف جدول را براحتی دریافت کنیم و این مقاله به RowDataBound اشاره دارد .
مرحله طراحی
مرحله 3
صفحه RowDataBoundPopUp_demo.aspx را بازکنید و کد هایی برای طراحی GridView بنویسید . به کد های زیر توجه کنید .
RowDataBoundPopUp_demo.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RowDataBoundPopUp _demo.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> .style1 { width: 240px; } </style> </head> <body> <form id="form1" runat="server"> <div> <table style="width:100%;"> <tr> <td> </td> <td class="style1"> </td> <td> </td> </tr> <tr> <td> </td> <td class="style1"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" DataKeyNames="id" GridLines="Horizontal" onrowcommand="GridView1_RowCommand"> <Columns> <asp:TemplateField HeaderText="Name"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Education"> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("education") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("education") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Location"> <EditItemTemplate> <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("location") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("location") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <EditItemTemplate> <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Button ID="Button1" runat="server" CommandName="Select" CommandArgument="<%# Container.DataItemIndex%>" Text="Click to PopUp Me" /> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="White" ForeColor="#333333" /> <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="White" ForeColor="#333333" /> <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#F7F7F7" /> <SortedAscendingHeaderStyle BackColor="#487575" /> <SortedDescendingCellStyle BackColor="#E5E5E5" /> <SortedDescendingHeaderStyle BackColor="#275353" /> </asp:GridView> </td> </tr> <tr> <td> </td> <td class="style1"> </td> <td> </td> </tr> </table> </div> </form> </body> </html>
GridView به این شکل خواهد بود :
مرحله کدنویسی
مرحله 4
فایل RowDataBoundPopUp_demo.aspx.cs را برای نوشتن برخی دستورات و تکمیل برنامه باز کنید .
RowDataBoundPopUp_demo.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { refreshdata(); } } public void refreshdata() { // for making Table DataTable dt = new DataTable(); // these are the table columns dt.Columns.Add("id", typeof(Int32)); dt.Columns.Add("name", typeof(string)); dt.Columns.Add("education", typeof(string)); dt.Columns.Add("location", typeof(string)); // these are the table rows with values dt.Rows.Add(1, "Nilesh", "B.E(IT)", "Rajkot"); dt.Rows.Add(2,"Purnima", "B.E(CSE)", "Rajkot"); dt.Rows.Add(2, "Chandni", "MSc(IT)", "Ahmedabad"); dt.Rows.Add(2, "Rinku", "MBA", "Pune"); dt.Rows.Add(2, "Nilu", "MBBS", "Bikaner"); GridView1.DataSource = dt; GridView1.DataBind(); } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName== "Select") { int Index =Convert.ToInt32(e.CommandArgument.ToString()); GridViewRow row = GridView1.Rows[Index]; string name = (row.FindControl("Label1") as Label).Text; string education = (row.FindControl("Label2") as Label).Text; string location= (row.FindControl("Label3") as Label).Text; ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Name:" + name + "\\nEducation:" + education +"\\nLocation:"+location+" ');", true); } } }
خروجی
زمانی که کاربر مرورگر را باز می کند اطلاعات در pop-up بصورت زیر نمایش داده می شود :
زمانی که روی دکمه کلیک می کند اطلاعات به این صورت نمایش داده می شوند :
- ASP.net
- 2k بازدید
- 4 تشکر