|
Hi
I have a link button which is part of my user control. Further, the control is actually added to the control dynamically. Whilst, I can see the control, and is added to the page without any visible errors, but when I click on to fire an event which on that user control nothing happens. What is even more odd, if I add a break point to event, and then click in debug mode, I can't even reach the break point!
To try and resolve this, I have tried giving the link button ids, and ensured they unique, added override using OnInit and used the createchildcontrols method, but nothing seems to work!
Has anyone came across and if so, can give me a solution?
|
|
|
|
|
Where in the code is the linkbutton created dynamically? Put a breakpoint on page_load and you should see that this is hit when you click the linkbutton. It doesn't fire the event because the control is not recreated again as part of the postback.
Try recreating the control in page_load if isPostBack = true. Remember to recreate your event handler here as well.
|
|
|
|
|
Hi
I had already done all of this.
The linkbutton is created on the usercontrol form.
I have added the linkbutton creation to, with this.linkbutton1.click += EventHandler(this.linkbutton1_click); to Page_Load, OnInit, Page_PreRender and CreateChildControls.
The event will not fire even after doing all of this!
|
|
|
|
|
Could you post the code behind please?
|
|
|
|
|
As you said the Link button is created Dynamically, you have to create it Every Time when the page is post back, also create it in the Init event of the user control.
|
|
|
|
|
Here is the user control with the link button on it!
public partial class ucCustomerDetails : System.Web.UI.UserControl
{
protected override void CreateChildControls( )
{
base.CreateChildControls( );
this.lnkShowAllCust.ID = "lnkShowAllCust";
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);
}
}
public event EventHandler ViewAllClicked;
}
I have deleted other stuff that is on it, but these other controls have nothing to do with the link button
|
|
|
|
|
Here is the user control with the link button on it!
public partial class ucCustomerDetails : System.Web.UI.UserControl
{
protected override void CreateChildControls( )
{
base.CreateChildControls( );
this.lnkShowAllCust.ID = "lnkShowAllCust";
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);
}
}
public event EventHandler ViewAllClicked;
}
I have deleted other stuff that is on it, but these other controls have nothing to do with the link button
|
|
|
|
|
Try following code:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
LinkButton lnk = new LinkButton();
lnk.Text = "My Link";
lnk.Click += new EventHandler(lnk_Click);
plcHld.Controls.Add(lnk);
}
void lnk_Click(object sender, EventArgs e)
{
lblMessage.Text = "Link Clicked";
}
|
|
|
|
|
I have tried this, and still no luck.
One thing I did omit to mention is that I have an AJAX control on the ASPX page that encompasses the whole user control. All this does is to refresh the page every 5 mins.
I commented this AJAX control out, added a break point to the link button event, and all that happened was the java script fired, but it didn't reach the break point.
|
|
|
|
|
This is a simple example of recreating your control on postback. So assuming that works and you still have the problem: is there any validation on the front end? Could it be that the form isn't valid so you don't get the postback?
public partial class WebUserControl1 : System.Web.UI.UserControl
{
static int x = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
if (ViewState["x"] != null)
{
CreateLB();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
CreateLB();
}
private void CreateLB()
{
LinkButton lb = new LinkButton();
lb.Text = "click me";
lb.Click += new EventHandler(lb_Click);
this.Controls.Add(lb);
ViewState["x"] = true;
}
void lb_Click(object sender, EventArgs e)
{
x += 1;
Label1.Text = x.ToString();
}
}
front end:
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
|
|
|
|
|
Hi
There is no form validation at all on the page.
The only other thing I have is an AJAX Control, which refreshes the page every 5 mins.
I will try your code, and see how I get on!
|
|
|
|
|
Hi
I have tried your code, and nothing happened.
However, as part of my debugging and trying to get to grips with this, I added a break point to the page_load method and ran again.
The page_load method didn't get hit, I even then moved the code from there to OnLoad, and it did get hit, but inside the if statement for the viewstate, it skipped over the CreateLB() method!
Any thoughts?
|
|
|
|
|
Have you wired up the button on the front page to hit right method:
protected void Button1_Click(object sender, EventArgs e)
{
CreateLB();
}
The idea is, you click the button on the form, this dynamically creates the linkbutton, then when you click the linkbutton it stays there and is recreated on postback every time you click it.
|
|
|
|
|
Yes, I have.
Since my reply to you, I have been digging a bit deeper, and my feelings at the moment are starting to sway if the button event is not being wired up correctly. Therefore, I double checked the AutoWireEvent, and this has been set to true on the usercontrol.
By the way, the Page_Load is being overriden by the OnLoad method, which I need to have on my page, because I am setting up a grid view, which needs to reload on PostBack, because it is also built dynamically.
What I have also done is put on a simple button on the control, and ran the ocde in debug through OnLoad. This has set the button text value, and I can see the event handler being added in. Next I clicked the button on Post Back. When the page is reload via OnLoad, which also has a if (IsPostBack) this is hit, and what I wanted to do is to reset the text value to something else.
However, what happened instead was that the button disappeared off the screen. Event for that button was never reached!
Am I missing a trick here?
|
|
|
|
|
how to simulate on demand combo in asp.net. i want when user click on arrow button of combo then combo will load the data. how to do this.Thanks in advance
tbhattacharjee
|
|
|
|
|
Why Dont you go for autocomplete extender..!
or you can go to teletrix controls thts only a suggestion
LatestArticle :Log4Net
Why Do Some People Forget To Mark as Answer .If It Helps.
|
|
|
|
|
Well, using auto-complete extender of ajax in textbox
should not be a bad idea to achieve what you want.
If you stil wish to populate combo box, call a WebMethod
on click of your combo box (drop down list actually), then get the
data in xml format, read that xml using javascript and then attach the
items to your combo box.
|
|
|
|
|
suppose when user click on link or button for downloading file in asp.net then download start.
so i just want to know how to detect that the file as been downloaded in client machine.
is it possible to track from server side code. because i want to develop a file download section
from where user at a time can download only single file. so in this case when file download start
i will just capture client ip and store it in session and when file download will be over then i will
clear that session and in this way my purpose will be solved and restric user to downlolad multiple file.
few days back i post this same question bot did not got any good solution. i forget to ask u that how rapid share,hotfile do this.
in hot file i saw they allow download one file at a time. the moment one file is downladed then another can be download.what type of logic they
use. please let me know if u guys have any idea.
Thanks in advance
tbhattacharjee
|
|
|
|
|
Hi ,
How to make a screen shot of our aspx page ? any idea ?
With warm regards ...
|
|
|
|
|
|
after Alt + Ptr Scr, open mspaint and press Ctrl + V & Save it....
|
|
|
|
|
You're right: I didn't think: if he didn't know how to Alt+PrtScr how the hell would he know what to do next?
|
|
|
|
|
why you are extending this..
|
|
|
|
|
Because I can and it's fun.
|
|
|
|
|
alt+print screen ?.this forum is for developers bro.
|
|
|
|