asp.net listbox problem - asp.net

foreach (Book b in o.list)
{
ListBox_Items.Items.Add(b.Title);
}
After I do this, the titles are now showing up in the listbox.
When I make a selection (Single Mode), ListBox_Items (Screen) is highlighting the row selected, but event SelectedIndexChanged is not triggering.
protected void ListBox_Items_SelectedIndexChanged(object sender, EventArgs e)
{
int i = ListBox_Items.SelectedIndex;
}
ID="ListBox_Items" runat="server" EnableViewState="False" Width="400px" Rows="25" onselectedindexchanged="ListBox_Items_SelectedIndexChanged"
Any ideas ?
Michael
Edit 1 : Thanks to everyone for helping out. Got it to work now. Anyway, I had to turn on EnableViewState to True too. Because I have a "Remove" button to remove items from the listbox control, if EnableViewState is False, whenever I clicked the Remove button, the listbox becomes empty again.

Add AutoPostBack="True" in your aspx tag

Try the following code.
<asp:ListBox ID="ListBox_Items"
runat="server"
EnableViewState="False"
Width="400px"
Rows="25"
OnSelectedIndexChanged="ListBox_Items_SelectedIndexChanged"
AutoPostBack="true"></asp:ListBox>

Do you have anything to make the page post back to the server?
You may need either a submit button, or you can add the property AutoPostBack="true" to your ListBox control.
See this MSDN Article for more information.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.autopostback.aspx

Related

ASP.Net AjaxControlToolkit CalendarExtender not updating Textbox in code behind

Have the following:
<asp:TextBox ID="txtStart" runat="server" Enabled="false"></asp:TextBox>
<asp:Image ID="ibDateS" runat="server" ImageUrl="../SystemImages/calendar.gif" ToolTip="Click to show calendar" AlternateText="Click to show calendar" CssClass="showpointer" />
<ajaxToolkit:CalendarExtender ID="ceStart" PopupButtonID="ibDateS" Format="dd/MM/yyyy" TargetControlID="txtStart" runat="server"></ajaxToolkit:CalendarExtender>
It all works ok on the DOM and the textbox gets updated with the new date BUT when I try to get the value in code behind i.e. txtStart.Text it still has the original value set on Page_Load.
Have I missed something?
EDIT:
TextBox set originally in Page_Load (yes contained in if(!IsPostback)):
txtStart.Text = DateTime.Now.ToString("dd/MM/yyyy");
Get it later like so:
DateTime dtStart = Convert.ToDateTime(txtStart.Text);
After a bit of research apparently its an issue with setting the textbox to readonly or enabled="false" on the page. Removing this and adding the following to page_load solved the problem:
txtStart.Attributes.Add("readonly", "readonly");
if you haven't use Page.IsPostBack property of page then plz use it and try to use you pageload code inside that. It seems that it may be the problem of Page.IsPostBack,Try for that
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// Your code for databind...
}
}
Hope you understand and works for you..
Enabled false preventing it to post the latest value.

Ajaxfileupload not working when dropdownlist is place inside an updatepanel in ASP.NET

When the page is loaded I have no problem using the ajaxfileupload and selecting files for the upload.
This works perfectly until I select a value of the first dropdownlist and the second dropdownlist inside the updatepanel gets populated via the partial postback. From this event on, the select button of the ajaxfileupload control is without function.
Is this a bug or how to overcome this issue?
set the dropdown's ID in PostBackTrigger of the update panel.
update:
<UpdatePanel ID="upd1">
<asp:DropDownList ID="ddl1" onselectedIndexChanged="ddl_changed" />
</UpdatePanel>
<UpdatePanel ID="upd2">
<Ajax:FileUploader />
</UpdatePanel>
protected void ddl1_changed(object sender, EventArgs e)
{
//your code
upd2.Update();
}
This might help you :)

Viewstate issue with first dropdownlist item selection

