this might seem like kind of a weird request, but I'm looking to prevent autopostback for selected index change until the user clicks outside of the listbox, instead of when the user selects/deselects an item.
Is such a thing possible? The reason I ask is I have the following plug in used for a multiselect dropdown.
As it is right now, autopostback closes the dropdown each time a user select an item, and the index is successfully changed. However, if I turn autopostback off, it stays open to allow the user to select multiple items, but it won't register as selected.
If anyone could suggest a means of doing this I would be eternally grateful. Thanks so much in advance.
I'll post my asp.net and c# codebehind for the multiselect below.
ASP.NET
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:ListBox ID="DependenciesListBox" runat="server" SelectionMode="Multiple" Width="300px" CssClass="search-box-sel-all" multiple="multiple" placeholder="Select Child Dependencies" ToolTip="Items that depend on this currency item" AutoPostBack="True" OnSelectedIndexChanged="DependenciesListBox_SelectedIndexChanged"></asp:ListBox>
</ContentTemplate>
</asp:UpdatePanel>
C#
protected void DependenciesListBox_SelectedIndexChanged(object sender, EventArgs e) {
ChildLabel.Text = "";
foreach (ListItem item in DependenciesListBox.Items) {
if (item.Selected)
ChildLabel.Text += item.ToString() + ", ";
}
}
<asp:ListBox ID="DependenciesListBox" runat="server" SelectionMode="Multiple" Width="300px" CssClass="search-box-sel-all" multiple="multiple" placeholder="Select Child Dependencies" ToolTip="Items that depend on this currency item" AutoPostBack="False" onchange="clientListChanged(this);" OnSelectedIndexChanged="DependenciesListBox_SelectedIndexChanged"></asp:ListBox>
//Java script code
function clientListChanged(el)
{
if ( el.value == '1' ) // for all indexes you dont want to post back add condition
{
return false;
}
return true; //else postback
}
Use onblur event for the event when user clicks outside the box and do a post back. I'm not sure if it allows html markup with list box; if not add via code behind using attributes.add for the list box. Then you can probably use __doPostBack(elementName.ClientId) in asp quotes to post to server
Note: I have not tested the code for errors or syntax issues. I just wrote it down here in editor. So please fix any errors you find.
I don't know perfect answer,
But this works for me, i hope it will work for you
Use updatepanel triggers
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="DependenciesListBox"
EventName="SelectedIndexChanged
="SelectedIndexChanged" />
</Triggers>
Related
I'm having a really strange problem where my code works with a breakpoint set in the code, but when I remove the breakpoint, parts of the code stop functioning.
I'm trying to make a textbox select all text on focus; I want it to automatically focus, selecting all text, when a search is performed.
I have a texbox (groupSearchTextbox), button (groupSearchButton), and a listbox which is inside of an update panel, inside of a panel with a default button specified:
<asp:Panel ID="groupPanel" runat="server" CssClass="listContainer" DefaultButton="groupSearchButton">
<h2>User Groups</h2>
<div class="searches">
<asp:TextBox ID="groupSearchTextbox" runat="server"></asp:TextBox>
<asp:Button ID="groupSearchButton" runat="server" Text="Search" OnClick="groupSearchButton_Click" />
<asp:Button ID="showAllGroupsButton" runat="server" Text="Show All" OnClick="showAllGroupsButton_Click" CssClass="right" />
</div>
<asp:UpdatePanel ID="groupUpdate" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:ListBox ID="groupListbox" runat="server" CssClass="list"></asp:ListBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="groupSearchButton" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="showAllGroupsButton" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>
I also have a jquery function to make the textboxes select on focus:
$('.searches input[type=text]').focus(function () {
$(this).select();
});
and onclick of groupSearchButton I have the following function to remove items from the listbox if they are not results of a search, and focus on the textbox:
protected void groupSearchButton_Click(object sender, EventArgs e) {
fillGroups(); //Sets the listbox to the original list
string searchString = groupSearchTextbox.Text;`
for (int i = groupListbox.Items.Count - 1; i > 0; i--) {
string itemName = groupListbox.Items[i].ToString();
if (!itemName.ToLower().Contains(searchString.ToLower())) {
groupListbox.Items.Remove(groupListbox.Items[i]);
}
}
groupSearchTextbox.Focus();
}
When I click the groupSearchButton, everything works as expected. I get my results and the groupSearchTextbox gets focus with the text selected.
When I press enter while focused on the textbox, making use of the panel's default button attribute, I get my results but the text in the textbox is not selected.
The strange part is, if I put a breakpoint where I set the focus in the groupSearchButton_Click method, the above method of pressing enter in the textbox works properly and the text is selected.
Any ideas what might be going on here?
Edit:
So I'm pretty sure that the issue is that the textbox needs to lose focus in order for it to select the text when it focuses again. That would explain the actual clicking of the button working, as well as (I believe) the breakpoint issue, since the textbox would lose focus when Visual Studio is displayed.
I came up with a fairly hacky jquery fix for this, but would still love to know if there's a decent way to handle this properly
If you want to focus on only one text box then you can use
<asp:Panel ID="groupPanel" runat="server" CssClass="listContainer" DefaultButton="groupSearchButton" defaultfocus="groupSearchTextbox">
on your panel
You can use this in Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
groupSearchTextbox.Focus();
}
}
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
I have one dropdownlist in my form in .aspx File.
Once I select "1", then next time if I redirect to same page then "1" should be
unselectable or Hidden for making sense that I have selected "1" previously because I have large items in my example.
Disabling any particular item in a dropdownlist is not possible.
Alternate:
You can use a BulletedList Server Control and use its Enable = False property to disable any particular item and all user can see that item as disabled.. here is a design time example..
<asp:BulletedList ID="BulletedList1" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem Enabled="False">3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:BulletedList>
Took from here
You can use a hidden field to store which item was selected in code behind and disable that item using the following code.
//Code Starts
$(document).ready(function() {
$('#ddlList option:contains("HTML")').attr("disabled","disabled");
});
//Code Ends
You can track your selection using Cookies,
on the "onChange" event of your dropdown list, create a cookie with the selected value,
then on body "onLoad" u can check and disable the items that are there in Cookie.
Or you can hold your selected values in a hidden field
To remove the last selected item in your dropdownlist (DDL), considering the following code for your DDL form, you can use the onselectedindexchanged event of your DDL (that is raised when you click on one of your DDL items):
<asp:DropDownList ID="_DDL" runat="server"
onselectedindexchanged="_DDL_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
And put on the code behind the RemoveAt() method to remove the selected Item from your DDL:
protected void _DDL_SelectedIndexChanged(object sender, EventArgs e)
{
int ItemToRemove = _DDL.SelectedIndex;
_DDL.Items.RemoveAt(ItemToRemove);
}
Hope this will be helpful :)
I have an ASP.NET/C# application.
The application containsa PlaceHolder inside an UpdatePanel.
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="LinkButton1" EventName="Click"/>
<asp:AsyncPostBackTrigger ControlID="LinkButton2" EventName="Click"/>
<asp:AsyncPostBackTrigger ControlID="LinkButton3" EventName="Click"/>
</Triggers>
<ContentTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible="false">
</asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
Basically I load controls in the placeholder depending on what LinkButton has been clicked using a function
LoadControls("ControlName");
I want to implement the ability for the user to use the Browser Back Button
in order to navigate through the controls. so I used AJAX History like this:
I save the current control ID in a session variable
Session["current"]
I enable History in scriptmanager and add an event handler
<asp:ScriptManager ID="ScriptManager" runat="server" EnableHistory="True" onnavigate="ScriptManager_Navigate" />
(The event handler will be covered below)
First I will create a new history point in the event handler of the Linkbuttons (they all use the same handler)
string ControlId=Session["current"].ToString();
if (ScriptManager.IsInAsyncPostBack && !ScriptManager.IsNavigating)
{
ScriptManager.AddHistoryPoint("Hist", ControlId);
}
then in the Scriptmanager navigate handler
protected void ScriptManager_Navigate(object sender, HistoryEventArgs e)
{
string Controlid = "";
if (e.State["index"] != null)
{
Controlid = e.State["Hist"].ToString();
LoadControls(Controlid );
}
}
When I load the controls I can navigate back and forward and everything is working fine
Here is the problem:
1) first problem
The first time I click on "Back". the history returns 2 steps and not one (after that it works normaly.) It is like the last history is not saved.
2) second problem
After "Backing" to the first step I can do an extra "Back" where the e.State["Hist"] will be null and give me an error. how can I disable the "Browser Back" button when the e.State["Hist"] will be null?
Thank you very much for any help. I hope I was clear.
If you need more info/code I will gladly provide them
Never mind. it was just a bug. the Session["Current"] was saving the previous control and not the current one. It is now fixed. I will leave this question if somebody needs a small example for creating ajax history.
Hi I've an HTML select control in my ASP.NET page and I made it as runat = "server" and now I tried to add some list items to it dynamically. something like below code
var list = document.getElementById('<%=list1.ClientID%>');
var newListItem = document.createElement('OPTION');
newListItem.text = "Emp1";
newListItem.value = "e101";
list.add(newListItem);
<asp:Panel ID="pnlemp" runat="server"
Style="display: none;"
CssClass="modalPopup"
width="700px" Height="350px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<select id="list1" multiple="true" name="list1" runat="server">
</select>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
and now when I try to access this from my code like list1.Items.Count it is showing 0.
Anything wrong in this?
Thanks in advance
When you modify the html in a client side script, the viewstate (which keeps track of all the controls) doesn't update . That results in that when you make a postback the new items isn't "there" .
Sometimes there's a __doPostBack() javascript which forces a postback, but I'm not sure it'll work .
For solution to this problem either add items dynamically through server side code or
dont bring server side into the picture and handle everything via javascript.
Items added via javascript will not be persisted by asp.net. further more you may recieve a "Invalid Callback or PostBack Argument" Exception for the same because it will not understand from where these listitems(options) have been added in the Select.
This is my link button:-
<asp:LinkButton ID="lnk1" Text="Set as Default" runat="server" Visible="false" OnClick="lnk1_OnClick"></asp:LinkButton>
In Code Behind I am simply making it visible
lnk1.Visible = true;
I have checked the IDs over n over..whats wrong ? Intellisense wont detect it either..I am doing something really silly..just cant figure what it is ..help!
I even restarted Visual Studio..still same error
Is the contol part of another control template? E.G. part of a repeaters ItemTemplate etc?
Update:
Since OP has said it's part of a repeaters ItemTemplate, just thought I'd explain what to do (Even though OP has sorted it)
You need to call FindControl on the Repeater, or Controls.OfType() depending on the situation, to get the control.
ASP:
<asp:Repeater runat="server" ID="rptrTest">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtBxName" />
<asp:CheckBox runat="server" ID="chkBx1" />
<asp:CheckBox runat="server" ID="chkBx2" />
</ItemTemplate>
</asp:Repeater>
C#
IEnumerable<CheckBox> chkBoxes = rptrTest.Controls.OfType<CheckBox>();
TextBox txtBxName = (TextBox)rptrTest.FindControl("txtBxName");
What I'll often do for commonly used controls (though wether it's a good idea or not I'm sure someone will now let me know), is create a member which executes this code.
private TextBox _txtBxName;
public TextBox txtBxName {
get {
if (_txtBxName == null) {
_txtBxName = (TextBox)rptrTest.FindControl("txtBxName");
}
return _txtBxName;
}
}
Sometimes the designer class is not re-generated correctly. Things you can try:
select the line, cut, save, rebuild,
paste back, save
delete the designer
.cs file, right click the aspx,
convert to web application -> this
will generate the designer class from
scratch
Since I do not have the rights to comment; so ...
In which event you are making that item visible? try doing that in PageLoad.
Can you show your markups?
Alternatively, you can try to Find the control.