Add asp controls dynamically using AJAX - asp.net

I want to make the +Add more original use LinkButton!
I have this row on my table I am using:
<asp:Label ID="Label43" runat="server"
Text="P-2.5 Original use:"></asp:Label>
</td>
<td class="style1">
<div style="float:left;">
<asp:DropDownList class="text-input" ID="DDLOriginalUse" runat="server"
DataSourceID="SqlDataSource5" DataTextField="DESCRIPTION"
DataValueField="ID_PROGRAM_USE" OnSelectedIndexChanged="itemSelected" AutoPostBack="True" AppendDataBoundItems="true">
<asp:ListItem Text="-Not assigned-" Value="-1" Selected="True" />
</asp:DropDownList>
</div>
<asp:UpdatePanel runat="server" style="float:left;" id="UpdatePanel2" updatemode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="DDLOriginalUse" eventname="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<asp:Label ID="lblProgramType" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:SqlDataSource ID="SqlDataSource5" runat="server"
ConnectionString="<%$ ConnectionStrings:MesarchConnectionString %>"
SelectCommand="SELECT * FROM [PROGRAM_USE]"></asp:SqlDataSource>
</td>
</tr>
I want to add new rows with the same content as above so I am using an asp:PlaceHolder :
this code continues..
</tr>
<asp:UpdatePanel runat="server" id="UpdatePanel1" updatemode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="LinkButtonAddOriginal" eventname="Click" />
</Triggers>
<ContentTemplate>
<asp:PlaceHolder ID="PlaceHolderAddNewOriginalUse" runat="server"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
<tr>
<td>
</td>
<td class="style1">
<asp:UpdatePanel runat="server" id="UpdatePanel3" updatemode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="LinkButtonAddOriginal" eventname="Click" />
</Triggers>
<ContentTemplate>
<b><asp:LinkButton ID="LinkButtonAddOriginal" OnClick="AddMoreOriginal" runat="server">+Add more original use</asp:LinkButton></b>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
and in my code behind I am doing this for the click(I want to add maximum 5 text boxes):
private static int countOriginalUse = 1;
private static LiteralControl htmlPresentation = new LiteralControl();
protected void AddMoreOriginal(object sender, EventArgs e)
{
StringBuilder str_for_ltr = new StringBuilder();
countOriginalUse++;
if (countOriginalUse >= 6) { LinkButtonAddOriginal.Text = "You can not add more.";return; }
str_for_ltr.Append("<tr><td> P-2.5 Original use " + countOriginalUse.ToString() + ":</td>");
str_for_ltr.Append("<td class='style1'>");
str_for_ltr.Append("</td>");
str_for_ltr.Append("</tr>");
htmlPresentation.Text += str_for_ltr.ToString();
PlaceHolderAddNewOriginalUse.Controls.Add(htmlPresentation);
/*DropDownList ddl1 = new DropDownList();
ddl1.ID="Dropdown1";
ddl1.CssClass = "Mycss";
ddl1.Items.Add("test");
PlaceHolderAddNewOriginalUse.Controls.Add(ddl1);
PlaceHolderAddNewOriginalUse.Controls.Add(new LiteralControl("<br/>"));*/
}
So the problems I face here are that it does not adds new rows, and even the text is not where I placed my asp:PlaceHolder (is on the top).
And surely I will have more problems when I will have to add this DropDownList with also a UpdatePanel for it(as shown on my first code).
Am I doing it totally wrong, do I have to follow some other way to do it?
I am confused...

I found a way to do it myself.
I made some new rows with DropDownLists in them and their personal UpdateControl but i had their style = "display:none".
then I have my link button like this:
<tr>
<td>
</td>
<td class="style1">
<b><a id="linkAddOriginal" onclick="javascript:addOriginalUse()">+Add more original use</a></b>
</td>
</tr>
and I also made this simple javascript function with a static variable in which I show the appropriate row each time:
<script type="text/javascript">
function addOriginalUse() {
if (typeof addOriginalUse.counter == 'undefined') {
// It has not... perform the initilization
addOriginalUse.counter = 1;
}
addOriginalUse.counter++;
if (addOriginalUse.counter >= 6) { document.getElementById("linkAddOriginal").innerHTML = "You can not add more original uses."; return; }
var ddl = document.getElementById("originalUseRow" + addOriginalUse.counter);
ddl.style.display = 'inline-block';
}
</script>
I have also some extra code showing the data depending on ItemSelection of the DropDownList, if you need it ask me!