In my ASP.NET 4.0 website, which uses master pages, I've disabled viewstate sitewide in web.config:
<pages enableViewState="false" />
and am trying to enable it only when absolutely necessary.
I've run into an issue with a DropDownList control (no databinding going on, just hardcoded items):
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="150px" ViewStateMode="Enabled" EnableViewState="True">
<asp:ListItem>Chocolate</asp:ListItem>
<asp:ListItem>Strawberry</asp:ListItem>
<asp:ListItem>Vanilla</asp:ListItem>
</asp:DropDownList>
Even though I've enabled view state for this particular control, there's a problem with selecting the first item:
protected void DropDownList1_SelectedIndexChanged (object sender, EventArgs e)
{
TextBox1.Text = (sender as DropDownList).SelectedValue;
}
The expected result is that whenever "Chocolate" is selected TextBox1 will display "Chocolate". But what I'm seeing is that TextBox1 only changes when Strawberry or Vanilla is selected. In the example above I selected Strawberry and then Chocolate.
In other words, the DropDownList SelectedIndexChanged isn't firing when the first item is selected, but is firing when the second or third is selected.
Here are the property settings for the DropDownList:
I tried the same code starting from a blank project and the page works as expected. (Selecting the first item does fire the event).
Thanks in advance for any suggestions.
It appears you can't set <pages enableViewState="false" /> in the web.config or in any page directives for the ViewStateMode property to work.
Basically EnableViewState=false will override any and all ViewStateMode settings.
There doesn't appear to be a way to set the ViewStateMode property in the web.config at this point so it looks like you'll have to remove any EnableViewState properties from your application and set the ViewStateMode property to Disabled in all of your page directives.
I guess I have know what you say. You want show default value from dropdownlist when page load. If you want so you can do like this when dropdownlist load.
protected void DropDownList1_Load(object sender, EventArgs e)
{
TextBox1.Text = (sender as DropDownList).Text;
}

DropDownList's SelectedIndexChanged event not firing

I have a DropDownList object in my web page. When I click on it and select a different value, nothing happens, even though I have a function wired up to the SelectedIndexChanged event.
First, the actual object's HTML code:
<asp:DropDownList ID="logList" runat="server"
onselectedindexchanged="itemSelected">
</asp:DropDownList>
And this is that function, itemSelected:
protected void itemSelected(object sender, EventArgs e)
{
Response.Write("Getting clicked; " + sender.GetType().ToString());
FileInfo selectedfile;
Response.Write("<script>alert('Hello')</script>");
foreach (FileInfo file in logs)
{
if (file.Name == logList.Items[logList.SelectedIndex].Text)
{
Response.Write("<script>alert('Hello')</script>");
}
}
}
None of the Responses appear, and that portion of JavaScript is never run. I've tried this on the latest 3.6 version of Firefox, as well as Internet Explorer 8. This is being served from a Windows Server 2003 R2 machine, running ASP.NET with the .NET Framework version 4.
Set DropDownList AutoPostBack property to true.
Eg:
<asp:DropDownList ID="logList" runat="server" AutoPostBack="True"
onselectedindexchanged="itemSelected">
</asp:DropDownList>
try setting AutoPostBack="True" on the DropDownList.
I know its bit older post, but still i would like to add up something to the answers above.
There might be some situation where in, the "value" of more than one items in the dropdown list is duplicated/same. So, make sure that you have no repeated values in the list items to trigger this "onselectedindexchanged" event
Add property ViewStateMode="Enabled" and EnableViewState="true"
And AutoPostBack="true" in drop DropDownList
Also make sure the page is valid.
You can check this in the browsers developer tools (F12)
In the Console tab select the correct Target/Frame and check for the [Page_IsValid] property
If the page is not valid the form will not submit and therefore not fire the event.
For me answer was aspx page attribute, i added Async="true" to page attributes and this solved my problem.
<%# Page Language="C#" MasterPageFile="~/MasterPage/Reports.Master".....
AutoEventWireup="true" Async="true" %>
This is the structure of my update panel
<div>
<asp:UpdatePanel ID="updt" runat="server">
<ContentTemplate>
<asp:DropDownList ID="id" runat="server" AutoPostBack="true" onselectedindexchanged="your server side function" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
Instead of what you have written, you can write it directly in the SelectedIndexChanged event of the dropdownlist control, e.g.
protected void ddlleavetype_SelectedIndexChanged(object sender, EventArgs e)
{
//code goes here
}

