What im trying to do is add a table row after clicking an add btn. Im getting this to work, but I get a problem when I click the newly created button in the newly added row. What is does it removes the row that has just been added for some some reason.
For example. I click add. It adds a row. I click add again and it removes the previously added row. Code below will illustrate what im doing.
And I also wanna know how I can use my newly created controls after adding them to a panel. My guess is to use viewstate["controlID"]???
when i put the method only in page_load, which i dont want to control gets added and when i add anothe one the control does not dispear but a new one doesnt get added. i thought the problem was because my i++ is not actually working. I set the i=0 in page_load()
if(!this.ispostback){ i=0}
bacause ii thought when client request for a page it creates an object page then with i=0. then when user does a call back it uses this same object and uses the same i. But it seems as though it increments it from 0 -> 1 again.
heres some code. Would ther be a better way in doing what im trying to do. maybe im not understanding the concept correctly. an i do not want to call the method in page_load as I have varu\ious updatePanel on my page.
Now it adds the whilst running the method below to add content to panel. But doing this adds the first time u click button. the second button i click wants to validate the form.
-I have RequiredFieldValidator. but i set the validation group for them to "a". because they conflicted with my buttons before.
Therefore in the code below i set the validation group to "b". when i do not initialise this it still tries to validate the formjust as if i would not initialise it. so btn.validationgroup doesnt seem to be initialising.
All of this code is in a big registration form
I would like to know how i would get the newly created code by my addRowToPanel to panel methods. retrieve their vali\ues in c#
ControlCollection controls = Page.Controls
?
would that work. im not there yet, but i just want to know when i get there how i would do it.
Becasue you are building you table dynamically the row gets created and after the PostBack occurs it Renders back to the basic HTML. Think this is the reason you don't see your added row in there. To avoid this you have to create your table ans EventHandlers
on every PostBack.
PageLoad()
{
CreateTable();
}
So where ever You want to add a new row to your table save the Whole table in the Session and build it again on PostBack. This should do the work for you.
Guculuma
Member
13 Points
36 Posts
Updating a panel inside an updatePanel via a button click
Feb 21, 2012 11:16 AM|LINK
What im trying to do is add a table row after clicking an add btn. Im getting this to work, but I get a problem when I click the newly created button in the newly added row. What is does it removes the row that has just been added for some some reason.
For example. I click add. It adds a row. I click add again and it removes the previously added row. Code below will illustrate what im doing.
And I also wanna know how I can use my newly created controls after adding them to a panel. My guess is to use viewstate["controlID"]???
<asp:UpdatePanel runat="server" ID="UpdatePanelSubjectType" UpdateMode="Conditional">
<ContentTemplate>
<table class="registrationTabel" >
<tr >
<td></td><td><asp:Label runat="server" Text="Type" /></td><td><asp:Label ID="Label6" runat="server" Text="Subject Category" /></td><td><asp:Label ID="Label8" runat="server" Text="Subject" /></td><td></td>
</tr>
<tr>
<td style="text-align:right;"><asp:Label runat="server" Text="Register 1:"/></td>
<td><asp:DropDownList ID="dropDownType" Width="130" runat="server" /></td>
<td><asp:DropDownList Width="130" ID="DropDownSubjectCategory" runat="server" /></td>
<td><asp:DropDownList Width="130" ID="DropDownSubject" runat="server" /></td>
<td><asp:ImageButton ID="ImageButton1" OnClick="addASubjectType_Click" runat="server" /></td>
</tr>
<asp:PlaceHolder runat="server" ID="myPlaceHolder" >
</asp:PlaceHolder>
</table>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ImageButton1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
public void addASubjectType_Click(object sender, ImageClickEventArgs e)
{
i++;
Label label = new Label();
label.ID = "RegisterLabel" + i;
label.Text = "Register " + i +": ";
DropDownList addDropListType = new DropDownList();
DropDownList addDropListSubjectCategory = new DropDownList();
DropDownList addDropListSubject = new DropDownList();
ImageButton imageBtn = new ImageButton();
imageBtn.ID = "addTypeSubject"+i;
imageBtn.Click += new ImageClickEventHandler(addASubjectType_Click);
imageBtn.ValidationGroup = "b";
myPlaceHolder.Controls.Add(new LiteralControl("<tr>"));
myPlaceHolder.Controls.Add(new LiteralControl("<td>"));
myPlaceHolder.Controls.Add(label);
myPlaceHolder.Controls.Add(new LiteralControl("</td>"));
myPlaceHolder.Controls.Add(new LiteralControl("<td>"));
myPlaceHolder.Controls.Add(addDropListType);
myPlaceHolder.Controls.Add(new LiteralControl("</td>"));
myPlaceHolder.Controls.Add(new LiteralControl("<td>"));
myPlaceHolder.Controls.Add(addDropListSubjectCategory);
myPlaceHolder.Controls.Add(new LiteralControl("</td>"));
myPlaceHolder.Controls.Add(new LiteralControl("<td>"));
myPlaceHolder.Controls.Add(addDropListSubject);
myPlaceHolder.Controls.Add(new LiteralControl("</td>"));
myPlaceHolder.Controls.Add(new LiteralControl("<td>"));
myPlaceHolder.Controls.Add(imageBtn);
myPlaceHolder.Controls.Add(new LiteralControl("</td>"));
myPlaceHolder.Controls.Add(new LiteralControl("</tr>"));
AsyncPostBackTrigger a = new AsyncPostBackTrigger();
a.ControlID = imageBtn.ID;
a.EventName = "Click";
UpdatePanelSubjectType.Triggers.Add(a);
// ImageButton1.Click -= new ImageClickEventHandler(addASubjectType_Click);
ImageButton1.Enabled = false;
// ImageButton1.Visible = false;
//addDropList.ID = ""
}
Shanthi.Gang...
Member
59 Points
34 Posts
Re: Updating a panel inside an updatePanel via a button click
Feb 21, 2012 11:47 AM|LINK
The controls will be cleared when the postback occurs.
Place your code in a separate function and call it in page postback and button click, it will do the trick
Let me know if this helps you.
MCTS in Microsoft .NET Framework 3.5, ASP.NET Application Development
Coimbatore, Tamil Nadu, India
Guculuma
Member
13 Points
36 Posts
Re: Updating a panel inside an updatePanel via a button click
Feb 21, 2012 02:58 PM|LINK
page postback?
you mean in the page_load?
when i put the method only in page_load, which i dont want to control gets added and when i add anothe one the control does not dispear but a new one doesnt get added. i thought the problem was because my i++ is not actually working. I set the i=0 in page_load() if(!this.ispostback){ i=0}
bacause ii thought when client request for a page it creates an object page then with i=0. then when user does a call back it uses this same object and uses the same i. But it seems as though it increments it from 0 -> 1 again.
heres some code. Would ther be a better way in doing what im trying to do. maybe im not understanding the concept correctly. an i do not want to call the method in page_load as I have varu\ious updatePanel on my page.
I tried -
taking everything out of the updatePanel -
like this
<table class="registrationTabel" >
<tr >
<td></td><td><asp:Label ID="Label9" runat="server" Text="Type" /></td><td><asp:Label ID="Label10" runat="server" Text="Subject Category" /></td><td><asp:Label ID="Label13" runat="server" Text="Subject" /></td><td></td>
</tr>
<tr>
<td style="text-align:right;"><asp:Label ID="Label16" runat="server" Text="Register 1:"/></td>
<td><asp:DropDownList ID="DropDownList3" Width="130" runat="server" /></td>
<td><asp:DropDownList Width="130" ID="DropDownList4" runat="server" /></td>
<td><asp:DropDownList Width="130" ID="DropDownList5" runat="server" /></td>
<td><asp:ImageButton ID="ImageButton2" OnClick="addASubjectType_Click" runat="server" /></td>
</tr>
<asp:PlaceHolder runat="server" ID="PlaceHolder1" >
</asp:PlaceHolder>
</table>
Now it adds the whilst running the method below to add content to panel. But doing this adds the first time u click button. the second button i click wants to validate the form.
-I have RequiredFieldValidator. but i set the validation group for them to "a". because they conflicted with my buttons before.
Therefore in the code below i set the validation group to "b". when i do not initialise this it still tries to validate the formjust as if i would not initialise it. so btn.validationgroup doesnt seem to be initialising.
All of this code is in a big registration form
I would like to know how i would get the newly created code by my addRowToPanel to panel methods. retrieve their vali\ues in c#
ControlCollection controls = Page.Controls
?
would that work. im not there yet, but i just want to know when i get there how i would do it.
protected void Page_Load(object sender, EventArgs e)
{
datareturner = new DataReturner();
if (!this.IsPostBack)
{
i = 0;
}
else {
addRowToPanel();
}
private void addRowToPanel()
{
i++;
Label label = new Label();
label.ID = "RegisterLabel" + i;
label.Text = "Register " + i + ": ";
DropDownList addDropListType = new DropDownList();
DropDownList addDropListSubjectCategory = new DropDownList();
DropDownList addDropListSubject = new DropDownList();
ImageButton imageBtn = new ImageButton();
imageBtn.ID = "addTypeSubject" + i;
imageBtn.Click += new ImageClickEventHandler(addASubjectType_Click);
imageBtn.ValidationGroup = "b";
myPlaceHolder.Controls.Add(new LiteralControl("<tr>"));
myPlaceHolder.Controls.Add(new LiteralControl("<td>"));
myPlaceHolder.Controls.Add(label);
myPlaceHolder.Controls.Add(new LiteralControl("</td>"));
myPlaceHolder.Controls.Add(new LiteralControl("<td>"));
myPlaceHolder.Controls.Add(addDropListType);
myPlaceHolder.Controls.Add(new LiteralControl("</td>"));
myPlaceHolder.Controls.Add(new LiteralControl("<td>"));
myPlaceHolder.Controls.Add(addDropListSubjectCategory);
myPlaceHolder.Controls.Add(new LiteralControl("</td>"));
myPlaceHolder.Controls.Add(new LiteralControl("<td>"));
myPlaceHolder.Controls.Add(addDropListSubject);
myPlaceHolder.Controls.Add(new LiteralControl("</td>"));
myPlaceHolder.Controls.Add(new LiteralControl("<td>"));
myPlaceHolder.Controls.Add(imageBtn);
myPlaceHolder.Controls.Add(new LiteralControl("</td>"));
myPlaceHolder.Controls.Add(new LiteralControl("</tr>"));
AsyncPostBackTrigger a = new AsyncPostBackTrigger();
a.ControlID = imageBtn.ID;
a.EventName = "Click";
UpdatePanelSubjectType.Triggers.Add(a);
// ImageButton1.Click -= new ImageClickEventHandler(addASubjectType_Click);
ImageButton1.Enabled = false;
// ImageButton1.Visible = false;
//addDropList.ID = ""
}
Thanx your help is greatly appreciated
Guculuma
Member
13 Points
36 Posts
Re: Updating a panel inside an updatePanel via a button click
Feb 21, 2012 03:01 PM|LINK
also, if I had a remove button on the page. how would I remove the individual controls. how should i go about that
sushanth009
Contributor
6243 Points
1168 Posts
Re: Updating a panel inside an updatePanel via a button click
Feb 21, 2012 03:05 PM|LINK
Becasue you are building you table dynamically the row gets created and after the PostBack occurs it Renders back to the basic HTML. Think this is the reason you don't see your added row in there. To avoid this you have to create your table ans EventHandlers on every PostBack.
PageLoad()
{
CreateTable();
}
So where ever You want to add a new row to your table save the Whole table in the Session and build it again on PostBack. This should do the work for you.