Bindind Repeater to Stack List - c#

Here is code. Its not showing any compile or run time error. I debugged also then till data binding works fine. But then also controls not shown in web page!
Sample.aspx:
<body>
<form id="form1" runat="server">
<asp:PlaceHolder ID="_placeHolder1" runat="server">
</asp:PlaceHolder>
</form>
</body>
Sample.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
_placeHolder1.Controls.Add(CreateReapeater());
}
private Control CreateReapeater()
{
Repeater _repeater1 = new Repeater();
Stack _stack1 = new Stack();
for (int i = 0; i < 7; i++)
{
_stack1.Push(i);
}
_repeater1.DataSource = _stack1;
_repeater1.DataBind();
return _repeater1;
}

Actually repeater have no inbuild column structure as like gridview. so when we bind repeater dynamically we need to also create the item template for that.
You need to modify the createrepeater function as below.
private Control CreateReapeater()
{
Repeater _repeater1 = new Repeater();
Stack _stack1 = new Stack();
for (int i = 0; i < 7; i++)
{
_stack1.Push(i);
}
_repeater1.DataSource = _stack1;
_repeater1.DataBind();
foreach (RepeaterItem repeatItem in _repeater1.Items)
{
int index = repeatItem.ItemIndex;
RepeaterItem repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Item);
Label lbl = new Label();
lbl.Text = "Item No :" + index.ToString() + "<br/>";
repeatItem.Controls.Add(lbl);
}
return _repeater1;
}
This will resolve your issue.
Happy Coding.....

Related

C# - How to add and delete multiple controls dynamically on button click

I am trying to code a survey application which requires text boxes to be added and removed dynamically. I managed to create text boxes dynamically on button click but I'm trying to find out how to removed the text boxes dynamically. Currently the delete functions works well only if I remove text boxes starting from the last. When I try deleting any text boxes in the middle followed by a delete of the last text box, a blank text box is created instead. I'm new to programming and am unsure if this is the best method to use. Also, I would like to find out if it is possible to nest text boxes(allow users to add options to their multi choice questions) to created text boxes and retrieve values inside later.
Code behind:
public partial class Dynamic_Form : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < TotalNumberAdded; ++i)
{
AddQuestion(i + 1);
}
}
protected int TotalNumberAdded
{
get { return (int)(ViewState["TotalNumberAdded"] ?? 0); }
set { ViewState["TotalNumberAdded"] = value; }
}
protected void btn_add_Click(object sender, EventArgs e)
{
TotalNumberAdded++;
AddQuestion(TotalNumberAdded);
}
private void AddQuestion(int controlNumber)
{
Panel newquestion = new Panel();
newquestion.ID = "Panel_" + controlNumber;
TextBox questionbox = new TextBox();
questionbox.ID = "Question_" + controlNumber;
questionbox.Text = "Question";
Button BtnDelete = new Button();
BtnDelete.ID = "BtnDelete_" + controlNumber;
BtnDelete.Text = "Delete"+ controlNumber;
BtnDelete.Click += delete_Click;
DropDownList DDLType = new DropDownList();
DDLType.ID = "DDLType_" + controlNumber;
DDLType.Items.Insert(0, new ListItem("TextField", ""));
DDLType.Items.Insert(1, new ListItem("Multiple Choice", ""));
//add controls
questions.Controls.Add(newquestion);
newquestion.Controls.Add(questionbox);
newquestion.Controls.Add(DDLType);
newquestion.Controls.Add(BtnDelete);
}
private void delete_Click(object sender,EventArgs e)
{
Button btn = (Button)sender;
Control control = btn.Parent;
if (questions.Controls.Contains(control))
{
questions.Controls.Remove(control);
control.Dispose();
TotalNumberAdded--;
Response.Write("Number of Questions:" + TotalNumberAdded);
}
}
}
Html file:
<body>
<form id="form1" runat="server">
<div>
<h1>Test Form</h1>
<asp:Button ID="btn_add" runat="server" Text="Add" OnClick="btn_add_Click" />
</div>
<div id="questions" runat="server">
</div>
</form></body>
.
#Aaron Yong, You can do these scenarios from client side using jQuery or javascript too. I have given below code to achieve what you want from server side.
private void delete_Click(object sender,EventArgs e)
{
Button btn = (Button)sender;
Control control = btn.Parent;
if (questions.Controls.Contains(control))
{
TotalNumberAdded--;
questions.Controls.Clear();
for (int i = 0; i < TotalNumberAdded; ++i)
{
AddQuestion(i + 1);
}
Response.Write("Number of Questions:" + TotalNumberAdded);
}
}
You want to recreate controls again, i have added for loop and removed some code in delete click event, then you will achieve.
As i have given above answer, you can also remove below code from previous answered.
questions.Controls.Remove(control);
control.Dispose();
You will get exact answer