ASP.Net repeater Item Command not getting fired

OK, I've used repeaters literally hundreds of times without problems but something has gone awry today. I have a repeater and I'm subscribing to the itemCommand event, but when my command runs, the page posts back but the event isn't fired.
To get around this I'm having to do my databinding on each postback.
My repeater looks like this:
<asp:Repeater id="MyRepeater" runat="server" onitemcommand="MyRepeater_ItemCommand">
<ItemTemplate>
<li>
<asp:Label id="Label" runat="server" />
<asp:LinkButton id="LinkButton1" runat="server" commandname="Complete" commandargument='<%# Eval("MyID") %>' text='<%# Eval("Title") %>' />
</li>
</ItemTemplate>
</asp:Repeater>
and my codebehind like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetupPage();
}
}
private void SetupPage()
{
// Do other stuff
MyRepeater.DataSource = Repository.GetStuff()
MyRepeater.DataBind();
}
protected void MyRepeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
// Do all my stuff here
}
MyRepeater_ItemCommand is not getting called unless I comment out the if (!IsPostBack) line. Once that is commented out and the repeater is getting databound on each postback it works OK. I've done this in so many other pages but on this on it just doesn't seem to work.
Anyone else come across this behaviour or have a solution?
Most likely, you have disabled ViewState for the page.
The reason is that when you execute the postback, all the controls in the repeater are rebuild from the data in the viewstate normally. Then the object that should receive the event is identified based on the ID of the control, and the event is routed.
If you disable the viewstate, the control tree is not rebuild during postback, and therefore the control that should receive the event does not exist in memory. So the event dies.
If you really want to disable the viewstate, but still want to receive the event, I have a workaround (and it's not dirty at all). I've long been thinking about writing a blog entry about it, so if you want, I can take a bit time off my normal chores, and describe it.
Edit: The workaround is described here: http://petesdotnet.blogspot.dk/2009/08/asp.html
Remove if (!IsPostBack) as this is preventing the repeater from rebinding,
and the item command event could not find the row after postback.
I have the same problem and aside from using update panel, I have a required field validator in my modal. I found out that the LinkButtons in my repeater triggers the requiredFieldValidor event and then I added CausesValidation="false" in the LinkButtons of my repeater. Works as expected.
I have this problem in a repeater when I use ImageButton ...
I have search the net for this solution when LinkButton work, but not ImageButton ...
Then I think, LinkButton work? so i will use it :)
<asp:LinkButton CommandName="Filter" CommandArgument='<%# Eval("ID") %>' Text="" runat="server" >
<asp:image imageurl='<%#Eval("Img") %>' runat="server"/>
</asp:LinkButton>
So, the image is inside the <A> tag
have fun :)
I removed PostBackUrl property in linkbutton and ItemCommand fired. I think postback runs first.
That may be you have set Validations on your page. So set an new attribute, causevaliation = "false" to Link button. M sure it will solve the problem
I had a similar issue - turned out some discreet validation controls were firing elsewhere on the page. It only took me a day to figure it out ...
I am not positive about this, but you might have to set the CommandName and optionally CommandArgument properties for the button causing the ItemCommand event. Otherwise ASP.NET would assume that there is no button on the page, which you'd like to fire the event. You can try that.
Plus, if you're not differentiating between command names, why not use each button's Click event instead? Just subscribe to those in the repeater's ItemCreated or ItemDataBound.
Try using Page_init instead of Page_load and that should fix the problem.
Try this:
protected void Page_Load(object sender, EventArgs e)
{
SetupPage();
}
If you use nested-repeater, you should rebind your inner repe
Here Is the code you have to use in code behind..
after PageLoad event,
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
// Bind Your Repeater here
rptUser();
}
now you can fire your Itemcommand..if you get Output please mark answer as right thanks
One other thing that it could be (as it just happened to me): if your databind takes place when your page is prerendered, it won't process the item command. Switch it to load or init and you'll be fine.

Resources