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.
Related
When I use Button inside Update panel it doesnot fire click event but outside the update panel it works.
here is the code
<asp:UpdatePanel ID="updatePanel2" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:Button ID="btnBlock" class="Button" Text="BlockCalls" runat="server"
onclick="btnBlock_Click" Enabled="True" Width="100px" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnBlock" />
</Triggers>
</asp:UpdatePanel>
code for button is
protected void btnBlock_Click(object sender, EventArgs e)
{
CtiWS.CtiWS CtiWS1 = new CtiWS.CtiWS();
Response.Write("<script>alert('"+Convert.ToString(Session["BlockCalls"])+"')</script>");
if (btnBlock.Text == "BlockCalls")
{
btnBlock.Text = "UnBlockCalls";
CtiWS1.BlockCalls("", "", HttpContext.Current.Session["HOSTID"].ToString()); //server block calls
}
else
{
btnBlock.Text = "BlockCalls";
CtiWS1.BlockCalls("", "", HttpContext.Current.Session["HOSTID"].ToString()); //server unblock calls
}
}
Try this
set ChildrenAsTriggers to true and add EventName="Click" in asp:AsyncPostBackTrigger
<asp:UpdatePanel ID="updatePanel2" runat="server" UpdateMode="Conditional"
ChildrenAsTriggers="true">
<ContentTemplate>
<asp:Button ID="btnBlock" class="Button" Text="BlockCalls" runat="server"
onclick="btnBlock_Click" Enabled="True" Width="100px" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnBlock" EventName="Click"/>
</Triggers>
</asp:UpdatePanel>
This is the code that I have:
<asp:UpdatePanel runat="server" ID="UPnlParent" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<div><h3>title</h3></div>
<asp:UpdatePanel runat="server" ID="UPnlChild" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox runat="server" ID="Tb1"></asp:TextBox>
<asp:LinkButton runat="server" ID="Btn1" Text="Create" OnClick="Create" />
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</asp:UpdatePanel>
In the code behind I have a function for an event "Create"
protected void Create(object sender, EventArgs e)
{
string textFromPostBack= Tb1.Text;
//do something...
}
The string is empty.
Thank you for your assistance.
You should add the button as a trigger to the update panel if you want it to trigger a post back:
<asp:UpdatePanel ...>
<ContentTemplate>
blablabla
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Btn1" />
</Triggers>
</asp:UpdatePanel>
More details :Click
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!
If there is UpdatePanel inside another UpdatePanel, and in the inner UpdatePanel there is a button, I want when clicking this button, only refresh the inner UpdatePanel. how ?
IN the innerupdate panel set the updatemode to conditional and set the outerupdatepanel childrenastriggers property to false. In the inner update panel add a postbacktrigger and set it to the button which will cause the postback. Something like this
<asp:UpdatePanel ID="parentup" runat="server" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:UpdatePanel ID="chidlup" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Button ID="btn" runat="server" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btn" />
</Triggers>
</asp:UpdatePanel>
</ContentTemplate>
</asp:UpdatePanel>
#Waqar Janjua is right.
But you don't have to set the ChildrenAsTriggers to false, sometimes it is more convenient to leave it as true.
Set in both of your updatepanels the attribute UpdateMode="Conditional" (leave the ChildrenAsTriggers as its default true). then between the: to the add the trigger to your button as Janjua said:
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn" />
</Triggers>
When the UpdateMode is not conditional each updatepanel would update it.
This code helps you:Here is Source
<asp:ScriptManager ID="script1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<asp:Label ID="lblTime" runat="server" ForeColor="Red"></asp:Label>
<asp:Button ID="buttonOuter" runat="server" OnClick="buttonOuter_Click" Text="What is the time?" />
<asp:UpdatePanel ID="up2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTime2" runat="server" ForeColor="Blue"></asp:Label>
<asp:Button ID="buttonInner" runat="server" OnClick="buttonInner_Click" Text="What is the time?" />
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="buttonOuter" EventName="Click"/>
</Triggers>
</asp:UpdatePanel>
And here is code behinde:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void buttonInner_Click(object sender, EventArgs e)
{
up2.Update();
lblTime2.Text = DateTime.Now.Second.ToString();
}
protected void buttonOuter_Click(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.Second.ToString();
}
If UP1 is the outer UpdatePanel , and UP2 is the inner, and you want to prevent the outer to be updated with inner's Button(for exp:Btn):
UP1.UpdateMode="Conditional"
UP1.ChildrenAsTriggers= "False"
Also you shouldn't add the Btn as a Trigger in UP1(like The following code)
<asp:UpdatePanel ID="UP1" runat="server" ....>
<ContentTemplate>
....
</ContentTemplate>
<Triggers>
<asp:Trigger ControlID="btn" EventName="Click"/>
</Triggers>
</asp:UpdatePanel>
Q:
I have a RequiredFieldValidator on my page ,when i click the button which is (postback triggered) it makes the postback, although the field which is required wasn't entered !
What's the reason to this behavior?
My .aspx:
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="btn_Search" />
</Triggers>
<ContentTemplate>
<asp:ImageButton ID="btn_Search" runat="server" ImageUrl="~/Images/save.png"
OnClick="btn_Search_Click" OnClientClick="return postbackButtonClick();" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:DropDownList ID="ddl_department" runat="server" Width="200px" OnSelectedIndexChanged="ddl_department_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="ddl_department"
Display="Dynamic" ErrorMessage="*" InitialValue="-1"></asp:RequiredFieldValidator>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddl_study" runat="server" Width="200px">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="ddl_study"
Display="Dynamic" ErrorMessage="*" InitialValue="-1"></asp:RequiredFieldValidator>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddl_department" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<script type="text/javascript">
var updateProgress = null;
function postbackButtonClick() {
updateProgress = $find("<%= UpdateProgress1.ClientID %>");
window.setTimeout("updateProgress.set_visible(true)", updateProgress.get_displayAfter());
return true;
}
</script>
Remove return clause from LinkButton's OnClientClick property value: OnClientClick="postbackButtonClick()" and rewrite postbackButtonClick as below:
function postbackButtonClick() {
Page_ClientValidate();
if (Page_IsValid) {
updateProgress = $find("<%= UpdateProgress1.ClientID %>");
window.setTimeout("updateProgress.set_visible(true)", updateProgress.get_displayAfter());
return true;
}
}