Member 12681079 Ответов: 1

Как включить сетку с редактированием и удалением


Я должен создать редактируемую сетку таким образом, чтобы при нажатии кнопки edit она перенаправлялась в текстовое поле, а при отправке значение сетки обновлялось.

Что я уже пробовал:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Final.aspx.cs" Inherits="WebApplication2.Final" %>

<!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> Task-2 </title>
    <script src="pikaday.js" type="text/javascript"></script>
    <link href="css/triangle.css" rel="stylesheet" type="text/css" />
    <link href="css/theme.css" rel="stylesheet" type="text/css" />
    <link href="css/site.css" rel="stylesheet" type="text/css" />
    <link href="css/pikaday.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .style1
        {
            width: 499px;
            height: 20px;
        }
        .style4
        {
            width: 499px;
            height: 517px;
        }
    </style>
</head>
<body>
    
    <form id="form1"  runat="server">
      
    <div>
        <table>
            <tr>
                <td class="style4">
                    <div id="div1">
                        <asp:Label ID="LblEmployeeId" runat="server" Text="Employee ID" Font-Bold="true">
                              
                        <asp:TextBox ID="txtemployeeid" MaxLength="20" onkeydown="return (!(event.keyCode>=65) && event.keyCode!=32);"

                            runat="server" Width="55px">
                          
                        <asp:RequiredFieldValidator ID="Rfv_empid" runat="server" ErrorMessage="Enter Id"

                            ControlToValidate="txtemployeeid">
                        <br />
                        <br />
                        <asp:Label ID="LblEmployeeName" runat="server" Text="Employee Name" Font-Bold="true">
                         <asp:TextBox ID="txtemployeename" MaxLength="50" onkeydown="return (!(event.keyCode<65)||(event.keyCode>122) && (event.keyCode>91)||(event.keyCode<96));"

                             runat="server" Width="108px">
                        <asp:RequiredFieldValidator ID="Rfv_empname" runat="server" ErrorMessage="Enter Name"

                            ControlToValidate="txtemployeename">
                        <br />
                        <br />
                        <br />
                        <asp:Label ID="LblDob" runat="server" Text="Date Of Birth" Font-Bold="true">
                            
                   
                        <asp:TextBox ID="txt_dob" TextMode="Date" runat="server">
                         <asp:RequiredFieldValidator ID="Rfv_dob" runat="server" ErrorMessage="Enter Date of Birth"

                            ControlToValidate="txt_dob" >
                        
                        <br />
                        <br />
                        <br />

                        Gender<br />
                        <br />
                        <asp:RadioButton ID="Rb_gender1" GroupName="Gender" Text="Male" Font-Bold="true"

                            runat="server" OnCheckedChanged="rb_gender" />
                        <asp:RadioButton ID="Rb_gender2" GroupName="Gender" Text="Female" Font-Bold="true"

                            runat="server" OnCheckedChanged="rb_gender" />
                        <asp:Label ID="lbl_Gender1" runat="server" Visible="False">
                        <br />
                        <br />
                        <asp:Label ID="lbladdress" runat="server" Text="Address" Font-Bold="true">
                        <br />
                        <br />
                        <asp:Label ID="lbldoorno" runat="server" Text="Door Number" Font-Bold="true">
                          <asp:TextBox ID="txtdoorno" MaxLength="50" runat="server" Width="29px">
                         <br />
                        <br />
                        <asp:Label ID="lblStreet"  runat="server" Text="Street" Font-Bold="true">
                                    
                        <asp:TextBox ID="txtstreet" MaxLength="50" runat="server" Width="97px">
                        <br />
                        <br />
                        <asp:Label ID="lblarea" runat="server" Text="Area" Font-Bold="true">
                                      
                        <asp:TextBox ID="txtarea" runat="server" MaxLength="50" Width="82px">
                        <br />
                        <br />
                        <asp:Label ID="lblcity" runat="server" Text="City" Font-Bold="true">
                                       
                        <asp:DropDownList ID="DropDownList1" runat="server">
                            <asp:ListItem Value="Select a city">Select a city 
                            <asp:ListItem Value="Chennai">Chennai 
                            <asp:ListItem Value="Madurai">Madurai
                            <asp:ListItem Value="Coimbatore">Coimbatore 
                            <asp:ListItem Value="Trichy">Trichy 
                            <asp:ListItem Value="Pondicherry">Pondicherry 
                        
                    </div>
                </td>
                <td class="style4">
                    <div id="div3">
                        <asp:Label ID="lbl_employeeid2" runat="server" Text="Employee ID" Font-Bold="true">
                             
                        <asp:TextBox ID="txt_employeeid2" MaxLength="20" onkeydown="return (!(event.keyCode>=65) && event.keyCode!=32);"

                            runat="server" Width="61px">
                          
                        <asp:RequiredFieldValidator ID="Rflempid2" runat="server" ErrorMessage="Enter Id"

                            ControlToValidate="txt_employeeid2">
                         <br />
                        <br />
                        <br />
                        <asp:Label ID="lbl_employeename2" runat="server" Text="Employee Name" Font-Bold="true">
                         <asp:TextBox ID="txt_employeename2" MaxLength="20" runat="server">
                        <asp:RequiredFieldValidator ID="Rfv_empname2" runat="server" ErrorMessage="Enter Employee Name"

                            ControlToValidate="txt_employeename2">
                        <br />
                        <br />
                        <br />
                        <asp:Label ID="lbl_dob2" runat="server" Text="Date Of Birth" Font-Bold="true">
                             
                        <asp:TextBox ID="txt_dob2" TextMode="Date" runat="server" Width="128px">
                        <asp:RequiredFieldValidator ID="Rfv_dob2" runat="server" ErrorMessage="Enter Date Of Birth"

                            ControlToValidate="txt_dob2">
                        <br />
                        <br />
                         <br />
                        Gender<br />
                        <br />
                        <asp:RadioButton ID="Rb_gender3" GroupName="Gender1" Text="Male" Font-Bold="true"

                            runat="server" OnCheckedChanged="rb_gender2" />
                        <asp:RadioButton ID="Rb_gender4" GroupName="Gender1" Text="Female" Font-Bold="true"

                            runat="server" OnCheckedChanged="rb_gender2" />
                        <asp:Label ID="lbl_gender2" runat="server">
                        <br />
                        <br />
                        <asp:Label ID="lbl_add2" runat="server" Text="Address" Font-Bold="true">
                        <br />
                        <br />
                        <asp:Label ID="lbl_doorno2" runat="server" Text="Door Number" Font-Bold="true">
                         
                        <asp:TextBox ID="txt_doorno2" MaxLength="50" runat="server" Width="33px">
                        <br />
                        <br />
                        <asp:Label ID="lbl_street2" runat="server" Text="Street" Font-Bold="true">
                                     
                        <asp:TextBox ID="txt_street2" MaxLength="50" runat="server" Width="93px">
                        <br />
                        <br />
                        <asp:Label ID="lbl_area2" runat="server" Text="Area" Font-Bold="true">
                                       
                        <asp:TextBox ID="txt_area2" MaxLength="20" runat="server" Width="87px">
                        <br />
                        <br />
                        <asp:Label ID="lblcity2" runat="server" Text="City" Font-Bold="true">
                                        
                        <asp:DropDownList ID="DropDownList_City2" runat="server">
                            <asp:ListItem Value="Select a city">Select a city 
                            <asp:ListItem Value="Chennai">Chennai 
                            <asp:ListItem Value="Madurai">Madurai
                            <asp:ListItem Value="Coimbatore">Coimbatore 
                            <asp:ListItem Value="Trichy">Trichy 
                            <asp:ListItem Value="Pondicherry">Pondicherry 
                        
                    </div>
                </td>
            </tr>
            <tr>
                <td class="style1">
                     
                </td>
                <td class="style1">
                     
                </td>
            </tr>
        </table>
        <div id="div2">
        </div>
        <asp:Button ID="Bt1_submit" runat="server" Text="Submit" OnClick="submitfunction" />
         <br />
        <br />
        <asp:GridView ID="GridView1" AutoGenerateEditButton="True" 

            AutoGenerateColumns="true" runat="server" Height="155px" Width="752px" Caption="Employee Details" 

            CaptionAlign="Top" ShowFooter="true" ShowHeader="true"  AutoGenerateDeleteButton="true" OnRowDeleting="GridView1_RowDeleting"

                  

            style="margin-left: 0px"> 
        
        <br />
        <br />
        <br />
        <asp:Button ID="Btn2_redirect" runat="server" CausesValidation="true" Text="Redirect" OnClick="redirect_OnClick" />
    </div>
    
    </form>