Related

Ajax update panel too slow in Asp.net. How to resolve?

I just update the ID passed in NavigateURL of Hyperlink during the selectedindexchanged in my RadioButtonList.
In Mozilla it takes 3.26 seconds for the ID to get updated in Hyperlink
The other browsers also takes too long.
How can i resolve this ?
I read a lot of articles and took the following measures :
Set Debug to false in web.config
Use conditional in Update mode
Only use the controls that has to be updated in the Update panel.
But still, it takes 3.26 seconds...
Protected Sub chlTypes_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chlTypes.SelectedIndexChanged
If (chlTypes.SelectedItem.Value.ToString() <> "") And (chlTypes.SelectedItem.Value.ToString() <> "0") Then
HypView.NavigateUrl = "~/Content.aspx?ID=" + chlTypes.SelectedItem.Value.ToString()
End If
End Sub
<table id="tbl1">
<tr>
<td>
<asp:UpdatePanel ID="UP1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:RadioButtonList ID="chlTypes" runat="server" AutoPostBack="true">
</asp:RadioButtonList>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td align="right">
<asp:UpdatePanel ID="UP2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="caregiver_view_button">
<asp:HyperLink ID="HypView" class="caregiver_view_button"
runat="server"><img src="/view_button.png" /></asp:HyperLink>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="chlTypes" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
</table>

Dropdown not binding Inside Ajax Update panel

I am using the below code to populate a dropdown on a selection of another dropdown.
But somehow, ddlSubTypes is not getting populated when a item is selected in ddlTypes
On selectedindex change event of ddlTypes, i am binding ddlSubTypes.
<tr>
<td class="style3">
<asp:ScriptManager ID="scma" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UP1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlTypes" runat="server" Width="200px" AutoPostBack="true" OnSelectedIndexChanged="ddlTypes_SelectedIndexChanged1">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td class="style3">
<asp:UpdatePanel ID="UP2" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlSubTypes" runat="server" Width="200px">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
Protected Sub ddlTypes_SelectedIndexChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlTypes.SelectedIndexChanged
Try
'Populate schemes
ddlSubTypes.Items.Clear()
Dim ID As Integer = ddlTypes.SelectedValue
Dim dt As DataTable = IterateSubtypesContents(ID)
ddlSubTypes.DataTextField = dt.Columns("Type").ToString()
ddlSubTypes.DataValueField = dt.Columns("ID").ToString()
ddlSubTypes.DataSource = dt
ddlSubTypes.DataBind()
UP2.Update()
Catch ex As Exception
End Try
End Sub
you should add a trigger to the second update panel that gets triggered on the first dropdown's selectedIndexChanged event.
<asp:UpdatePanel ID="UP2" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlSubTypes" runat="server" Width="200px">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Control That Triggers this Panel" EventName="Desired Event that triggers" />
</Triggers>
</asp:UpdatePanel>

ASP.NET Trying to add css class to tr in codebehind

