I`m building a dashboard application using ASP.NET where users can change positions of the widgets; i used jquery sortables for that. The widgets are ascx (asp.net usercontrols) which are added dynamically to the page. Every event in the widget works fine on if the user have not changed the positions of the widgets but if the position is changed i get this error.
Uncaught Sys.InvalidOperationException: Sys.InvalidOperationException: Could not find UpdatePanel with ID 'xxx'. If it is being updated dynamically then it must be inside another UpdatePanel.
Here is the code of one of the widgets and user can add multiple instances of it.
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<div class="setting" >
<asp:TextBox ID="txtwidgettitle" runat="server"></asp:TextBox>
<div id="settingfooter">
<asp:LinkButton ID="lnkbtnSave" runat="server" onclick="lnkbtnSave_Click">Save</asp:LinkButton>
<asp:LinkButton ID="lnkbtncancel" runat="server" CssClass="btn ui-state-default ui-corner-all">Cancel</asp:LinkButton>
</div>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="lnkbtnSave" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<div class="pcontent">
<asp:Literal ID="ltrtwcontent" runat="server"></asp:Literal>
</div>
And the code behind
protected void Page_Load(object sender, EventArgs e)
{
ltrtwcontent.Text = jqPlotHelper.RenderChart(CurrentWidgetInstance.Id.ToString());
}
protected void lnkbtnSave_Click(object sender, EventArgs e)
{
DashboardServices d = new DashboardServices();
CurrentWidgetInstance.Title = txtwidgettitle.Text;
CurrentWidgetInstance.LastUpdate = DateTime.Now;
d.SaveOrUpdateWidgetInstance(CurrentWidgetInstance);
}
on the aspx page loading the controls i first have a container which is added dynamically and the container will add the the widget instances (the ascx controls)
Code on the page
protected void Page_Init(object sender, EventArgs e)
{
this.SetBasePageVAR(DashboredPageGuid);
if (this.UsrPage != null)
{
phltabs.Controls.Add(LoadDashboardTab());
}
}
public HtmlGenericControl LoadDashboardTab()
{
HtmlGenericControl mainList = new HtmlGenericControl("div");
mainList.Attributes["Id"] = "tabs";
foreach (WidgetInstance widgetInst in CurrentDashboardTab.WidgetInstances)
{
HtmlGenericControl headerList = new HtmlGenericControl("ul");
WidgetContainerBaseControl widgetContainer = LoadControl("~/Dashboard/WidgetContainer.ascx") as WidgetContainerBaseControl;
widgetContainer.SetControlVAR(widgetInst);
headerList.Controls.Add(widgetContainer);
mainList.Controls.Add(headerList);
}
return mainList;
}
and on WidgetContainer.ascx
protected void Page_Init(object sender, EventArgs e)
{
LoadWidgetInstance();
}
public void LoadWidgetInstance()
{
WidgetControl widget = LoadControl(CurrentWidgetInstance.Widget.Url) as WidgetControl;
widget.ID = "wid_" + CurrentWidgetInstance.Id;
this.phlcontent.Controls.Add(div);
}
try this: Add trigger Like this and Remove the one in Asps Page
ScriptManager sm = (ScriptManager)Page.Master.FindControl("ScriptManager1");
sm.RegisterPostBackControl(Button);
Related
Please consider this scenario:
I have a simple page and I want to log all controls causing postback. I create this simple page. It contains a grid to show some URLs and when user click on an icon a new tab should open:
<form id="form1" runat="server">
<div>
<table style="width: 100%;">
<tr>
<td style="background-color: #b7ffbb; text-align: center;" colspan="2">
<asp:Button ID="Button3" runat="server" Text="Click Me First" Height="55px" OnClick="Button3_Click" />
</td>
</tr>
<tr>
<td style="background-color: #f1d8fe; text-align: center;" colspan="2">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" BackColor="White" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="SiteAddress" HeaderText="Address" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" ImageUrl="~/download.png" runat="server" CommandArgument='<%# Eval("SiteAddress") %>' CommandName="GoTo" Height="32px" Width="32px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
and code behind:
public partial class WebForm2 : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
List<Address> Addresses = new List<Address>()
{
new Address(){ SiteAddress = "https://google.com" },
new Address(){ SiteAddress = "https://yahoo.com" },
new Address(){ SiteAddress = "https://stackoverflow.com" },
new Address(){ SiteAddress = "https://learn.microsoft.com/}" }
};
GridView1.DataSource = Addresses;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "MyScript", "window.open('" + e.CommandArgument.ToString() + "', '_blank')", true);
}
}
class Address
{
public string SiteAddress { get; set; }
}
every thing is fine till here. Now I create a base class for all of my pages and add below codes for finding postback control:
public class MyPageBaseClass : Page
{
protected override void OnInit(EventArgs e)
{
if (!IsPostBack)
{
}
else
{
var ControlId = GetPostBackControlName(); <------
//Log ControlId
}
base.OnInit(e);
}
private string GetPostBackControlName()
{
Control control = null;
string ctrlname = Page.Request.Params["__EVENTTARGET"];
if (ctrlname != null && ctrlname != String.Empty)
{
control = Page.FindControl(ctrlname);
}
else
{
foreach (string ctl in Page.Request.Form)
{
Control c;
if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
{
string ctrlStr = ctl.Substring(0, ctl.Length - 2);
c = Page.FindControl(ctrlStr);
}
else
{
c = Page.FindControl(ctl);
}
if (c is System.Web.UI.WebControls.Button ||
c is System.Web.UI.WebControls.ImageButton)
{
control = c;
break;
}
}
}
if (control != null)
return control.ID;
else
return string.Empty;
}
}
and change this line:
public partial class WebForm2 : MyPageBaseClass
Now when I click on icons grid view disappears...(STRANGE...) and nothing happened. When I comment specified line then every thing will be fine...(STRANGE...).
In GetPostBackControlName nothings changed to Request but I don't know why this happened. I checked and I see if I haven't RegisterStartupScript in click event every thing is fine. Please help we to solve this problem.
Thanks
when I click on icons grid view disappears...
ASP.Net page class object instances only live long enough to serve one HTTP request, and each HTTP request rebuilds the entire page by default.
Every time you do a postback, you have a new HTTP request and therefore a new page class object instance and a completely new HTML DOM in the browser. Any work you've done for a previous instance of the page class — such as bind a list of addresses to a grid — no longer exists.
You could fix this by also rebuilding your grid code on each postback, but what I'd really do is skip the whole "RegisterStartupScript" mess and instead make the grid links open the window directly, without a postback at all.
The problem is related to OnInit event. I replaced it with OnPreLoad and every things is fine now.
For search engines: OnInit event has conflict with RegisterStartupScript
I am trying to make the input box show some text in the asp.net using c#, but after I run the code behind, nothing is displayed in the input box. The ui is RadNumericTextBox uiDistance. In class, I put this.uiDistance.Text = "123"; But in the webpage after run the code. I cannot see "123" is put in the uiDistance text box. How can I fix it?
Here is my aspx UsageControl2SubControl1.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="UsageControl2SubControl1.ascx.cs" Inherits="WebControls.UsageControl2SubControl1" %>
<asp:HiddenField ID="uiRemoved" Value="false" runat="server" />
<asp:HiddenField ID="uiID" runat="server" />
<div class="form-group">
<div class="col-md-2 customColumnPadding">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<telerik:RadTextBox ID="uiToLocation" runat="server" Width="100%" AutoPostBack="true"
OnTextChanged="uiToLocation_Leave" EmptyMessage="<%$ Resources:ResourceHKEx,Arrive_To %>" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div class="col-md-2 customColumnPadding" id="uiColumnDistance" runat="server">
<telerik:RadNumericTextBox ID="uiDistance" runat="server" Width="100%" MinValue="0" NumberFormat-DecimalDigits="2" EmptyMessage="<%$ Resources:Resource,Distance %>" />
</div>
</div>
<div class="hr-line-dashed"></div>
And Here is my code behind: UsageControl2SubControl1.ascx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
namespace WebControls
{
public partial class UsageControl2SubControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToBoolean(uiRemoved.Value))
{
this.Visible = false;
}
}
protected void uiAdd_Click(object sender, Telerik.Web.UI.ImageButtonClickEventArgs e)
{
}
protected void uiRemove_Click(object sender, Telerik.Web.UI.ImageButtonClickEventArgs e)
{
}
protected void RadComboBoxProduct_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
}
protected void uiAdd_Click1(object sender, ImageClickEventArgs e)
{
}
protected void uiRemove_Click1(object sender, ImageClickEventArgs e)
{
}
protected void uiToLocation_Leave(object sender, EventArgs e)
{
this.uiDistance.Text = "123";
}
}// end class
}// end namespace
Just to update your aspx code little bit
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="form-group">
<div class="col-md-2 customColumnPadding">
<telerik:RadTextBox ID="uiToLocation" runat="server" Width="100%" AutoPostBack="true"
OnTextChanged="uiToLocation_Leave" EmptyMessage="<%$ Resources:ResourceHKEx,Arrive_To %>" />
</div>
<div class="col-md-2 customColumnPadding" id="uiColumnDistance" runat="server">
<telerik:RadNumericTextBox ID="uiDistance" runat="server" Width="100%" MinValue="0" NumberFormat-DecimalDigits="2" EmptyMessage="<%$ Resources:Resource,Distance %>" />
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
As your textbox is only inside the update panel so, on post back only that portion is partially updated, you need to put RadNumericTextBox inside the update panel too.
Hope this will work.
You are using the wrong property. Use Value not Text
protected void uiToLocation_Leave(object sender, EventArgs e)
{
this.uiDistance.Value = "123";
}
As always, check the documentation. See: https://docs.telerik.com/devtools/aspnet-ajax/controls/numerictextbox/features/getting-and-setting-values
Your current code will works only when you tab out from uiToLocation textbox.
If you want to show it when page loads, just move your code in to Page_Load event instead uiToLocation_Leave.
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToBoolean(uiRemoved.Value))
{
this.Visible = false;
}
this.uiDistance.Text = "123"; // it will assign when page loads.
}
If you want to load it only once when page loads, use IsPostaback, see below.
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToBoolean(uiRemoved.Value))
{
this.Visible = false;
}
if (!IsPostBack)
{
this.uiDistance.Text = "123"; // it will assign only once when page loads.
}
}
IT seems you are using the wrong property to assign value to telerik Textbox.
Use .Value property as .Text property if for default aspx Textbox control
this.uiDistance.Value= "123"
I wrote the following code for multiview where I am using gridview and datalist:
<ContentPlaceHolderID="ContentPlaceHolder1">
<div class="alert alert-success" >
<div class="divbtn" style="text-align:right">
<asp:LinkButton ID="gridbtn" runat="server" CssClass="glyphicon glyphicon-th" OnClick="gridbtn_Click"></asp:LinkButton>
<asp:LinkButton ID="listbtn" runat="server" CssClass="glyphicon glyphicon-th-list" OnClick="listbtn_Click"></asp:LinkButton>
</div>
</div>
<asp:MultiView runat="server" ID="multiview" ActiveViewIndex="0">
<asp:View runat="server" ID="gridview">
<uc1:GridControl runat="server" ID="GridControl"/>
</asp:View>
<asp:View runat="server" ID="listview">
<uc1:Listing runat="server" ID="Listing" />
</asp:View>
</asp:MultiView>
</asp:Content>
I am using two link buttons to call their respective views by firing two separate events as follows.
protected void listbtn_Click(object sender, EventArgs e)
{
multiview.ActiveViewIndex = 1;
}
protected void gridbtn_Click(object sender, EventArgs e)
{
multiview.ActiveViewIndex = 0;
}
Suppose, my datalist (Index=1) is active on my page and if there is a post back it should still be showing the datalist but on postback it automatically switches back to grid view (Index=0). I really need help with this!
You can save the index to a session variable and then read it back on post back like this:
To save:
Session["index"] = index.ToString();
Read it on page load like this:
Index = Session["index"];
You will need the session variable to maintain state per user session. If you want to maintain state for the application then you have to use the application variable.
Hi add the following code in your page_load event.
if(!Page.IsPostBack)
{
multiview.ActiveViewIndex=0;
}
i think you are setting the multiview's active index to zero on every post back like follows
protected void Page_Load(object sender, EventArgs e)
{
multiview.ActiveViewIndex=0;
}
this will cause the multiview to set active index as 0 on every post back.To avoid this you have to set it as follows
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
multiview.ActiveViewIndex=0;
}
}
I am trying to get the text of my label which is inside a repeater, but I keep getting a NullPointerException.
All of the data is coming from database and it is coming correctly.
When I click on the LinkButton, I want to use the Label Text for next bit code.
Aspx page:
<asp:Repeater ID="RepeaterDepartmentParent" runat="server">
<ItemTemplate>
<div id="outerDiv" class="col-lg-3 col-xs-6" runat="server">
<!-- small box -->
<div>
<div class="inner">
<p>
<%# DataBinder.Eval(Container.DataItem, "Department_Namestr")%>
</p>
</div>
<asp:Label ID="lblDepartmentId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Department_Idint")%>' Visible="true"></asp:Label>
<asp:LinkButton ID="linkChildDepartment" CommandName="Click" runat="server" CssClass="small-box-footer" OnClick="linkChildDepartment_Click">More info<i class="fa fa-arrow-circle-right"></i></asp:LinkButton>
</div>
</div><%--<%-- ./col -->--%>
</ItemTemplate>
</asp:Repeater>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
DataSet dsParentDepartment = null;
dsParentDepartment = objDepartmentBL.viewDepartmentparent();
RepeaterDepartmentParent.DataSource = dsParentDepartment.Tables[0];
RepeaterDepartmentParent.DataBind();
}
protected void linkChildDepartment_Click(Object sender, EventArgs e)
{
//what to write here??
//i have tried the bellow code but it gives me every data in that loop but i
//want the single data for a link button click.
//foreach (RepeaterItem item in RepeaterDepartmentParent.Items)
// {
// Label myLabel = (Label)item.FindControl("lblDepartmentId");
// myLabel.Text = Id;
//}
//edited code that works properly
LinkButton linkChildDepartment = (LinkButton)sender;
RepeaterItem item = (RepeaterItem)linkChildDepartment.NamingContainer;
Label myLabel = (Label)item.FindControl("lblDepartmentId");
}
How can I correctly reference the Link Button Label Text?
You can use the NamingContainer property to get the reference of the RepeaterItem. From there it's a short way to your label:
protected void linkChildDepartment_Click(Object sender, EventArgs e)
{
LinkButton linkChildDepartment = (LinkButton) sender;
RepeaterItem item = (RepeaterItem) linkChildDepartment.NamingContainer;
Label myLabel = (Label)item.FindControl("lblDepartmentId");
// ...
}
i have an image button in datalist to open a new page to send mail, but when i click on it for the first time , it doesn't work , it works when click for the second time , i use update panel inside datalist . here's the code
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<asp:ImageButton ID="ImgBtnMail" runat="server" ImageUrl="../../../../Icons/Send-Email.jpg"
Width="22" Height="18" CommandName="DoMail"
CommandArgument='<%#DataBinder.Eval(((System.Web.UI.WebControls.DataListItem)(Container)).DataItem, "AdID")%>' />
</ContentTemplate>
</asp:UpdatePanel>
and the code behind :
protected void DLAds_ItemCommand(object source, DataListCommandEventArgs e)
{
if(e.CommandName=="DoMail")
{
string TargetPage = "window.open('SendMail.aspx?',null,'height=150, width=150,left=500 ,top=300 ,status= no, resizable= no, scrollbars=no, toolbar=no,location=no,menubar=no ');";
ImageButton MailBtn = (ImageButton)e.Item.FindControl("ImgBtnMail");
MailBtn.Attributes.Add("onclick", TargetPage);
}
}
With the example code you've posted, you'd be better off assigning the OnCommand event handler directly to the button.
<asp:ImageButton ID="ImageButton1" runat="server" OnCommand="ImageButton1_Command" ...>
And in your code:
protected void ImageButton1_Command(object sender, EventArgs e)
{
string targetPage = "..."
((ImageButton)sender).Attributes.Add("onclick", targetPage);
}
Set UpdateMode="Conditional" in the UpdatePanel and Update your UpdatePanel in the CodeBehind like below:
<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
protected void ImageButton1_Command(object sender, EventArgs e)
{
up1.Update();
string targetPage = "..."
((ImageButton)sender).Attributes.Add("onclick", targetPage);
}