how to delete dynamic created textbox controls on dropdownlist selectedIndexChanged event and recreate new dynamic control on new selected value

Aspx page:
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:placeholder id="container" runat="server" visible="false"></asp:placeholder>
</td>
</tr>
</table>
On Code Behind file:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
generateDynamicControls();
}
private void generateDynamicControls()
{
for (int i = 0; i < dt.Rows.Count; i++)
{
HtmlGenericControl tr = new HtmlGenericControl("tr");
HtmlGenericControl td1 = new HtmlGenericControl("td");
Label lbl = new Label();
lbl.ID = "lbl" + dt.Rows[i]["sSubjectName"].ToString();
lbl.Text = dt.Rows[i]["sSubjectName"].ToString() + " :";
lbl.Width = new Unit(170);
lbl.Style.Add("padding-left", "10px");
lbl.Style.Add("font-weight", "bold");
td1.Controls.Add(lbl);
tr.Controls.Add(td1);
HtmlGenericControl td2 = new HtmlGenericControl("td");
TextBox txtBox = new TextBox();
txtBox.ID = "txt" + dt.Rows[i]["sSubjectName"].ToString();
txtBox.CssClass = "TxtBox";
txtBox.TextMode = TextBoxMode.MultiLine;
txtBox.Width = new Unit(840);
td2.Style.Add("colspan", "3");
td2.Controls.Add(txtBox);
tr.Controls.Add(td2);
container.Controls.Add(tr);
HtmlGenericControl tr1 = new HtmlGenericControl("tr");
HtmlGenericControl td3 = new HtmlGenericControl("td");
td3.Style.Add("colspan", "4");
td3.InnerHtml = " ";
tr1.Controls.Add(td3);
container.Controls.Add(tr1);
//container.Controls.Add(new LiteralControl("<br />"));
//LiteralControl literalBreak = new LiteralControl();
//literalBreak.Text ="</br>";
}
container.Visible = true;
}
}
`dt(datatable)` is filled from other function.
this is working fine and controls are created correclty.
But I want to recreate this dynamic controls on selected change event of dropdownlist. but before recreate these controls i have to delete previously created controls.
I have tried below code in dropdownlist selected changed event to delete these controls and recreate new controls.
for (int i = 0; i < dt.Rows.Count; i++)
{
container.Controls.Remove(container.FindControl(dt.Rows[i]["nSubjectId"].ToString()));
}
generateDynamicControls();
But it's not work and give me following error. becuase of onload event is alread ycalled first and created the dyanmic controls.
Multiple controls with the same ID 'lblOrals' were found. FindControl requires that controls have unique IDs.
How can I solve this problem.
Please help me solve this problem.
Thanks in advance
Try this
Give the for loop variable (i) to your label id, like the below way
lbl.ID = "lbl" + dt.Rows[i]["sSubjectName"].ToString()
+i.ToString();//i is loop variable for coordinated with labelid.
Now if the "dt.Rows[i]["sSubjectName"]" is return "LabeleName", When the for loop will assign the values like
dt.Rows[i]["sSubjectName"].ToString() + i.ToString() , So It is return values like "LabeleName1"
Finally you got the label id's are followings name
lblLabeleName1
lblLabeleName2
lblLabeleName3
.
.
.
.etc
textbox are like
txtBox.ID = "txt" + dt.Rows[i]["sSubjectName"].ToString() + i.ToString();
Result are
txtLabeleName1
txtLabeleName2
txtLabeleName3
.
.
.
etc.
Remove fields code like
//For label
container.Controls.Remove(container.FindControl("lbl" + dt.Rows[i]["sSubjectName"].ToString() +i.ToString()));
//For TextBox
container.Controls.Remove("txt" + dt.Rows[i]["sSubjectName"].ToString()+ i.ToString() ));
And Should call the generateDynamicControls(); method inside of !ISPostBack() event in page load event
Like
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if(!IsPostBack)
{
generateDynamicControls();
}
}

