I am having a problem updating a listview's databinding when inside a update panel. The listview is to do a databind and return some user names.
I have tested the databind by triggering it from an asp button and it works fine like so.
protected void getFacebookFriends(object sender, EventArgs e)
{
List<testResult> getFriends = (from i in lqDataContext.test(base.UserId) select i).ToList();
lvFacebookFriends.DataSource = getFriends;
lvFacebookFriends.DataBind();
}
when I try do this via ajax using
<telerik:RadAjaxPanel ID="updateFriends" runat="server" OnAjaxRequest="updateFriends_AjaxRequest">
function invokeAjaxRequest() {
$find("<%= updateFriends.ClientID%>").ajaxRequestWithTarget("<%= updateFriends.UniqueID %>", 97);
}
protected void updateFriends_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
List getFriends = (from i in lqDataContext.test(base.UserId) select i).ToList();
lvFacebookFriends.DataSource = getFriends;
lvFacebookFriends.DataBind();
updateFriends.EnableAJAX = false;
}
I can see in debug that I get through updateFriends_AjaxRequest without error but the ui has not change.
Thanks for helping me understand this.
Mark
Just wrap it in a regular update panel. The postback will be caught and the listview will re-render with the new information asynchronously.
Related
I have DropDownList inside my ASP page.
When selection changed postback accured and the Page_Load method fired.
I need to get selected item(selectedValue and selectedIndex) in Page_Load method.
I know that I can use selectedIndexChanged event handler, but in my case it is not
suitable solution because of incorrect architecture.
Any idea how to get selected item in DropDownList control in Page_Load method.
With incorrect architecture is it useful to write
var selectedValue = Request.Params[drpDownList.UniqueID];
You should be OK as long as you check for postback -
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
var index = ddlDropDown.SelectedIndex;
// do stuff
}
}
I'm assuming that the control isn't dynamically created.
I have a PlaceHolder control inside of a ListView that I am using to render controls from my code behind. The code below adds the controls:
TextBox tb = new TextBox();
tb.Text = quest.Value;
tb.ID = quest.ShortName.Replace(" ", "");
((PlaceHolder)e.Item.FindControl("ph_QuestionInput")).Controls.Add(tb);
I am using the following code to retrieve the values that have been entered into the TextBox:
foreach (ListViewDataItem di in lv_Questions.Items)
{
int QuestionId = Convert.ToInt32(((HiddenField)di.FindControl("hf_QuestionId")).Value);
Question quest = dc.Questions.Single(q => q.QuestionId == QuestionId);
TextBox tb = ((TextBox)di.FindControl(quest.ShortName.Replace(" ","")));
//tb is always null!
}
But it never finds the control. I've looked at the source code for the page and the control i want has the id:
ctl00_cphContentMiddle_lv_Questions_ctrl0_Numberofacres
For some reason when I look at the controls in the ListViewDataItem it has the ClientID:
ctl00_cphContentMiddle_lv_Questions_ctrl0_ctl00
Why would it be changing Numberofacres to ctl00? Is there any way to work around this?
UPDATE:
Just to clarify, I am databinding my ListView in the Page_Init event. I then create the controls in the ItemBound event for my ListView. But based on what #Womp and MSDN are saying the controls won't actually be created until after the Load event (which is after the Page_Init event) and therefore are not in ViewState? Does this sound correct?
If so am I just SOL when it comes to retrieving the values in my dynamic controls from my OnClick event?
UPDATE 2:
So i changed the code i had in my Page_Init event from:
protected void Page_Init(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//databind lv_Questions
}
}
to:
protected void Page_Init(object sender, EventArgs e)
{
//databind lv_Questions
}
And it fixed my problem. Still a little confused as to why I want to databind regardless of whether it's a postback or not but the issue is resolved.
It looks like you're adding your textbox to a Placeholder control... but then you're searching a ListViewDataItem container for it later.
Seems to me that you need to search for the Placeholder first, and then search it for the textbox.
I am using a Listview in a usercontrol that I databind to a list of object in the page load event.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
BindListViews();
}
private void BindListViews()
{
MyListView.DataSource = IncludeExpressions;
MyListView.DataBind();
}
I need to handle inserting new items in the list.
To do that, I added an InsertItemTemplate with a button that has "Insert" as command argument.
I dont want to persist the data to the database until the user press the save button, at the bottom of the form.
So in the ItemCommand event, Here is my code:
protected void Expression_ItemCommand(object sender, ListViewCommandEventArgs e)
{
var listView = (sender as ListView);
var expressions = GetExpressions(listView);
var newExpression = new Expression
{
CaseSensitive = ((CheckBox)e.Item.FindControl("CaseSensitiveCheckBox")).Checked,
SearchText = ((TextBox)e.Item.FindControl("SearchTextTextBox")).Text,
Scope = (Scope)Enum.Parse(typeof(Scope), ((DropDownList)e.Item.FindControl("ScopeDropDownList")).SelectedValue, true),
Type = (Type)Enum.Parse(typeof(Type), ((DropDownList)e.Item.FindControl("TypeDropDownList")).SelectedValue, true),
};
expressions.Add(newExpression);
listView.DataSource = expressions;
listView.DataBind();
UpdatePanelInclude.Update();
}
private List<Expression> GetExpressions(ListView lv)
{
var expressions = new List<Expression>();
foreach (var row in lv.Items)
{
var searchText = ((TextBox)row.FindControl("SearchTextTextBox")).Text;
...
expressions.Add(new Expression
{
CaseSensitive = caseSensitive,
Scope = scope,
Type = type,
SearchText = searchText
});
}
return expressions;
}
This works perfectly fine until I add an UpdatePanel around the listview.
When I add an updatepanel, the Expression_ItemCommand handler is hit only every 2 clicks, eventhough the page is post back every click.
While debugging, I can see that I do enter the Page_Load event of the page at each click on the Insert button, but it hits the Expression_ItemCommand only every 2 clicks. and reset the content of my listview when the ItemCommand is not hit.
I smell ViewState problems here, but I can't figure out how to fix it.
Here is what the markup looks like :
<asp:UpdatePanel ID="UpdatePanelInclude" UpdateMode="Conditional" ChildrenAsTriggers="true" runat="server">
<asp:ListView ID="MyListView" OnItemCommand="Expression_ItemCommand" OnItemInserting="ExpressionInserting" OnDataBinding="ListViewDataBinding" InsertItemPosition="LastItem" runat="server" ItemPlaceholderID="itemPlaceHolder">
...
...
Any Idea how to solve this?
Stéphane
After looooong investigations and rebuilding of the page control by control, the reason was that the viewstate was compressed and the scriptmanager didnt like it somehow, even though I specify the hidden field to it.
Probleme solved...
I have a user control, which is added to another user control. The nested user control is built up of a GridView, an image button and a link button. The nested user control is added to the outer control as a collection object based upon the results bound to the GridView.
The problem that I have is that my link button doesn't work. I click on it and the event doesn't fire. Even adding a break point was not reached. As the nested user control is added a number of times, I have set image button to have unique ids and also the link button. Whilst image button works correctly with its JavaScript. The link button needs to fire an event in the code behind, but despite all my efforts, I can't make it work. I am adding the link button to the control dynamically. Below is the relevant code that I am using:
public partial class ucCustomerDetails : System.Web.UI.UserControl
{
public event EventHandler ViewAllClicked;
protected override void CreateChildControls( )
{
base.CreateChildControls( );
string strUniqueID = lnkShowAllCust.UniqueID;
strUniqueID = strUniqueID.Replace('$','_');
this.lnkShowAllCust.ID = strUniqueID;
this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click);
this.Controls.Add(lnkShowAllCust);
}
protected override void OnInit (EventArgs e)
{
CreateChildControls( );
base.OnInit(e);
}
protected override void OnLoad(EventArgs e)
{
base.EnsureChildControls( );
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
CreateChildControls( );
}
}
protected void lnkShowAllCust_Click(object sender, EventArgs e)
{
this.OnCustShowAllClicked(new EventArgs ( ));
}
protected virtual void OnCustShowAllClicked(EventArgs args)
{
if (this.ViewAllClicked != null)
{
this.ViewAllClicked(this, args);
}
}
}
I have been stuggling with this problem for the last 3 days and have had no success with it, and I really do need some help.
Can anyone please help me?
My LinkButton wasn't firing it's Click event, and the reason was I had its CausesValidation property set to True. If you don't want the link to validate the form, be sure to set this to False.
Try adding your click event to the linkbutton tag:
<asp:LinkButton runat="server" OnClick="linkShowAllCust_Click" />
Or adding it to your Page_Load:
Page_Load(object sender, EventArgs e)
{
this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click);
}
Is the usercontrol within the gridview? If so register the event handler on the gridview's onrowcreated event.
It appears that you have a viewstate issue. Because the control isn't there when the viewstate is loaded the application doesn't know how to hook up the event to be fired. Here is how to work around this.
You can actually make your app work like normal by loading the control tree right after the loadviewstateevent is fired. if you override the loadviewstate event, call mybase.loadviewstate and then put your own code to regenerate the controls right after it, the values for those controls will be available on page load. In one of my apps I use a viewstate field to hold the ID or the array info that can be used to recreate those controls.
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(savedState)
If IsPostBack Then
CreateMyControls()
End If
End Sub
I had the same issue. I had viewstate="false" on the page I was adding the control to. (on the aspx page)
I'm using DotNetNuke 4.9.2 and am running into an odd issue.
I have a MultiView in the module that I'm developing, and in one of the views have a GridView that is bound to an ObjectDataSource.
In a separate view, i have several buttons that will switch the SelectMethod of the ObjectDataSource in the 2nd view and then set that view active. That all works fine, until the grid is sorted on the 2nd view - which causes a postback and the ODS somehow picks up its original SelectMethod. The SelectParameters that are assigned at the same time in the code-behind stick though.
Seems to me that the ObjectDataSource should be remembering the SelectMethod in viewstate, shouldn't it?
<asp:ObjectDataSource runat="server" ID="MyObjectDataSource" SelectMethod="MyFirstSelectMethod" TypeName="Whatever"></asp:ObjectDataSource>
protected void Button1_Click(object sender, EventArgs e)
{
MyObjectDataSource.SelectMethod = "MyNewMethod";
// more code here to change the parameters as well...
MyMultiView.SetActiveView(MyView2);
}
When I run that button click, the grid displays as expected. When I click on one of the column headers for the GridView and break in the page load to inspect the SelectMethod, it has reverted to the one declared in the markup.
Any suggestions as to what my problem could be here?
I'm guessing you've made sure that you're not resetting .SelectMethod when the page reloads?
I ended up working around the issue by just using a page property to hold the selectmethod, and then resetting it on each postback...
protected string MySelectMethod
{
get
{
return (string)ViewState["MySelectMethod"] ?? MySearchResultsDataSource.SelectMethod;
}
set
{
ViewState["MySelectMethod"] = value;
MySearchResultsDataSource.SelectMethod = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
MySearchResultsDataSource.SelectMethod = MySelectMethod;
}
}
protected void MyButton_Click(object sender, EventArgs e)
{
MySelectMethod = "MyNewMethod";
}
Still not sure why that SelectMethod prop doesn't stick on a postback in nuke. I'm sure this has worked fine for me in straight asp.net projects in the past...