I've got a user control thats used twice on the same page, each have the ability to be updated (a dropdown list gets a new item) and I'm not sure what might be the best way to handle this.
One concern - this is an older system (~4+ years, datasets, .net2) and it is amazingly brittle. I did manage to have it run on 3.5 with no problems, but I've had a few run-ins with the javascript validation (~300 lines per page) throwing up all over the place when I change/add/modify controls in the parent.
Add an event to your user control.
public event EventHandler MyEvent;
protected void OnMyEvent(EventArgs e)
{
if(MyEvent != null)
{
MyEvent(this, e);
}
}
protected void AddOptionAdded(object sender, EventArgs e)
{
OnMyEvent(EventArgs.Empty);
}
Then in your page you can subscribe to both controls event.
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl1.MyEvent += OnMyEventHander;
WebUserControl2.MyEvent += OnMyEventHander;
}
protected void OnMyEventHandler(object sender, EventArgs e)
{
// Notify the other controls that something changed.
}
Then in your page's event handler you can do whatever you need to do to update the other control. Calling a method, etc.
You can also go as far as creating your own delegate and EventArgs classes to pass additional custom data that may be needed.
Didn't even need most of it, I forgot to mention it was vb, so I put this in the user control...
Public Event UpdateListings As EventHandler
Public Function SomethingToDo
'doing some cool stuff ...not really
RaiseEvent UpdateListings(Me, EventArgs.Empty)
Return result
End Function
then on the code behind on the parent page
Protected Sub UpdateStuff(ByVal sender As Object, ByVal e As EventArgs) Handles userControl1.UpdateListings, userControl2.UpdateListings
userControl1.BindStuff()
userControl2.BindStuff()
End Sub
Related
I have created a User Control(Popupcontrol) and in that control i have created a property(PageType) and when i am using the Popupcontrol on the page then i set the property(pagetype) according to the page.
but now there is some problem i have to two button on the page and on the second button click i want to change the pagetype property .So is there any solution for the same.
Based on your comment, it seems you bind the data (PageType property in your question) in the Page_Load event, instead of this it should be done in overrided DataBind method which should be called if the page is not in post back request (otherwise your data will be overwritting in the next Page_Load event as you mentioned in your comments):
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
DataBind();
}
}
public override void DataBind()
{
PageType = someValue;
}
after this your click handler may looks like:
protected void button2_Clicked(object sender, EventArgs e)
{
PageType = someOtherValue;
}
Are you setting the variable in a page load event? You may need to add:
if (!Page.IsPostback) {
// Code here.
}
In my website I wrote that code:
protected void Page_Load(object sender, EventArgs e){ LinkButton lbtnTopicAddress = new LinkButton(); lbtnTopicAddress.Click += lbtnSpecificTopic1_Click;}
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e){ Server.Transfer("~/SpecificTopic.aspx)"
}
But when I press on the link in run time, the caller doesn't go to the EventHandler method.
Why?
Note,
I wrote code like that in many pages in the same website but it work only in one page.
i added that code to many page in website but it worded only in one page every page has its specific code and no relation between them I hope you understand me thanks
I need help pleaseeeeeeee..........................
Did you mean to miss off a ;and a } here?
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e){ Server.Transfer("~/SpecificTopic.aspx)"
I assume you've put a breakpoint in to ensure it isn't being fired?
I'm not exactly sure but I've got a feeling that instead of Page_Load you need to use Page_Init so your code would look this this:
protected void Page_Init(object sender, EventArgs e)
{
LinkButton lbtnTopicAddress = new LinkButton();
lbtnTopicAddress.Click += lbtnSpecificTopic1_Click;
}
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e)
{
Server.Transfer("~/SpecificTopic.aspx");
}
p.s. 5 mins formatting your code can work wonders when trying to debug
Are you adding the button to the controls on your page, or are you trying to find the "lbtnTopicAddress" control on your page?
Simply declaring the button won't do anything -- you have to get a reference to the control itself from the page.
I have a LinkButton within a User Control and it has handled with:
Private Sub LoginLinkLinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginLinkLinkButton.Click
Response.Redirect("/", True)
End Sub
On certain ASPX pages I would like to handle the click from the page's code behind, opposed to within the user control. How do I override the handle within the control from the parent's code behind page?
Update:
Based on the answer, I have the updated the User Control:
Public Event LoginLinkClicked As OnLoginClickHandler
Public Delegate Sub OnLoginClickHandler(ByVal sender As Object, ByVal e As EventArgs)
[...]
Private Sub LoginLinkLinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginLinkLinkButton.Click
**If OnLoginClickHandler IsNot Nothing Then**
RaiseEvent LoginLinkClicked(Me, e)
Else
Response.Redirect("/", True)
End If
End Sub
The problem is determining the correct syntax for the if line because the above is invalid.
You'll have to expose a new event from the user control. Apologies as the following code is all in C# and it's been a long time since I touched VB.Net, but you should get the idea:
You can use a delegate event by adding the following to your UserControl:
public event OnLoginClickHandler LoginClick;
public delegate void OnLoginClickHandler (object sender, EventArgs e);
Then call the following to your LinkButton Click event:
protected void LoginLinkLinkButton_Click(object sender, EventArgs e)
{
// Only fire the event if there's a subscriber
if (OnLoginClickHandler != null)
{
OnLoginClickHandler(sender, e);
}
else
{
// Not handled, so perform the standard redirect
Response.Redirect("/", true);
}
}
You can then just hook up into this within your aspx markup:
<asp:LinkButton runat="server" ID="Foo" OnLoginClick="Foo_LoginClick" />
And your server side event handler on your Page will be as follows:
protected void Foo_LoginClick_Click(object sender, EventArgs e)
{
// This event was fired from the UserControl
}
UPDATE
I think this is how you translate the event subscription check to VB.Net:
If LoginClick IsNot Nothing Then
RaiseEvent LoginClick(sender, e)
End If
I think you should have a look into below code and links.
Parent Page aspx.cs part
public partial class getproductdetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button btnYes = (Button)ucPrompt.FindControl("btnYes");
btnYes.Click += new EventHandler(ucPrompt_btnYes_Click);
}
void ucPrompt_btnYes_Click(object sender, EventArgs e)
{
//Do Work
}
}
Read more details how it works (Reference) :-
Handle events of usercontrol in parent page in asp.net
Calling a method of parent page from user control
Time for my daily ASP.NET question.
One of my pages shows all of our customer information from a customer table. I want the user to choose whether to see all customer records, or select a specific record from a list. So, my webpage has two radio buttons (show all customers, show specific customer), a listbox (full of customer names), and a formview control. The problem I'm having is getting the formview to update when I change modes via radio buttons or listbox selection (see code below).
Can anyone provide me with some pointers on how to do what I'm trying to do?
protected void Page_Load(object sender, EventArgs e)
{
UpdatePage ();
}
protected void RadioButtonShowAll_CheckedChanged(object sender, EventArgs e)
{
}
protected void RadioButtonShowSelected_CheckedChanged(object sender, EventArgs e)
{
}
protected void DropDownListCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
RadioButtonShowSelected.Checked = true;
UpdatePage ();
}
protected void UpdatePage ()
{
if (RadioButtonShowAll.Checked)
SqlDataSource1.SelectCommand = "SELECT * FROM [Customer] ORDER BY [Company]";
else
SqlDataSource1.SelectCommand = "SELECT * FROM [Customer] WHERE ([CustomerID] = #CustomerID) ORDER BY [Company]";
FormView1.DataBind();
}
First, you only have the SelectedIndexChanged event wired up... In that case, what happens when you change the drop down box is first, Page_Load() fires--which calls UpdatePage(). Then, the event fires, which calls UpdatePage() again. The second time probably doesn't do what you expect.
The fix is to only call UpdatePage() from Page_Load() the first time the page is loaded, but not from postbacks:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
UpdatePage();
}
Your page has to be updated on Client side, for it to show the new data. You'll need to use Javascript or AJAX and have some variable that keeps track of the need to refresh your page, this way you can send a request to update the page to the server when the Formview needs updating.
I need to insert a bit of Javascript into the process when a Web Form is submitted, but after the client side validation takes place.
RegisterOnSubmitStatement seems to place the javascript before the validation.
Anyone know how to get it to render after?
Solution found:
In a web control, I put something like this:
protected override OnInit(EventArgs e) {
Page.SaveStateComplete += new EventHandler(RegisterSaveStuff);
base.OnInit(e);
}
void RegisterSaveStuff(object sender, EventArgs e) {
Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "name", "JS code here");
}
that´s right, the RegisterOnSubmitStatement DO NOT WORK in the init function.
It should be called after in the page lige cycle.
I thing the right place therefor is:
"PreRenderComplete"
protected override OnInit(EventArgs e)
{
Page.PreRenderComplete+= new EventHandler(Page_PreRenderComplete);
base.OnInit(e);
}
void Page_PreRenderComplete(object sender, EventArgs e)
{
Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "name", "JS code here");
}
After some research online and playing around with it, I figured out that you can do it by hooking into the Page's SaveStateComplete event. I guess the validation submit statement is registered during the PreRender event, so if you register it after that (in the SaveStateComplete event) you can get it afterward.
You do have to re-register it, but that's not a big deal because I'm not relying on ViewState for my JS.