开发者

Data of text box is lost after partial post back

I have used update panel and in that update panel I have kept ASP Table control in which I am creating rows dynamically which in turn contains 1 dropdown lists and 3 text boxes. My problem is that after partial postback text boxes and dropdown list are found but text property of text boxes is showing empty and dropdown list's selected value is set to the first record in that box. I have also stored entire table in session and retrieve it in(!isPostBack) condition at page_load event. Please guide me how to resolve this problem?

part of my .aspx page is as below

<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">&开发者_运维知识库lt;/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>

and My .aspx.cs page is as below

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);
}
}

I want to add one row on every addRowTbn click event there by retaining previous rows.


From my first look through, you are trying to pull drop down's selected value and the text box's text value from the table you stored in session, but you stored that table in session prior to the user having the opportunity to make changes to the values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