I have an asp.net page which has a web control.
This web control displays a Treeview, TextBox and a button.
For the treeview i do on Page_Load:
if (!Page.IsPostBack) {
BindTreeView();
}
This is my TreeView definition:
<asp:TreeView ID="TvwData" runat="server"
BackColor="White" ShowCheckBoxes="Leaf" Width="99%"
ExpandDepth="3" AutoGenerateDataBindings="false"
onselectednodechanged="TvwData_SelectedNodeChanged"
EnableViewState="true"
>
The TreeView is shown correctly.
However, when I click the submit button, the following happens:
Page reloads, textbox still displays my text which is OK.
My Treeview doesn't display any data at all.
When debugging, TvwData.CheckedNodes doesn't contain any data while I'm sure there are treeview items checked.
I really tried my best to found a solution on major ASP.NET forums, but I'm clueless at this moment.
Thanks in advance!
Try the following:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
BindTreeView();
}
Furthermore, remove BindTreeView(); from Page_Load.
Related
I have radlistview. I add datasource to listview when page_load. But when I clicked I button in same page. I lost datasource of listview. Why? how can I fix it?
Try putting your listview databinding code inside something like this.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
myListview.DataSource = myDatasource;
myListView.DataBind();
}
}
You may also want to consider using UpdatePanels if you wanting to only postback a part of the page, rather than the whole page.
I found the solution. I added EnableViewState="true" property and problem fixed. Like this
<telerik:RadListView ID="lvDSTableSelection" runat="server" AllowMultiItemSelection="true" OnSelectedIndexChanged="lvDSTableSelection_SelectedIndexChanged" EnableViewState="true" >
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 :)
This seems like an elementary issue but it has me stumped.
I have a main page which loads a custom control (.ascx) on page_load.
This custom control has two fields. One of them being a dropdownlist and the other being a text box. When the dropdownlist changes value it triggers a post back, some logic is executed server side and a value is generated for the textbox. I stepped through the code and the value is created and assigned correctly. However, when the page is rendered the value does not change.
If I change the textbox value to "QQQ" and trigger the postback, "QQQ" stays in the textbox so I can verify viewstate is working.
Is there any reason why the generated value is not being displayed in the form on postback. This process works fine on the initial page load.
.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
string ascxPath = #"~/_CONTROLTEMPLATES/TRC_BITS.ascx";
TRC_BITS control = Page.LoadControl(ascxPath) as TRC_BITS;
phForm.Controls.Add(control);
}
.ascx
<asp:TextBox ID="message" runat="server" TextMode="MultiLine" /><br/>
<asp:DropDownList ID="year" runat="server" AutoPostBack="true">
<asp:ListItem Text="2011">2011</asp:ListItem>
<asp:ListItem Text="2012">2012</asp:ListItem>
<asp:ListItem Text="2013">2013</asp:ListItem>
</asp:DropDownList>
.ascx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
year.SelectedValue = DateTime.Now.Year.ToString();
}
if (year.SelectedValue == 2012)
message.Text = "ABC";
else
message.Text = "XYZ";
}
Because you're adding these controls to the page dynamically, you need to assign an ID to the user control. Make sure to assign the same ID to the controls every time the page is posted back, and the fields will be repopulated from ViewState.
Also, as Shai suggested, it would be more appropriate if you loaded the controls during OnInit instead of Page_Load. The situation is a little different with user controls, but in general you want to add your dynamic content before the LoadViewState method is executed.
If you're looking for something to take the pain out of persisting dynamic content, I would suggest taking a look at the DynamicControlsPlaceHolder.
Because you are adding the controls dynamically, you need to add them during the page's oninit event.
Try it, believe me. Go for it. Yalla.
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;
}
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