Runtime control creation and postback problem

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/
<span>Question:</span>"));
phStores.Controls.Add(tx);

}
}


private void AddNewControl()
{


TextBox tx = new TextBox();
tx.ID = "ControlID_" + NumberOfControls.ToString();
phStores.Controls.Add(new LiteralControl("<br/
<span>Question:</span>"));
phStores.Controls.Add(tx);

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
 
C

CreativeMind

hi,

on selected index change event of dropdown, i m retrieving records
from db. if records are 3 , 3 controls are created. it's fine.. but
when i click on Add btn , it eliminates/over-writes the third control.
by showing the newly created textbox.. i want to add the new text box
after position 3rd in this case. in other words, Numberofcontrol
property is not setting at btnadd click.. how can i set?

thanx
 
A

Andrew Morton

CreativeMind said:
on selected index change event of dropdown, i m retrieving records
from db. if records are 3 , 3 controls are created. it's fine.. but
when i click on Add btn , it eliminates/over-writes the third control.
by showing the newly created textbox.. i want to add the new text box
after position 3rd in this case. in other words, Numberofcontrol
property is not setting at btnadd click.. how can i set?

It looks to me like your Numberofcontrols variables may be out of step with
what you're trying to do. I think you may need to increment it /before/
adding the new one. Stepping through it in the debugger should show if
that's the case.

Oh, and
"DataSet ds = DBUtility.getDataSet("select top 1
optionalfield1,optionalfield2,optionalfield3,optionalfield4,optionalfield5,optionalfield6,optionalfield7,optionalfield8,optionalfield9,optionalfield10
from storeoptionalfields where storeid=" +
this.ddlStores.SelectedValue);"

is a possible SQL injection attack vector unless you've validated
this.ddlStores.SelectedValue before using it.

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top