C
CreativeMind
hi,
i m facing problem in postback...
I m creating a textbox named as txtdefault.
and some textboxes at runtime which persist their values on postback.
I m using a btnAdd to add(append) a textbox. it works.
on selectedindexchanged of dropdown i make numberofcontrols as 0. and
then create them depending upon the database values number.
now after this event, when i try to add a textbox, it creates problem,
can u help me out, plz?
i want simply to add the textbox in addition to already created
textbox.
here goes all of my code...
public partial class CustomFields : System.Web.UI.Page
{
Members _objMember;
public CustomFields()
{
_objMember = new Members();
}
protected int NumberOfControls
{
get { return (int)ViewState["NumControls"]; }
set { ViewState["NumControls"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (Session["sess_cur_member"] != null)
{
_objMember = (Members)(Session["sess_cur_member"]);
if (_objMember == null)
_objMember = new Members();
if (Page.IsPostBack)
{
// if (this.ddlStores.SelectedIndex < 1)
if(ViewState["flag"]!=null)
{
NumberOfControls = ViewState["flag"].ToString
() == "0" ? 0 : NumberOfControls;
ViewState["flag"] = null;
}
this.CreateControls();
}
else
{
this.NumberOfControls = 0;
FillStores();
//CreateSubmitButton();
}
}
else
{
Response.Redirect("../default.aspx");
}
}
private void CreateControls()
{
int count = this.NumberOfControls;
for (int i = 0; i < count; i++)
{
TextBox tx = new TextBox();
tx.ID = "ControlID_" + i.ToString();
phStores.Controls.Add(new LiteralControl("<br/
}
}
private void AddNewControl()
{
TextBox tx = new TextBox();
tx.ID = "ControlID_" + NumberOfControls.ToString();
phStores.Controls.Add(new LiteralControl("<br/
NumberOfControls++;
}
private void FillStores()
{
this.ddlStores.DataTextField = "StoreName";
this.ddlStores.DataValueField = "StoreId";
this.ddlStores.DataSource = DBUtility.getDropDownDataSet
("stores", "StoreId", "StoreName", true, "0", "Please Select", null,
"text");
this.ddlStores.DataBind();
}
protected void btnAddQuestion_Click(object sender, EventArgs
e)
{
ViewState["flag"] = null;
AddNewControl();
}
private void RetrieveControls()
{
DataSet ds = DBUtility.getDataSet("select top 1
optionalfield1,optionalfield2,optionalfield3,optionalfield4,optionalfield5,optionalfield6,optionalfield7,optionalfield8,optionalfield9,optionalfield10
from storeoptionalfields where storeid=" +
this.ddlStores.SelectedValue);
this.txtdefault.Text = "";
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
//int counter = 0;
foreach (DataRow dr in ds.Tables[0].Rows)
{
foreach (DataColumn dc in ds.Tables[0].Columns)
{
if (dr["OptionalField1"] != DBNull.Value)
this.txtdefault.Text = dr["OptionalField1"].ToString();
if (dr[NumberOfControls+ 1] != DBNull.Value)
{
phStores.Controls.Add(new LiteralControl
("<br/><span>Question:</span>"));
TextBox tx = new TextBox();
tx.ID = "ControlID_" +
NumberOfControls.ToString();
tx.Text = dr[NumberOfControls + 1].ToString
();
phStores.Controls.Add(tx);
NumberOfControls++;
//NumberOfControls++;
}
}
}
--NumberOfControls;
ViewState["flag"] = null;
}
}
protected void ddlStores_SelectedIndexChanged(object sender,
EventArgs e)
{
ViewState["flag"] = 0;
RetrieveControls();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
DBUtility dbUtility = new DBUtility();
bool flag = false;
TextBox tb = (TextBox)phStores.FindControl("txtdefault");
if (tb != null && !tb.Text.Trim().Equals(string.Empty))
{
dbUtility.setValues("optionalfield1", tb.Text.Trim(),
DBTypes.str_text);
flag = true;
}
int k = flag ? 1 : 0;
for (int i = 0; i < NumberOfControls; i++)
{
TextBox tx = (TextBox)phStores.FindControl
("ControlID_" + i.ToString());
dbUtility.setValues("optionalfield" + (++k).ToString
(), tx.Text.Trim(), DBTypes.str_text);
}
if (dbUtility.insertRow("storeoptionalfields") > 0)
Response.Redirect("dashboard.aspx");
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("DashBoard.aspx");
}
}
thx
i m facing problem in postback...
I m creating a textbox named as txtdefault.
and some textboxes at runtime which persist their values on postback.
I m using a btnAdd to add(append) a textbox. it works.
on selectedindexchanged of dropdown i make numberofcontrols as 0. and
then create them depending upon the database values number.
now after this event, when i try to add a textbox, it creates problem,
can u help me out, plz?
i want simply to add the textbox in addition to already created
textbox.
here goes all of my code...
public partial class CustomFields : System.Web.UI.Page
{
Members _objMember;
public CustomFields()
{
_objMember = new Members();
}
protected int NumberOfControls
{
get { return (int)ViewState["NumControls"]; }
set { ViewState["NumControls"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (Session["sess_cur_member"] != null)
{
_objMember = (Members)(Session["sess_cur_member"]);
if (_objMember == null)
_objMember = new Members();
if (Page.IsPostBack)
{
// if (this.ddlStores.SelectedIndex < 1)
if(ViewState["flag"]!=null)
{
NumberOfControls = ViewState["flag"].ToString
() == "0" ? 0 : NumberOfControls;
ViewState["flag"] = null;
}
this.CreateControls();
}
else
{
this.NumberOfControls = 0;
FillStores();
//CreateSubmitButton();
}
}
else
{
Response.Redirect("../default.aspx");
}
}
private void CreateControls()
{
int count = this.NumberOfControls;
for (int i = 0; i < count; i++)
{
TextBox tx = new TextBox();
tx.ID = "ControlID_" + i.ToString();
phStores.Controls.Add(new LiteralControl("<br/
phStores.Controls.Add(tx);<span>Question:</span>"));
}
}
private void AddNewControl()
{
TextBox tx = new TextBox();
tx.ID = "ControlID_" + NumberOfControls.ToString();
phStores.Controls.Add(new LiteralControl("<br/
phStores.Controls.Add(tx);<span>Question:</span>"));
NumberOfControls++;
}
private void FillStores()
{
this.ddlStores.DataTextField = "StoreName";
this.ddlStores.DataValueField = "StoreId";
this.ddlStores.DataSource = DBUtility.getDropDownDataSet
("stores", "StoreId", "StoreName", true, "0", "Please Select", null,
"text");
this.ddlStores.DataBind();
}
protected void btnAddQuestion_Click(object sender, EventArgs
e)
{
ViewState["flag"] = null;
AddNewControl();
}
private void RetrieveControls()
{
DataSet ds = DBUtility.getDataSet("select top 1
optionalfield1,optionalfield2,optionalfield3,optionalfield4,optionalfield5,optionalfield6,optionalfield7,optionalfield8,optionalfield9,optionalfield10
from storeoptionalfields where storeid=" +
this.ddlStores.SelectedValue);
this.txtdefault.Text = "";
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
//int counter = 0;
foreach (DataRow dr in ds.Tables[0].Rows)
{
foreach (DataColumn dc in ds.Tables[0].Columns)
{
if (dr["OptionalField1"] != DBNull.Value)
this.txtdefault.Text = dr["OptionalField1"].ToString();
if (dr[NumberOfControls+ 1] != DBNull.Value)
{
phStores.Controls.Add(new LiteralControl
("<br/><span>Question:</span>"));
TextBox tx = new TextBox();
tx.ID = "ControlID_" +
NumberOfControls.ToString();
tx.Text = dr[NumberOfControls + 1].ToString
();
phStores.Controls.Add(tx);
NumberOfControls++;
//NumberOfControls++;
}
}
}
--NumberOfControls;
ViewState["flag"] = null;
}
}
protected void ddlStores_SelectedIndexChanged(object sender,
EventArgs e)
{
ViewState["flag"] = 0;
RetrieveControls();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
DBUtility dbUtility = new DBUtility();
bool flag = false;
TextBox tb = (TextBox)phStores.FindControl("txtdefault");
if (tb != null && !tb.Text.Trim().Equals(string.Empty))
{
dbUtility.setValues("optionalfield1", tb.Text.Trim(),
DBTypes.str_text);
flag = true;
}
int k = flag ? 1 : 0;
for (int i = 0; i < NumberOfControls; i++)
{
TextBox tx = (TextBox)phStores.FindControl
("ControlID_" + i.ToString());
dbUtility.setValues("optionalfield" + (++k).ToString
(), tx.Text.Trim(), DBTypes.str_text);
}
if (dbUtility.insertRow("storeoptionalfields") > 0)
Response.Redirect("dashboard.aspx");
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("DashBoard.aspx");
}
}
thx