avoid postback dynamically created checkbox in asp.net c#

Within a web form I am dynamically creating a series of chekboxes that are added to an asp:panel.Where as the panel in reside into a update panel.I want to avoid postback on checkbox changed event fire.
my code snippet:
Aspx code:
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Panel ID="pnlCuisine" runat="server"></asp:Panel>
</ContentTemplate>
C# Code:
public static CheckBox[] chkbx = new CheckBox[15];
public static Label[] lblCuisine = new Label[15];
private void LoadCuisine(string searchStr)
{
.....
.....
.....
int i = 1;
foreach(var item in cuisineList)
{
chkbx[i]=new CheckBox();
chkbx[i].ID = item.CategoryName;
chkbx[i].Text = item.CategoryName;
chkbx[i].AutoPostBack = true;
lblCuisine[i] = new Label();
lblCuisine[i].ID = "lblCuisine" + Convert.ToString(i);
lblCuisine[i].Text = "(" + item.Count + ")";
chkbx[i].CheckedChanged += new EventHandler(chkbx_CheckedChanged);
pnlCuisine.Controls.Add(chkbx[i]);
pnlCuisine.Controls.Add(lblCuisine[i]);
pnlCuisine.Controls.Add(new LiteralControl("<br/>"));
i++;
}
}
void chkbx_CheckedChanged(object sender, EventArgs e)
{
string searchString = ((CheckBox)sender).ClientID;
populateGrid(searchString);
}
Please change this line of code
chkbx[i].AutoPostBack = true;
to
chkbx[i].AutoPostBack = false;
then you can avoid auto post back on checkbox changed event fire

ASP Text box ID assignment using a for loop.?

I am a noob and i've been trying to figure out how we may assign an ID to asp:TextBox tags on creation in ASP.NET using c#.
Example:
I need to create a thread that may have multiple textboxes.
When a user clicks on a button, a text box must be generated with an ID say, txt01. On being clicked the second time, the ID of the generated text box must be txt02 and so on..depending on the number of clicks.
Thanks in Advance.
Remember last ID in some variable, for example int lastID, then in button's onClick method when creating the new TextBox assign its ID="txt"+lastID;.
You have to persist the lastID during page postbacks, you can store it in a ViewState.
Take and placeholder in your aspx page: eg: <asp:PlaceHolder runat="server" ID="pholder" />
and in code behind:
TextBox txtMyText = new TextBox();
tb1.ID = YourDynamicId;
pholder.Controls.Add(txtMyText);
You can save current id in ViewState and get the same id and assign incremented id to your dynamic textbox.
Try This:
int i = 1;
if (ViewState["i"] == null)
{
ViewState["i"] = i;
}
else
i = (int)ViewState["i"];
PlaceHolder1.Controls.Clear();
for (int j = 1; j <= i; j++)
{
TextBox TextBox = new TextBox();
TextBox.ID = "TextBox" + j.ToString();
PlaceHolder1.Controls.Add(TextBox);
}
ViewState["i"] = i + 1;
add this on your .aspx page
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
Hope This help.
I think this is what you are looking for -
You will notice that I have used Page_Init because if you create/add the controls in Page_Load then FindControl will return null in PostBack. And also any data you entered in the dynamically added controls will not persist during Postbacks.
But Page_Init is called before ViewState or PostBack data is loaded. Therefore, you can not use ViewState or any other controls to keep the control count. So I have used Session to keep the count.
Try it out and let me know what you think.
ASPX Page
<form id="form1" runat="server">
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="btnCreate" runat="server" Text="Create" OnClick="btnCreate_Click" />
<asp:Button ID="btnRead" runat="server" Text="Read" OnClick="btnRead_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
Code Behind
protected int NumberOfControls
{
get { return Convert.ToInt32(Session["noCon"]); }
set { Session["noCon"] = value.ToString(); }
}
private void Page_Init(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
//Initiate the counter of dynamically added controls
this.NumberOfControls = 0;
else
//Controls must be repeatedly be created on postback
this.createControls();
}
private void Page_Load(object sender, System.EventArgs e)
{
}
protected void btnCreate_Click(object sender, EventArgs e)
{
TextBox tbx = new TextBox();
tbx.ID = "txtData"+NumberOfControls;
NumberOfControls++;
PlaceHolder1.Controls.Add(tbx);
}
protected void btnRead_Click(object sender, EventArgs e)
{
int count = this.NumberOfControls;
for (int i = 0; i < count; i++)
{
TextBox tx = (TextBox)PlaceHolder1.FindControl("txtData" + i.ToString());
//Add the Controls to the container of your choice
Label1.Text += tx.Text + ",";
}
}
private void createControls()
{
int count = this.NumberOfControls;
for (int i = 0; i < count; i++)
{
TextBox tx = new TextBox();
tx.ID = "txtData" + i.ToString();
//Add the Controls to the container of your choice
PlaceHolder1.Controls.Add(tx);
}
}
store the id in a ViewState something like this
First initialise a count variable similiar to this.
in your class write this
protected int replyCount //declare the variable
{
get { return (int)ViewState["replyCount"]; }
set { ViewState["replyCount"] = value; }
}
In your page Load write this to initialise the replyCount if its not a postback;
protected void Page_Load(object sender, EventArgs e)
{
if(!page.IsPostBack)
{
replyCount = 0; //initialise the variable
}
}
then while creating dynamic textboxes
protected void Button_Click(Object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.id = "tb" + replycount; //use the variable
replycount++; // and after using increment it.
form.controls.add(tb); // assuming your form name is "form"
}
Thats it you should do.

