Click here to Skip to main content
14,924,198 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi my team,


i have gridview with dropdown list,and text boxes.and one button.if i select the one dropdown it will populate other dropdown list.and i added a new row by using the button.but am getting lost by previous row data during postback:-(.

then am getting data througn "Enum" not from database.please give a solution.



Thanks in advance to all...........
Posted
Comments
ZurdoDev 20-Nov-15 8:59am
   
Solution for what? And how?
Suvendu Shekhar Giri 20-Nov-15 11:50am
   
If you are binding the dropdown in the page load event then make sure that it wrapped inside the !IsPostback statement.

Sharing the relevant code would help us understanding your actual issue.
Nanthinee 25-Nov-15 1:50am
   
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList ddlType = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("ddlType");

DropDownList ddlCategory = (DropDownList)GridView1.Rows[rowIndex].Cells[2].FindControl("ddlCategory"); TextBox txtDescription = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("txtDescription");
DropDownList ddlDedicatedORshared = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("ddlDedicatedORshared");
DropDownList ddlPhysicalLocation = (DropDownList)GridView1.Rows[rowIndex].Cells[5].FindControl("ddlPhysicalLocation");
DropDownList ddlSupportedLocation = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("ddlSupportedLocation");
DropDownList ddlDepartmentORclient = (DropDownList)GridView1.Rows[rowIndex].Cells[7].FindControl("ddlDepartmentORclient");
TextBox txtDepartment = (TextBox)GridView1.Rows[rowIndex].Cells[8].FindControl("txtDepartment");
TextBox txtBillpack = (TextBox)GridView1.Rows[rowIndex].Cells[9].FindControl("txtBillpack");
TextBox txtCurrency = (TextBox)GridView1.Rows[rowIndex].Cells[10].FindControl("txtCurrency");
TextBox txtCountry = (TextBox)GridView1.Rows[rowIndex].Cells[11].FindControl("txtCountry");
TextBox txtContactAmt = (TextBox)GridView1.Rows[rowIndex].Cells[12].FindControl("txtContactAmt");
TextBox txtInvoiceAmt = (TextBox)GridView1.Rows[rowIndex].Cells[13].FindControl("txtInvoiceAmt");
TextBox txtTax = (TextBox)GridView1.Rows[rowIndex].Cells[14].FindControl("txtTax");
TextBox txtVariance = (TextBox)GridView1.Rows[rowIndex].Cells[15].FindControl("txtVariance");
TextBox Txtdate1 = (TextBox)GridView1.Rows[rowIndex].Cells[16].FindControl("Txtdate1");
TextBox Txtdate2 = (TextBox)GridView1.Rows[rowIndex].Cells[17].FindControl("Txtdate2");
TextBox TextApprovedAmt = (TextBox)GridView1.Rows[rowIndex].Cells[18].FindControl("TextApprovedAmt");
TextBox TxtpaidAmt = (TextBox)GridView1.Rows[rowIndex].Cells[19].FindControl("TxtpaidAmt");
TextBox TxtApprovedVspaid = (TextBox)GridView1.Rows[rowIndex].Cells[20].FindControl("TxtApprovedVspaid");
DropDownList ddlPaymentMode = (DropDownList)GridView1.Rows[rowIndex].Cells[21].FindControl("ddlPaymentMode");
TextBox TxtPaymentDetails = (TextBox)GridView1.Rows[rowIndex].Cells[22].FindControl("TxtPaymentDetails");
ddlType.Text = dt.Rows[i]["Column1"].ToString();
ddlCategory.Text = dt.Rows[i]["Column2"].ToString();
txtDescription.Text = dt.Rows[i]["Column3"].ToString();
ddlDedicatedORshared.Text = dt.Rows[i]["Column4"].ToString();
ddlPhysicalLocation.Text = dt.Rows[i]["Column5"].ToString();
ddlSupportedLocation.Text = dt.Rows[i]["Column6"].ToString();
ddlDepartmentORclient.Text = dt.Rows[i]["Column7"].ToString();
txtDepartment.Text = dt.Rows[i]["Column8"].ToString();
txtBillpack.Text = dt.Rows[i]["Column9"].ToString();
txtCurrency.Text = dt.Rows[i]["Column10"].ToString();
Nanthinee 25-Nov-15 1:52am
   
i call dis method in (!ispostback) only....for text box the values are remain stable wen am adding a new row in gridview..but for dropdown i lost my selected dropdown list values.

1 solution

Hi ,

You can refer below code
You have to get selected value in session and set that value after postback to the dropdownlist.

e.g.

C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
 {
    Session["DDLValue"] = dropdownlist1.SelectedValue;    
 }
On page load, get the session and set it to drop down list if it is not NULL:

if(Session["DDLValue"] != null)
{
    dropdownlist1.SelectedValue = Session["DDLValue"].ToString();
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900