</body>
</html>

С ФОНОВЫМ КОДОМ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication2
{
    public partial class Final : System.Web.UI.Page
    {
        DataTable data1 = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            //data1 = (DataTable)Session["redirectfunction"];  
            DataColumn datacolumn0 = new DataColumn("Employee ID");
            DataColumn datacolumn1 = new DataColumn("Employee Name");
            DataColumn datacolumn2 = new DataColumn("Date Of Birth");
            DataColumn datacolumn3 = new DataColumn("Gender");
            DataColumn datacolumn4 = new DataColumn("Address");
            DataColumn datacolumn5 = new DataColumn("Options");
            data1.Columns.Add(datacolumn0);
            data1.Columns.Add(datacolumn1);
            data1.Columns.Add(datacolumn2);
            data1.Columns.Add(datacolumn3);
            data1.Columns.Add(datacolumn4);
            data1.Columns.Add(datacolumn5);
            DataRow datarow1 = data1.NewRow();
            DataRow datarow2 = data1.NewRow();
            GridView1.DataSource = data1;
            GridView1.DataBind();


        }


        //public void calender(object sender, EventArgs e)
        //{
        //    txt_dob.Text = Calendar1.SelectedDate.ToShortDateString();

        //}


        public void rb_gender(object sender, EventArgs e)
        {
            if (Rb_gender1.Checked)
            {
                lbl_Gender1.Text = Rb_gender1.Text;
            }
            else if (Rb_gender2.Checked)
            {
                lbl_Gender1.Text = Rb_gender2.Text;
            }
        }
        
        public void rb_gender2(object sender, EventArgs e)
        {
            {
                if (Rb_gender3.Checked)
                {
                    lbl_gender2.Text = Rb_gender3.Text;
                }
                else if (Rb_gender4.Checked)
                {
                    lbl_gender2.Text = Rb_gender4.Text;
                }
            }
        }
        public void foraddress(object sender, EventArgs e)
        {
            txtdoorno.Text = txtstreet.Text + txtarea.Text;
            txt_doorno2.Text = txt_street2.Text + txt_area2.Text;
        }


        public void submitfunction(object sender, EventArgs e)
        {
            DataRow datarow1 = data1.NewRow();
            datarow1[0] = txtemployeeid.Text;
            datarow1[1] = txtemployeename.Text;
            datarow1[2] = txt_dob.Text;
            datarow1[3] = lbl_Gender1.Text;
            datarow1[4] = txtdoorno.Text + txtstreet.Text + DropDownList1.Text;
            data1.Rows.Add(datarow1);
            GridView1.DataSource = data1;
            GridView1.DataBind();
            DataRow datarow2 = data1.NewRow();
            datarow2[0] = txt_employeeid2.Text;
            datarow2[1] = txt_employeename2.Text;
            datarow2[2] = txt_dob2.Text;
            datarow2[3] = lbl_gender2.Text;
            datarow2[4] = txt_doorno2.Text + txt_street2.Text + DropDownList_City2.Text;
            data1.Rows.Add(datarow2);

            GridView1.DataSource = data1;
            GridView1.DataBind();
            clear(sender,e);
         

        }

        public void clear(object sender,EventArgs e)
        {
            txtemployeeid.Text = "";
            txt_employeeid2.Text="";
            txtemployeename.Text="";
            txt_employeename2.Text="";
            txt_dob.Text="";
            txt_dob2.Text="";
            txtdoorno.Text="";
            txt_doorno2.Text="";
            txtstreet.Text="";
            txt_street2.Text="";
            txtarea.Text="";
            txt_area2.Text = "";
            DropDownList_City2.Text = "";
            
            
            
            
            }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            Label lbldeleteid = (Label)row.FindControl("lblID");
        }

        protected void redirect_OnClick(object sender, EventArgs e)
        {
            DataRow datarow1 = data1.NewRow();
            datarow1[0] = txtemployeeid.Text;
            datarow1[1] = txtemployeename.Text;
            datarow1[2] = txt_dob.Text;
            datarow1[3] = lbl_Gender1.Text;
            datarow1[4] = txtdoorno.Text + txtstreet.Text + DropDownList1.Text;
            data1.Rows.Add(datarow1);
            GridView1.DataSource = data1;
            GridView1.DataBind();
            DataRow datarow2 = data1.NewRow();
            datarow2[0] = txt_employeeid2.Text;
            datarow2[1] = txt_employeename2.Text;
            datarow2[2] = txt_dob2.Text;
            datarow2[3] = lbl_gender2.Text;
            datarow2[4] = txt_doorno2.Text + txt_street2.Text + DropDownList_City2.Text;
            data1.Rows.Add(datarow2);

            GridView1.DataSource = data1;
            GridView1.DataBind();

            Session["redirectfunction"] = data1;
            Response.Redirect("WebForm5.aspx");
        }
    }
}

ZurdoDev

Я бы посоветовал вам пройти несколько учебных пособий в интернете. Их тут целая куча.

Member 12681079

не могли бы вы предложить мне несколько ссылок?

Vincent Maverick Durano

просто погуглите "GridView Edit and Delete" - это даст вам массу результатов.

1 Ответов

Рейтинг:
11

Vincent Maverick Durano

Поскольку вы уже используете GridView, то почему бы не использовать встроенную функцию редактирования и удаления, которую он предлагает.

При использовании столбцов BoundField

[GridView Insert, Edit, Update and Delete – Адо.Чистый путь]

При использовании столбцов TemplateField

[Добавление строк в GridView с функциями редактирования, обновления и удаления]

Но, конечно, вы всегда используете комбинацию BoundFields и TemplateFields в зависимости от ваших требований.


Member 12681079

Большое спасибо:) это работает!