无法检索在网格视图的第一个空行中输入的数据

时间:2011-03-24 06:23:26

标签: asp.net

我已将网格视图放在更新面板中,第一行为空,并且不与任何数据库绑定。相反,我已将数据表绑定到网格视图。每当我点击“添加新行”按钮时,就会在网格视图中创建一个新行,将数据字段保留在旧行中但我的问题是每当我点击保存按钮时,此时第一行的数据不会被重试但是我可以获得第二行及以后填写的数据。我已将数据表保存在会话中,并在每次回发后再次检索它。

这是我的.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;
using AppResumeMaster;
using System.Collections;
using AppQualificationDetail;


public partial class Applicant_ApplicationForm : System.Web.UI.Page
{
    int Rows = 1;
    object MaxAppId = 0;
    protected void Page_Load(object sender, EventArgs e)
    { 
        if (!IsPostBack)
        {
            setInitialRow();
        }   
    }
    protected void saveBtn_Click(object sender, EventArgs e)
    {

        try
        {
            using (DataOperation oDo = new DataOperation())
            {
                MaxAppId= oDo.ExecuteScaler("select max(AppId) from tblAppResumeMaster");
                if (MaxAppId == System.DBNull.Value)
                    MaxAppId = 0;
            }

            using (AppResumeMasterClass objAppResumeMasterClass = new AppResumeMasterClass())
            {
                objAppResumeMasterClass.AppId = Convert.ToInt32(MaxAppId)+1;
                objAppResumeMasterClass.AppFirstName = firstnameBox.Text;
                objAppResumeMasterClass.AppLastName = lastnameBox.Text;
                objAppResumeMasterClass.AppAddress1 = CurAddBox.Text;
                objAppResumeMasterClass.AppCity1 = CurAddBox.Text;
                objAppResumeMasterClass.AppState1 = CurStateBox.Text;
                objAppResumeMasterClass.AppCountry1 = CurCountrybox.Text;
                objAppResumeMasterClass.AppAddress2 = PerAddBox.Text;
                objAppResumeMasterClass.AppCity2 = PerCityBox.Text;
                objAppResumeMasterClass.AppState2 = PerStateBox.Text;
                objAppResumeMasterClass.AppCountry2 = PerCountrybox.Text;
                objAppResumeMasterClass.AppEmail1 = emailBox.Text;
                objAppResumeMasterClass.AppEmail2 = AltEmailBox.Text;
                objAppResumeMasterClass.AppMobileNo = mobileNoBox.Text;
                objAppResumeMasterClass.AppContactNo = phoneNoBox.Text;
                objAppResumeMasterClass.AppDOB = Convert.ToDateTime(birthDateBox.Text);
                objAppResumeMasterClass.AppKeySkill = skillsBox.Text;
                objAppResumeMasterClass.AppGoal = goalBox.Text;
                objAppResumeMasterClass.AppWeakness = weaknessBox.Text;
                objAppResumeMasterClass.AppStrengths = strengthsBox.Text;

                objAppResumeMasterClass.setVal(1);
            }

            DataTable Table = (DataTable)Session["CurTable"];
            int cnt = Table.Rows.Count;
            using(AppQualificationDetailClass objAppQualificationDetail = new AppQualificationDetailClass())
            {
                for (int RowCnt = 0; RowCnt < Table.Rows.Count; RowCnt++)
                {
                    objAppQualificationDetail.AppId = Convert.ToInt32(MaxAppId) + 1;
                    objAppQualificationDetail.QualiId = Convert.ToInt32(Table.Rows[RowCnt][1]);
                    objAppQualificationDetail.Year = Convert.ToInt32(Table.Rows[RowCnt][3]);
                    objAppQualificationDetail.Percentage = (float)Table.Rows[RowCnt][2];
                    objAppQualificationDetail.InstiName = Table.Rows[RowCnt][4].ToString();


                    objAppQualificationDetail.setVal(1);
                }

            }
             ExistMsgLbl.Text = Table.Rows[0][2].ToString();
        }

        catch (Exception ex)
        {

            throw ex;
        }   
    }
    protected void addRowBtn_Click(object sender, EventArgs e)
    {
        AddNewRow();
    }
    protected void InstituteLbl_PreRender(object sender, EventArgs e)
    {
        //Session.Add("Table", Table1);
    }
    public void setInitialRow()
    {
        DataTable Table = new DataTable();
        DataRow dr = null;
        Table.Columns.Add(new DataColumn("Qualification",typeof(string)));

        Table.Columns.Add(new DataColumn("QualiId",typeof(string)));

        Table.Columns.Add(new DataColumn("Percentage",typeof(string)));
        Table.Columns.Add(new DataColumn("Passing Year",typeof(string)));
        Table.Columns.Add(new DataColumn("Institute Name",typeof(string)));
        dr = Table.NewRow();

        dr["Percentage"] = string.Empty;
        dr["Passing Year"] = string.Empty;
        dr["Institute Name"]=string.Empty;

        Table.Rows.Add(dr);
        Session.Add("CurTable", Table);
        GridView1.DataSource = Table;
        GridView1.DataBind();


        ArrayList Array = new ArrayList();
        DataSet ds = new DataSet();
        DropDownList DDL = (DropDownList)GridView1.Rows[0].Cells[0].FindControl("QualificationList");
        FillDropDownList(DDL);
        }
    public void AddNewRow()
    {
        DataSet ds=new DataSet();

        int RowIndex = 0;
        if (Session["CurTable"] != null)
        {
            DataTable CurTable = (DataTable)Session["CurTable"];
            DataRow CurRow = null;
            if (CurTable.Rows.Count > 0)
            {
                CurRow = CurTable.NewRow();
                CurTable.Rows.Add(CurRow);
                Session.Add("CurTable", CurTable);
                for (int count = 0; count < CurTable.Rows.Count - 1; count++)
                {
                    DropDownList DDL = (DropDownList)GridView1.Rows[count].Cells[0].FindControl("QualificationList");
                    TextBox PercentageBox = (TextBox)GridView1.Rows[count].Cells[1].FindControl("percentageBox");
                    TextBox yearBox = (TextBox)GridView1.Rows[count].Cells[1].FindControl("yearBox");
                    TextBox InstituteNameBox = (TextBox)GridView1.Rows[count].Cells[1].FindControl("InstituteNameBox");

                    CurTable.Rows[count]["Percentage"] = PercentageBox.Text;
                    CurTable.Rows[count]["Passing Year"]=yearBox.Text;
                    CurTable.Rows[count]["Institute Name"]=InstituteNameBox.Text;
                    CurTable.Rows[count]["Qualification"]=DDL.SelectedItem.Text;
                    CurTable.Rows[count]["QualiId"] = DDL.SelectedValue;
                }

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

            }
        }
        setPreviousData();
    }
    public void setPreviousData()
    {
        int RowIndex = 0;
        if (Session["CurTable"] != null)
        {
            DataTable RestoreTable = (DataTable)Session["CurTable"];
            if (RestoreTable.Rows.Count > 0)
            {
                for (int row = 0; row < RestoreTable.Rows.Count; row++)
                {
                    DropDownList DPList = (DropDownList)GridView1.Rows[row].Cells[0].FindControl("QualificationList");
                    TextBox PercentageBox = (TextBox)GridView1.Rows[row].Cells[1].FindControl("percentageBox");
                    TextBox YearBox = (TextBox)GridView1.Rows[row].Cells[2].FindControl("yearBox");
                    TextBox InstituteName = (TextBox)GridView1.Rows[row].Cells[3].FindControl("InstituteNamebox");

                    FillDropDownList(DPList);

                    if (row < RestoreTable.Rows.Count - 1)
                    {
                        PercentageBox.Text = RestoreTable.Rows[row]["Percentage"].ToString();
                        YearBox.Text = RestoreTable.Rows[row]["Passing Year"].ToString();
                        InstituteName.Text = RestoreTable.Rows[row]["Institute Name"].ToString();

                        DPList.ClearSelection();
                        DPList.Items.FindByText(RestoreTable.Rows[row]["Qualification"].ToString()).Selected = true;
                    }
                    RowIndex++;
                }
            }
        }
    }
    private ArrayList FillArrayList()
    {
        ArrayList ArrayList = new ArrayList();
        DataSet ds = new DataSet();
          using(DataOperation oDo = new DataOperation())
          {
            ds =oDo.DropDownList("select * from tblQualificationMaster");
            for(int count=0;count<ds.Tables[0].Rows.Count;count++)
            {
                ArrayList.Add(new ListItem(ds.Tables[0].Rows[count][1].ToString(), ds.Tables[0].Rows[count][0].ToString()));
            }
         }
          return ArrayList;
    }
    private void FillDropDownList(DropDownList DDL)
    {
        ArrayList ArrayList = FillArrayList();
        foreach (ListItem item in ArrayList)
        {
            DDL.Items.Add(item);
        }

    }
}

1 个答案:

答案 0 :(得分:1)

使用ViewState而不是Session。