Telerik RadDock for ASP.NET AJAX initialization problem - asp.net

I've run into a problem while using Telerik RadDock control for asp.net ajax. I'm generating the dock layout dynamically and there are multiple page configurations retrieved from the database. The thing is that I need to use post data before handling LoadDockLayout event, because there is a dropDownList which gives the ability to select one of avialable configurations.
I may be missing something, but it appears to me that LoadDockLayout event is fired before PreLoad event (it looks like the LoadDockLayout is fired before ProcessPostData method call) and before dropDownList selectedIndexChanged event, so I don't have the value I need to initialize dock layout properly.
I was thinking of converting my dropDownList to client side control and passing the selected value in query string, but it seems for me to be an ugly solution. Any advices would be appreciated.
Thanks in advance.

The RadDockLayout.LoadDockLayout event is fired at Page.InitComplete before any post data can be handled. If you want to load different controls based on a DropDownList selected item, you can do it by checking the parameters sent from the client. Here is an example on how you can retrieve them:
ASPX
<asp:Label ID="Label1" EnableViewState="false" runat="server" />
<asp:DropDownList ID="Dropdownlist1" runat="server" AutoPostBack="true">
<asp:ListItem Text="text1" Value="val1" />
<asp:ListItem Text="text2" Value="val2" />
<asp:ListItem Text="text3" Value="val3" />
<asp:ListItem Text="text4" Value="val4" />
</asp:DropDownList>
C#
protected void Page_Init(object sender, EventArgs e)
{
if (IsPostBack)
{
var selValue = this.Request.Params.Get("Dropdownlist1");
Label1.Text = "EVENTTARGET: " + this.Request.Params.Get("__EVENTTARGET") + "<br/>Value sent: " + selValue;
}
}

Related

AutoPostBack and parameter value after browser Back

When using postback and then browser Back button, previous content is shown correctly, but the parameter that triggered postback has the latest value - it is not returned to the value it had before it triggered postback. This creates an invalid state.
In the example, dropdown has AutoPostBack set to true.
After I type "a2" and change dropdown value to "text2":
After I type "a3" and change dropdown value to "text3":
After Back:
Turning of browser cache programmatically didn't help, as it gives "Document Expired" on browser Back action.
After going Back, I wish that parameters match the content which is generated with those parameters, specifically the ones that are AutoPostBack. Is there a desired way of getting that behavior?
Code:
aspx
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem Text="text1" />
<asp:ListItem Text="text2" />
<asp:ListItem Text="text3" />
</asp:DropDownList>
<div>Server content - <asp:Label ID="ServerContent" runat="server" /> </div>
aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
ServerContent.Text = string.Format(
"received values: {0}, {1}", TextBox1.Text, DropDownList1.SelectedValue);
}
It is still not very clear what you are trying to do. The DropDownList changes, the server does a form post, you get a response and then you want that response in the previous state (before form post)? Seems like a weird rquirement. If a user goes back because he made a mistake, he changes the value and then does a PostBack again. No harm done.
But if you really want to set the value to it's default, you need to set the value of the DropDownList with JavaScript when the page has not done a PostBack. Take a look at my snippet below, it gives the desired results. It sets the SelectedIndex at 2 when the page is loaded, but does not when a PostBack is performed so the new value is still visible. Now when the user goes back the script is still present and will set the DropDownList to it's default value again.
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="aaa" Value="aaa"></asp:ListItem>
<asp:ListItem Text="bbb" Value="bbb"></asp:ListItem>
<asp:ListItem Text="ccc" Value="ccc"></asp:ListItem>
</asp:DropDownList>
Code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "setValue", "document.getElementById('" + DropDownList1.ClientID + "').selectedIndex = 2;", true);
}
}
I had to keep values for certain fields in additional hidden fields and use them on the client. The same example with "solution":
aspx
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem Text="text1" />
<asp:ListItem Text="text2" />
<asp:ListItem Text="text3" />
</asp:DropDownList>
<asp:HiddenField runat="server" ID="DropDownList1ValueCarrier" ClientIDMode="Static" />
<div>Server content - <asp:Label ID="ServerContent" runat="server" /> </div>
aspx.cs
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
DropDownList1ValueCarrier.Value = DropDownList1.Text;
}
js
$('#DropDownList1').val($('#DropDownList1ValueCarrier').val());

Why check box is checked after postback

I have next code,
<form id="form1" runat="server">
<asp:Label runat="server" ID="Label1" EnableViewState="false" />
<asp:CheckBox runat="server" ID="Check1" EnableViewState="false" Checked="false" />
<asp:Button runat="server" ID="Button1" Text="Button1" />
</form>
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
Label1.Text = "Label value";
Check1.Checked = true;
}
Why on postback text is despairing but check box still checked ?
Thanks
Citing references from this MSDN article - Understanding ASP.NET View State by Scott Mitchell doc:
Page Life cycle Stage 3 - Load Postback Data
It is a common misconception among developers that view state is
somehow responsible for having TextBoxes, CheckBoxes, DropDownLists,
and other Web controls remember their values across postback. This is
not the case, as the values are identified via posted back form field
values, and assigned in the LoadPostData() method for those controls
that implement IPostBackDataHandler.
Because the checkbox picks up the state from the form data, so that it remains checked when it is recreated.
The viewstate is only needed to remember what state the checkbox had before, so that the server side Change event works.