Read all rows and cell values dynamic table asp.net

I found the tutorial to create a dynamic table and add rows:
http://geekswithblogs.net/dotNETvinz/archive/2009/06/29/faq-dynamically-adding-rows-in-asp-table-on-button-click.aspx
How can I read all rows in the table, and then the values ​​of the textbox in the cells? The values ​​are entered in a table in a database (SQL Server).
Can I continue to use C# and asp.net or do I have to use Javascript?
I hope someone will give me help.
This is the code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Dynamic Adding of Rows in ASP Table Demo</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Add New Row" />
</form>
</body>
</html>
CODE BEHIND:
public partial class _Default1 : System.Web.UI.Page
{
//A global variable that will hold the current number of Rows
//We set the values to 1 so that it will generate a default Row when the page loads
private int numOfRows = 1;
protected void Page_Load(object sender, EventArgs e)
{
//Generate the Rows on Initial Load
if (!Page.IsPostBack)
{
GenerateTable(numOfRows);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (ViewState["RowsCount"] != null)
{
numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString());
GenerateTable(numOfRows);
}
}
private void SetPreviousData(int rowsCount, int colsCount)
{
Table table = (Table)Page.FindControl("Table1");
if (table != null)
{
for (int i = 0; i < rowsCount; i++)
{
for (int j = 0; j < colsCount; j++)
{
//Extracting the Dynamic Controls from the Table
TextBox tb = (TextBox)table.Rows[i].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);
//Use Request objects for getting the previous data of the dynamic textbox
tb.Text = Request.Form["TextBoxRow_" + i + "Col_" + j];
}
}
}
}
private void GenerateTable(int rowsCount)
{
//Creat the Table and Add it to the Page
Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);
//The number of Columns to be generated
const int colsCount = 3;//You can changed the value of 3 based on you requirements
// Now iterate through the table and add your controls
for (int i = 0; i < rowsCount; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < colsCount; j++)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
// Set a unique ID for each TextBox added
tb.ID = "TextBoxRow_" + i + "Col_" + j;
// Add the control to the TableCell
cell.Controls.Add(tb);
// Add the TableCell to the TableRow
row.Cells.Add(cell);
}
// And finally, add the TableRow to the Table
table.Rows.Add(row);
}
//Set Previous Data on PostBacks
SetPreviousData(rowsCount, colsCount);
//Sore the current Rows Count in ViewState
rowsCount++;
ViewState["RowsCount"] = rowsCount;
}
}
Since you're creating the table dynamically, you can't reference it like controls that are statically embedded on the page. To get a reference to the Table object you'll need to find it in the Page's ControlCollection and cast it out, just like in the example:
Table table = (Table)Page.FindControl("Table1");
The Table class contains a table row collection which you will then need to loop over. Each row in the row collection has a collection of cells, each of which will contain child controls (see: Controls property) which will be your textboxes.
Hi if you take look at SetPreviousData this function is trying to read previous textbox value but there is some thing wrong with it i have done following changes and it works fine
first of all the following code in SetPreviousData
Table table = (Table)Page.FindControl("Table1");
is always returning NULL because this function is not a recursive function and your table may be inside a container in your page so add following function to your code
private Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
and change the code
Table table = (Table)Page.FindControl("Table1");
to
Table table = FindControlRecursive(Page , "Table1") as Table;
in next step change the following code in SetPreviousData
tb.Text = Request.Form["TextBoxRow_" + i + "Col_" + j];
to
tb.Text = Request.Form[tb.UniqueID];
set break points and see the results if it's tough to you let me know to provide you a function which returns table data in a datatable
I think you must bind your table again at the Page_load event.For example if you are binding the table at View_Click button event,you have to call this method in Page_load event as View_Click(null,null).
<script type="text/javascript">
var StringToSend='';
function fncTextChanged(newvalueadded) {
if (StringToSend != '')
StringToSend = StringToSend + ';' + newvalueadded;
else
StringToSend = newvalueadded;
//alert(StringToSend); -- For Testing all values
// now store the value of StringToSend into hidden field ---- HFTEXTVALUES to access it from server side.
}
</script>
<asp:Button ID="Save" runat="server" onclick="SaveTextBoxValues_Click" Text="save" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Add New Row" />
<asp:HiddenField ID="hfTextValues" runat="server" Visible="false" Value="" />
Server Side :
protected void Page_Load(object sender, EventArgs e)
{
//Generate the Rows on Initial Load
if (!Page.IsPostBack)
{
GenerateTable(numOfRows);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (ViewState["RowsCount"] != null)
{
numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString());
GenerateTable(numOfRows);
}
}
protected void SaveTextBoxValues_Click(object sender, EventArgs e)
{
string AllTextBoxValues = hfTextValues.Value;
string[] EachTextBoxValue = AllTextBoxValues.Split('~');
//here you would get all values, EachTextBoxValue[0] gives value of first textbox and so on.
}
private void GenerateTable(int rowsCount)
{
//Creat the Table and Add it to the Page
Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);
//The number of Columns to be generated
const int colsCount = 3;//You can changed the value of 3 based on you requirements
// Now iterate through the table and add your controls
for (int i = 0; i < rowsCount; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < colsCount; j++)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
HiddenField hf = new HiddenField();
// Set a unique ID for each TextBox added
tb.ID = "tb" + i + j;
hf.ID = "hf" + i + j;
tb.Attributes.Add("onblur", "fncTextChanged(this.value);");
// Add the control to the TableCell
cell.Controls.Add(tb);
// Add the TableCell to the TableRow
row.Cells.Add(cell);
}
// And finally, add the TableRow to the Table
table.Rows.Add(row);
}
}
Let me know if you have any questions.
One more thing, if you wish to store the values and retrieve them later, the code would be modified accordingly.
reference for a dynamic table doesn't work well , blame microsoft ,
I use this :
Save inside a session List[YourObjectData] and every time you get into Page_Load , fill it the table
here is solution for number 2 :
somewhere inside Default.aspx
<div class="specialTable">
<asp:Table ID="Table1" runat="server" />
</div>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
FillDynamicTable();
if (!IsPostBack)
{
}
}
private void FillDynamicTable()
{
Table1.Rows.Clear(); // Count=0 but anyways
if (Session["Table1"] == null) return;
foreach (var data in Session["Table1"] as List<MyObject>)
{
AddRow(data);
}
}
private void AddRow(MyObject data)
{
var row = new TableRow { Height = 50 };
var cell = new TableCell();
var label = new Label { Text = data.something, Width = 150 };
cell.Controls.Add(label);
row.Cells.Add(cell);
Table1.Rows.Add(row);
}

Categories

Resources