部分回发后文本框的数据丢失

时间:2011-03-18 10:59:52

标签: asp.net ajax

我使用了更新面板,在该更新面板中,我保留了ASP Table控件,我在其中动态创建行,而这些行又包含1个下拉列表和3个文本框。我的问题是,在找到部分回发文本框和下拉列表后,文本框的文本属性显示为空,下拉列表的选定值设置为该框中的第一个记录。我还在会话中存储了整个表,并在page_load事件中以(!isPostBack)条件检索它。请指导我如何解决这个问题?

我的.aspx页面的一部分如下

<td colspan="2">
               <asp:ScriptManager ID="ScriptManager1" runat="server">
                </asp:ScriptManager>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server" EnableViewState="true">
                <ContentTemplate>  
                  <table class="style1">
                   <tr>
                     <td>
                        <table class="style1" border="1">
                        <tr>
                           <td width="120">
                                <asp:Label ID="QualificationLbl" runat="server" Text="Qualification"></asp:Label>                                           </td>
                                            <td  width="100">
                                                <asp:Label ID="PercentageLbl" runat="server" Text="Percentage"></asp:Label>
                                            </td>
                                            <td width="100">
                                                <asp:Label ID="YearLbl" runat="server" Text="Passing Year"></asp:Label>
                                            </td>
                                            <td width="*">
                                                <asp:Label ID="InstituteLbl" runat="server" Text="Institute Name" Width="120px" 
                                                    onprerender="InstituteLbl_PreRender"></asp:Label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="4" align="left">
                                                <asp:PlaceHolder ID="PlaceHolder" runat="server"></asp:PlaceHolder>
                                                <asp:Table ID="Table1" runat="server">
                                                </asp:Table>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">
                                    <asp:Button ID="addRowBtn" runat="server" onclick="addRowBtn_Click" 
                                        Text="Add New Row" />
                                </td>
                            </tr>
                        </table>
                  </ContentTemplate>
                    <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="addRowBtn" EventName="Click" />
                    </Triggers>

            </asp:UpdatePanel>
            <br />
    </td>

和我的.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 AppQualificationDetail;


public partial class Applicant_ApplicationForm : System.Web.UI.Page
{
    int Rows = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
        generateTable(Rows);
    }
    else
    {
        Table ReturnedTable = new Table();
        ReturnedTable = (Table)Session["Table"];
        int rowCount = ReturnedTable.Rows.Count;
        int colCount = 4;

        DropDownList objList = new DropDownList();
        TextBox txtBox = new TextBox();
        if (ReturnedTable != null)
        {
            for (int Rcount = 0; Rcount < rowCount; Rcount++)
            {
                for (int Ccount = 0; Ccount < colCount; Ccount++)
                {
                    if (Ccount == 0)
                    {
                        objList = (DropDownList)ReturnedTable.Rows[Rcount].Cells[Ccount].FindControl("DropDownList(" + Rcount + "," + Ccount + ")");
                        objList.SelectedValue = ((DropDownList)ReturnedTable.Rows[Rcount].Cells[Ccount].FindControl("DropDownList(" + Rcount + "," + Ccount + ")")).SelectedValue;
                    }
                    else
                    {
                        txtBox = (TextBox)ReturnedTable.Rows[Rcount].Cells[Ccount].FindControl("TextBox(" + Rcount + "," + Ccount + ")");
                        txtBox.Text = ((TextBox)ReturnedTable.Rows[Rcount].Cells[Ccount].FindControl("TextBox(" + Rcount + "," + Ccount + ")")).Text;
                        //txtBox.Text = Request.Params["TextBox(" + Rcount + "," + Ccount + ")"];
                    }
                }
            }

        }
    }

}
protected void saveBtn_Click(object sender, EventArgs e)
{
}
protected void addRowBtn_Click(object sender, EventArgs e)
{

    if (ViewState["rowCount"] != null)
    {
        Rows = Convert.ToInt32(ViewState["rowCount"].ToString());
        generateTable(Rows);
    }

}
public void generateTable(int rowCount)
{
    try
    {
        int colCount = 4;
        DropDownList objDropDownList;
        for (int Row = 0; Row < rowCount; Row++)
        {
            TableRow objTablerow = new TableRow();

            for (int cols = 0; cols < colCount; cols++)
            {
                TableCell objTableCell = new TableCell();
                if (cols == 0)
                {
                    objDropDownList = new DropDownList();
                    objDropDownList.Width = 120;
                    objTableCell.Controls.Add(objDropDownList);

                    DataOperation objDataoperation = new DataOperation();
                    DataSet ds = new DataSet();
                    ds = objDataoperation.DropDownList("select * from tblQualificationMaster");

                    objDropDownList.DataValueField = ds.Tables[0].Columns[0].ToString();
                    objDropDownList.DataTextField = ds.Tables[0].Columns[1].ToString();
                    objDropDownList.DataSource = ds.Tables[0];
                    objDropDownList.DataBind();

                    objDropDownList.ID = "DropDownList(" + Row + "," + cols + ")";
                    objTableCell.Controls.Add(objDropDownList);
                    objTablerow.Controls.Add(objTableCell);
                }
                else
                {
                    TextBox objTextBox = new TextBox();
                    objTextBox.ID = "TextBox(" + Row + "," + cols + ")";
                    objTableCell.Controls.Add(objTextBox);
                    objTablerow.Controls.Add(objTableCell);
                }
            }

            Table1.Rows.Add(objTablerow);

        }
        rowCount++;
        ViewState["rowCount"] = rowCount;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
protected void InstituteLbl_PreRender(object sender, EventArgs e)
{
    Session.Add("Table", Table1);
}
}

我希望通过保留以前的行在每个addRowTbn点击事件上添加一行。

1 个答案:

答案 0 :(得分:0)

从我的第一次看,您试图从存储在会话中的表中提取下拉列表的选定值和文本框的文本值,但是在用户有机会对其进行更改之前,您将该表存储在会话中价值观。