How can I create an ASP.NET AJAX UpdatePanel that refreshes a Repeater when a DropdownList control has the Selected Item changed?

I have a simple problem. I have a dropdown list and a databound repeater. When the user changes the selection in the dropdown list I would like to reload the repeater. I would like to use ASP.NET AJAX so that this does not refresh the entire page. I have read the ASP.NET AJAX documentation over and over but can not find a clear explanation of how to do this. Here is part of my ASPX page
<asp:ScriptManager id="smScriptManager" runat="server" />
<asp:DropDownList id="ddlLocations" runat="server" AutoPostback="true" OnSelectedIndexChanged="ddlLocations_SelectedIndexChanged">
<asp:ListItem Text="Location 1" Value="1"></asp:ListItem>
<asp:ListItem Text="Location 2" Value="2"></asp:ListItem>
<asp:ListItem Text="Location 2" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:UpdatePanel id="upUpdatePanel" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlLocations" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<asp:Repeater id="rptItems" runat="server">
<ItemTemplate>
... data bound fields for each location go here ...
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
In my code behind I have a separate data binding function that binds the repeater. Currently I call it on Page Load only if Page.IsPostBack = false. So the page loads fine the first time and the Repeater is loaded with all of the data for Location 1 (since it is selected). I also have code in the "ddlLocations_OnSelectedIndexChanged" function that calls the data binding function. In fact, if I strip out all of the AJAX/UpdatePanel stuff the page works fine (although it does a complete page refresh).
With the AJAX stuff, when the user selects Location 2 or Location 3 I get the following JS error:
Message: Sys.WebForms.PageRequestManagerServerErrorException: StartIndex cannot be less than zero.
Parameter name: startIndex
Line: 4723
Char: 21
Code: 0
URI: http://localhost/ScriptResource.axd?d=sTWt9V9Pb7V99ft1kYaE90l3bO7maFff4srx6ham1O6ideYDjPbZU5dv5QJjmzBCPxFQIGrdN2PIuOG7y3evLrZF4ETSZ2de6iwiLo9yZ4w0RvtADl-5JZXm0C_1eyn2F6p6fNX5ZDkxi6yJHF0czKc2lWjUlRrkShaVa4T1l8BPah6A0&t=ffffffff9bdfdd38
Anyone have any ideas why this is happening? Am I doing something wrong? I feel like I don't quite understand how the UpdatePanel works. I get the feeling that I should first set up the page to work without the UpdatePanel/AJAX stuff (and do a complete page refresh). Then add in the UpdatePanel/AJAX stuff and somehow the code magically figures out that I just want the updated Repeater HTML. Or something like that. Any ideas?
UPDATE:
Here is how my back end code works
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
this.BindData();
}
}
protected void BindData()
{
this.BindDropDownList();
this.BindRepeater();
}
protected void BindDropDownList()
{
...call code for binding drop down list...
(ddlLocations is actually databound - I simplified it above for purposes of this post)
}
protected void BindRepeater()
{
...call code for binding the repeater...
}
protected void ddlLocations_SelectedIndexChanged(object sender, EventArgs e)
{
this.BindRepeater();
}
Thanks,
Corey

asp.net set textbox control in panel contol

I want to set the textbox contol located in the panel control via code
I know to retrieve the inputted value in the textbox control:
string myVal = Request.Form["txtResult"];
I want to set the txtResult.text = "some text";
makeup snippet:
<asp:Panel ID="Panel1" runat="server" Style="display: none" Width="233px">
<asp:TextBox ID="txtResult" runat="server" AutoPostBack="True"></asp:TextBox>
<br />
<div align="center">
<asp:Button ID="OkButton" runat="server" Text="OK" />
<asp:Button ID="CancelButton" runat="server" Text="Cancel" />
</div>
</asp:Panel>
txtResult is not available within code, I tried to see if it is available in the page_load, it's not
texReults was a typo, its txtResult, I updated the ID
the intellisense does not recognize any cntr by the name txtResult
its a new web application and the panel visibility=True
maybee this wil help, above the snipet, I use ScriptManager from the AJAX Exstension
I am aware of he Asnchronius affects, partial potback, etc.
It's a managed control, you should be able to set it on the Page_Load event:
protected void Page_Load(object sender, System.EventArgs e)
{
txtResult.Text = "some text";
}
Update: Based on your update, there are a couple of things that you would need to check:
Spelling: Are you sure you're spelling the control name correctly?
Its ID in your code is "txtResults", but you're referencing it as
"txtResult".
Designer: Did you copy the aspx page or bypass VS in some way for this page? If so check the .designer file for the reference to the control: i.e. "Page1.aspx.designer.cs"
Visibility: Is the Panel control's visibility set to true? If not, then it won't render the controls that are contained within it.
Update 2: If you're doing this through scriptmanager, then I highly recommend that you read through this: http://www.wrox.com/WileyCDA/Section/Using-the-ASP-NET-AJAX-ScriptManager.id-305492.html

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
}

Resources