So i have the following method
protected void isDirector_CheckedChanged(object sender, EventArgs e)
{
HtmlTableRow row = (HtmlTableRow)e.Item.FindControl("today");
But getting error
CS0117: 'System.EventArgs' does not contain a definition for 'Item'
EDIT :
<asp:UpdatePanel ID="UpdatePanel2"
runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<asp:RadioButtonList ID="isDirector" RepeatDirection="Horizontal" runat="server" AutoPostBack="True" OnSelectedIndexChanged="isDirector_CheckedChanged">
<asp:ListItem Text="Yes" Value="True" selected></asp:ListItem>
<asp:ListItem Text="No" Value="False"></asp:ListItem>
</asp:RadioButtonList>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel
<ContentTemplate>
<tr runat="server" id="test">
<td>Director First Name:</td>
<td><asp:TextBox ID="DirectorfirstNametxt" runat="server" MaxLength="100" CssClass="input"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" Display="None" runat="server"
ErrorMessage="Director First Name is required." ControlToValidate="DirectorfirstNametxt"></asp:RequiredFieldValidator>
</td>
</tr>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="isDirector" EventName="SelectedIndexChanged" />
</Triggers>
I am trying to change the CSS CLASS of TR ID = "test"
Assuming you have your Checkboxes inside of a HTML-TableRow and you want to set the CSS-Class of the TR in the CheckedChanged-Event:
This is an example(note that the TR's have a runat="server"-tag):
<table>
<tr ID="TR1" runat="server">
<td>
<asp:CheckBox ID="CheckBox1" OnCheckedChanged="isDirector_CheckedChanged" AutoPostBack="true" runat="server" />
</td>
</tr>
<tr ID="TR2" runat="server">
<td>
<asp:CheckBox ID="CheckBox2" OnCheckedChanged="isDirector_CheckedChanged" AutoPostBack="true" runat="server" />
</td>
</tr>
<tr ID="test" runat="server">
<td>
<asp:CheckBox ID="CheckBox3" OnCheckedChanged="isDirector_CheckedChanged" AutoPostBack="true" runat="server" />
</td>
</tr>
</table>
and this is the codebehind:
protected void isDirector_CheckedChanged(object sender, EventArgs e)
{
//var row = (HtmlTableRow)((CheckBox)sender).Parent.Parent;
test.Attributes("class") = "CssClass";
}
Edit: if your tr's are runat="server" and they have unique ID's, you can access them directly
What kind of control is isDirector_CheckChanged tied to - a checkbox?
As the error says, EventArgs is the type of event you're expecting and it doesn't have an 'Item' property. Maybe you're thinking of GridView, Repeater, or other 'item-like' controls?
I'm guessing you're trying to handle the changed event of a checkbox you've put in a repeating/table control. If so, you'll need to handle a Selected or similar event for the repeater that DOES use an EventArgs-derived type with an Item property.

list box validation

i want to validate for listBox whether list box is empty of or not i am doing following
protected void isToListEmpty_ServerValidate(object source, ServerValidateEventArgs args)
{
if (toListBox.Items.Count == 0)
{
args.IsValid = false;
}
else args.IsValid = true;
}
<td>
<asp:UpdatePanel ID="second" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:ListBox id="toListBox" runat="server" Width="150px" Height="200" SelectionMode="Multiple">
</asp:ListBox>
<asp:CustomValidator id="isToListEmpty" runat="server" OnServerValidate="isToListEmpty_ServerValidate" ErrorMessage="Select Student Details" ValidationGroup="verify" ControlToValidate="toListBox"></asp:CustomValidator>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ButtonAdd" EventName="Click"></asp:AsyncPostBackTrigger>
<asp:AsyncPostBackTrigger ControlID="ButtonRemove" EventName="Click"></asp:AsyncPostBackTrigger>
<asp:AsyncPostBackTrigger ControlID="ButtonAddAll" EventName="Click"></asp:AsyncPostBackTrigger>
<asp:AsyncPostBackTrigger ControlID="ButtonRemoveAll" EventName="Click"></asp:AsyncPostBackTrigger>
</Triggers>
</asp:UpdatePanel>
</td>
but it doesn't work for empty list box
How to check for this? please let me know
Update: answer rewritten.
The original asker found the following answer (see comments): you must set ValidateEmptyText to true.

Communicate between UpdatePanels ASP.Net

I have placed a treeView in One Update Panel and Each View in one update Panel
something like this
<UpdatePanel id="UP1">
<ContentTemplate>
<TreeView/>
</ContentTemplate>
</UpdatePanel>
<MultiView>
<UpdatePanel id="UP2">
<View1/>
</UpdatePanel>
Now I want to know how I can make sure When I click any node of TreeView respective Views should get displayed
Another way of approaching it would be to call UP2.Update() from the codebehind if you have code for the click event of the Treeview. Remember, UP2 needs to have its RenderMode set to Conditional for this to work. Hope it helps
Add an AsyncPostBackTrigger to the second updatePanel, so it gets updated when the TreeView Click event is fired.
<Asp:UpdatePanel id="UP2">
<View1/>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TreeView1" EventName="Click" />
</Triggers>
</Asp:UpdatePanel>
OK, Here Is a working Example.
The Markup:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<table style="width: 100%;">
<tr>
<td>
<asp:UpdatePanel ID="upTreeView" runat="server">
<ContentTemplate>
<asp:TreeView ID="TreeView1" runat="server"
onselectednodechanged="TreeView1_SelectedNodeChanged">
<Nodes>
<asp:TreeNode Text="GrandFather" Value="GrandFather">
<asp:TreeNode Text="Father" Value="Father">
<asp:TreeNode Text="Son" Value="Son"></asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
</ContentTemplate>
</asp:UpdatePanel>
</td>
<td>
<asp:UpdatePanel ID="upView" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TreeView1"
EventName="SelectedNodeChanged" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
</table>
The code behind:
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
Label1.Text = TreeView1.SelectedValue;
}

Resources