I have this code in my program. When I am clicking on addmore button the textboxes are adding in the panel but while I am clicking on the reset button then the added textboxes are not clearing. Clicking for 2nd time on the reset button then the textboxes are clearing so what is the problem for that? Please give me the solution for that problem.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Generic;
public partial class UpdateProduct : System.Web.UI.Page
{
static int TextBoxCount = 0;
static int count = 0;
static int count1 = 0;
private List<TextBox> textboxes;
private List<TextBox> LoadTextboxes;
public string connString = ConfigurationManager.ConnectionStrings["connString"].ToString();
CommonFunctions common = new CommonFunctions();
static int catid, subcatid;
static string prodId;
protected override void Render(HtmlTextWriter writer)
{
Page.ClientScript.RegisterForEventValidation(btnTest.UniqueID);
base.Render(writer);
}
protected void Page_InIt(System.Object sender, System.EventArgs e)
{
checkSession();
}
private void checkSession()
{
if (Session["option"] == null)
{
Session["option"] = "Company";
}
if (Session["userid"] == null)
{
Response.Redirect("login.aspx?option=" + Request.Cookies["option"].Value);
}
}
protected void Page_Load(object sender, EventArgs e)
{
imgNext.Attributes.Add("onclick","javascript:return validate()");
PreRender += new EventHandler(_Default_PreRender);
textboxes = new List<TextBox>();
LoadTextboxes = new List<TextBox>();
if (IsPostBack)
{
for (int i = 0; i < count; i++)
{
TextBox tb = new TextBox();
tb.CssClass = "newinput";
tb.ID = "tb" + i;
tb.Width = 450;
panel1.Controls.Add(tb);
textboxes.Add(tb);
tb.Text = Request.Form[tb.ClientID];
}
for (int i = 0;i<count1; i++)
{
TextBox tBox = new TextBox();
tBox.CssClass = "newinput";
tBox.ID = "tBox" + i;
tBox.Width = 450;
tBox.Text = Request.Form[tBox.ClientID];
panelLoad.Controls.Add(tBox);
LoadTextboxes.Add(tBox);
}
}
if (!Page.IsPostBack)
{
textboxes.Clear();
count = int.Parse(textboxes.Count.ToString());
LoadingPage();
}
}
public void LoadingPage()
{
pnlAddProduct.Visible = true;
panelPreview.Visible = false;
string idp = Request.QueryString["prodid"].ToString();
string test1 = idp;
if (idp.Contains(" "))
{
test1 = idp.Replace(' ', '+');
}
prodId = Cryptography.Decrypt(test1);
SqlDataReader dr = common.ExecuteReader("select cat_id,subcat_id,prod_name,prod_type,prod_code,tag_names,prod_details,bestFeatures from products where prod_id=" + int.Parse(prodId));
if (dr.Read())
{
string test = dr["subcat_id"].ToString();
txtProdName.Text = dr["prod_name"].ToString();
txtProdType.Text = dr["prod_type"].ToString();
txtProdCode.Text = dr["prod_code"].ToString();
txtTagName.Text = dr["tag_names"].ToString();
txtProdDetails.Text = dr["prod_details"].ToString();
panelLoad.Controls.Clear();
if ((dr["bestFeatures"].ToString() != "")||(dr["bestFeatures"].ToString() != null))
{
string features;
if (lblBestFeatures.Text != "")
{
features = lblBestFeatures.Text;
}
else
{
features = dr["bestFeatures"].ToString();
}
if (features.Contains(","))
{
string[] arr = features.Split(',');
TextBoxCount = arr.Length;
txtBestFeature.Text = arr[0].ToString();
for (int i = 1; i < arr.Length; i++)
{
int count = LoadTextboxes.Count;
TextBox tBox = new TextBox();
tBox.CssClass = "newinput";
tBox.ID = "tBox" + LoadTextboxes.Count;
tBox.Width = 450;
tBox.Text = arr[i].ToString();
panelLoad.Controls.Add(tBox);
LoadTextboxes.Add(tBox);
}
}
else
{
txtBestFeature.Text = features;
}
}
DataSet ds = new DataSet();
ds = common.ExecuteDataset("select cat_id,cat_name from categories");
if (ds.Tables[0].Rows.Count > 0)
{
ddCategory.DataSource = ds;
ddCategory.DataValueField = "cat_id";
ddCategory.DataTextField = "cat_name";
ddCategory.DataBind();
}
if(Convert.ToString((ddCategory.Items.FindByValue(dr["cat_id"].ToString()))) != "" )
{
ddCategory.Items.FindByValue(dr["cat_id"].ToString()).Selected = true;
}
if (ddCategory.SelectedItem.Text == "Other")
{
ddSubCategory.Enabled = false;
txtCat.Visible = true;
txtSubCategory.Visible = true;
SqlDataReader dReader = common.ExecuteReader("select cat_name,subcat_name from userAddedCategories where prod_id=" + int.Parse(prodId));
if (dReader.Read())
{
txtCat.Text = dReader["cat_name"].ToString();
txtSubCategory.Text = dReader["subcat_name"].ToString();
}
}
else
{
ddSubCategory.Enabled = true;
txtCat.Visible = false;
txtSubCategory.Visible = false;
ddSubCategory.Items.Clear();
DataSet ds1 = new DataSet();
ds1 = common.ExecuteDataset("select subcat_id,subcat_name from subcategories where cat_id=" + int.Parse(dr["cat_id"].ToString()));
if (ds1.Tables[0].Rows.Count > 0)
{
ddSubCategory.DataSource = ds1;
ddSubCategory.DataValueField = "subcat_id";
ddSubCategory.DataTextField = "subcat_name";
ddSubCategory.DataBind();
}
if(Convert.ToString(ddSubCategory.Items.FindByValue(dr["subcat_id"].ToString())) != "" )
{
ddSubCategory.Items.FindByValue(dr["subcat_id"].ToString()).Selected = true;
}
}
}
}
protected void btnTest_Click(object sender, EventArgs e)
{
SqlDataReader dr = common.ExecuteReader("select prod_id from products where prod_name='" + txtProdName.Text + "'");
if (dr.Read())
{
panelLbl.Visible = true;
lblError.Text = "Entered Product Exists With Us.";
hlinkView.Visible = true;
hlinkView.NavigateUrl = "ProductDetails.aspx?prodid=" +Cryptography.Encrypt(dr["prod_id"].ToString()) + "&option=" + Request.Cookies["option"].Value;
}
}
protected void imgNext_Click(object sender, EventArgs e)
{
lblProdCode.Text = txtProdCode.Text;
lblProdName.Text = txtProdName.Text;
lblProdType.Text = txtProdType.Text;
lblProdDetails.Text = txtProdDetails.Text;
if (ddCategory.SelectedItem.Text == "Other")
{
lblSubCat.Text = txtSubCategory.Text;
lblCat.Text = txtCat.Text;
}
else
{
lblSubCat.Text = ddSubCategory.SelectedItem.Text;
lblCat.Text = ddCategory.SelectedItem.Text;
}
lblTagNames.Text = txtTagName.Text;
string bestFeatures = "";
if (txtBestFeature.Text != "")
{
bestFeatures = txtBestFeature.Text + ",";
}
for (int i = 0; i < LoadTextboxes.Count; i++)
{
if (LoadTextboxes[i].Text != "")
{
bestFeatures = bestFeatures + LoadTextboxes[i].Text + ",";
}
}
for (int i = 0; i < textboxes.Count; i++)
{
if (textboxes[i].Text != "")
{
bestFeatures = bestFeatures + textboxes[i].Text + ",";
}
}
bestFeatures = bestFeatures.Remove(bestFeatures.Length - 1, 1);
lblBestFeatures.Text = bestFeatures;
pnlAddProduct.Visible = false;
panelPreview.Visible = true;
}
protected void imgBtnSubmit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connString);
con.Open();
if (Request.QueryString["prodid"] != null)
{
catid=int.Parse(ddCategory.SelectedValue.ToString());
if (ddCategory.SelectedItem.Text == "Other")
{
}
else
{
subcatid = int.Parse(ddSubCategory.SelectedValue.ToString());
}
string insQuery = "update products set cat_id=@cat_id,subcat_id=@subcat_id,prod_name=@prod_name,prod_code=@prod_code,prod_type=@prod_type,prod_details=@prod_details,tag_names=@tag_names,updated_date=@updated_date,bestFeatures=@bestFeatures where prod_id=@prod_id";
SqlCommand cmd = new SqlCommand(insQuery, con);
cmd.Parameters.Add(new SqlParameter("@cat_id",catid ));
cmd.Parameters.Add(new SqlParameter("@subcat_id", subcatid));
cmd.Parameters.Add(new SqlParameter("@prod_name", txtProdName.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@prod_code", txtProdCode.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@prod_type", txtProdType.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@prod_details", txtProdDetails.Text));
cmd.Parameters.Add(new SqlParameter("@tag_names", txtTagName.Text));
cmd.Parameters.Add(new SqlParameter("@updated_date", DateTime.Now.ToString()));
cmd.Parameters.Add(new SqlParameter("@bestFeatures", lblBestFeatures.Text));
cmd.Parameters.Add(new SqlParameter("@prod_id", int.Parse(prodId)));
cmd.ExecuteNonQuery();
con.Close();
lblError.Text = "Product Updated Successfully.";
hlinkView.Text = "";
panelLbl.Visible = true;
hlinkView.Visible = false;
if (ddCategory.SelectedItem.Text == "Other")
{
string strQry = "update userAddedCategories set cat_name='" + txtCat.Text + "',subcat_name='" + txtSubCategory.Text + "' where prod_id=" + int.Parse(Request.QueryString["prodid"].ToString());
common.ExecuteQueryString(strQry);
}
}
}
protected void imgBtnCancel_Click(object sender, EventArgs e)
{
LoadingPage();
}
protected void ddCategory_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddCategory.SelectedItem.Text == "Other")
{
ddSubCategory.Enabled = false;
txtCat.Visible = true;
txtSubCategory.Visible = true;
}
else
{
txtCat.Visible = false;
txtSubCategory.Visible = false;
ddSubCategory.Enabled = true;
ddSubCategory.Items.Clear();
DataSet ds1 = new DataSet();
ds1 = common.ExecuteDataset("select subcat_id,subcat_name from subcategories where cat_id=" + int.Parse(ddCategory.SelectedValue.ToString()));
if (ds1.Tables[0].Rows.Count > 0)
{
ddSubCategory.DataSource = ds1;
ddSubCategory.DataValueField = "subcat_id";
ddSubCategory.DataTextField = "subcat_name";
ddSubCategory.DataBind();
}
}
}
protected void btnAddMore_Click(object sender, EventArgs e)
{
if (count < 19-LoadTextboxes.Count)
{
TextBox tb = new TextBox();
tb.CssClass = "newinput";
tb.ID = "tb" + textboxes.Count;
tb.Width = 450;
panel1.Controls.Add(tb);
textboxes.Add(tb);
}
count = int.Parse(textboxes.Count.ToString());
}
protected void cmdReset_Click(object sender, EventArgs e)
{
textboxes.Clear();
count = int.Parse(textboxes.Count.ToString());
textboxes.Clear();
count = 0;
if (Request.QueryString["prodid"] != null)
{
textboxes.Clear();
count = int.Parse(textboxes.Count.ToString());
textboxes.Clear();
count = 0;
}
else
{
txtProdName.Text = "";
txtProdType.Text = "";
txtProdCode.Text = "";
txtTagName.Text = "";
txtProdDetails.Text = "";
ddCategory.SelectedIndex = -1;
ddSubCategory.SelectedIndex = -1;
textboxes.Clear();
count = int.Parse(textboxes.Count.ToString());
count = 0;
}
}
void _Default_PreRender(object sender, EventArgs e)
{
count = int.Parse(textboxes.Count.ToString());
count1 = int.Parse(LoadTextboxes.Count.ToString());
}
}