ASP.NET Change label text in custom event - c#

I am trying to change the text on a label from a custom event. When I update the text through a button click event (Button1_Click), everything works. However, when I try to update the same label in a custom event(ArReceiver_OnArDataUpdate) nothing happens. ArReceiver_OnArDataUpdate is getting triggered properly.
Why does my event react differently than a button click event? What do I need to do to get my event working?
This is an out of the box web project in VS2013. Here's the code to explain better:
Code Behind:
namespace WinReceiver
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
ArReceiver.OnArDataUpdate += ArReceiver_OnArDataUpdate;
}
void ArReceiver_OnArDataUpdate(object sender, ArEventArgs e)
{
labelVoltage.Text = "Reset";
UpdatePanel1.Update();
}
protected void Button1_Click(object sender, EventArgs e)
{
labelVoltage.Text = "Button Clicked";
UpdatePanel1.Update();
}
}
}
Default.aspx: (The site.master has the ScriptManager in it by default)
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="labelVoltage" runat="server" Text="..."></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
EDIT:
I did find this question that says it's not possible, but can someone explain why this may not be possible? I'm ok with a page refresh too, but when I tried Respone.Redirect I got exceptions.

If OnArDataUpdate is a static event, it's likely to be fired when the ASP.NET page rendering process is out of scope, and thus the label is not existent because it isn't in scope of the page being processed (since ASP.NET is stateless).
However, if ArReceiver is a control on the page, it should process fine, and the issue could be within the control or something overriding it (depending on when the event fires, it may get overridden by viewstate).

Related

Full postback on select & remove within update panel

I'm having issues with the asp.net textbox within an update panel. It works perfectly fine when adding or removing each individual character but if I highlight all of the text within the textbox, and then remove it a full postback occurs, not a partial postback which is expected.
Why is this happening? I haven't found anything related to this particular problem so it's likely I'm doing something wrong.
Example aspx:
<asp:UpdatePanel ID="updExample" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:Repeater ID="rptExample" runat="server" .... >
<ItemTemplate>
<asp:TextBox ID="txtExample" runat="server" ClientIDMode="static" Text='<%# Eval("Example") %>' OnTextChanged="txtExample_TextChanged" AutoPostBack="true"></asp:TextBox>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
Example TextChanged Event:
protected void txtExample_TextChanged(object sender, EventArgs e)
{
updExample.Update();
}
Additional Notes:
Switching UpdateMode to 'Always' doesn't work.
Karthikeyan Nagaraj pointed out in the comments to try adding triggers alongside what I already had. I did in fact already have this, however, I was assigning the trigger in the ItemDataBound event which I realized was incorrect after reinvestigating. The ItemCreated event was much more suited.
I had no issues finding the control in the ItemCreated event, however adding a new async postback trigger to the update panel gave me grief and said the control couldn't be found when changing the text. To resolve this, I used the script managers RegisterAsyncPostBackControl(); method as shown below.
protected void rptExample_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var input = e.item.FindControl("txtExample");
if (input != null) {
ScriptManager sm = ScriptManager.GetCurrent(this);
sm.RegisterAsyncPostBackControl(input);
}
}

OnClick event of button fires everytime on PageLoad

I have a button below:
<asp:Button ID="btnApprove" runat="server" Text="Approve" CssClass="button" OnClick="btnApprove_Click" />
Event handler of this button on server side is :
protected void btnApprove_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "key", "alert('Button Approve Clicked')", true);
}
Just get alert on the button click from the server side.
My issue is that once I clicked on Approve button, now when I load or refresh my page again this btnApprove_Click event gets executed everytime.
I have many others button on the same form but none shows this strange kind of behaviour. I tried to change this button as HTML but still the same behaviour.
Can anyone please help me to get out of it. Thanks in advance.
You should use IsPostBack on Page load to prevent every time page load on button click. You can check every time on Page_Load.
Sample Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// First Time Load When User Come
}
else
{
// Every Time Load When Any click button in page
}
Try to Add OnClientClick="return false;"
<asp:Button ID="btnApprove" runat="server" Text="Approve" CssClass="button" OnClick="btnApprove_Click" OnClientClick="return false;"/>
How about using RegisterOnSubmitStatement instead?
Page.ClientScript.RegisterOnSubmitStatement(Page.GetType(), "key", "alert('Button Approve Clicked')");

Change asp:Label text from C# code behind

I've been trying with this for an hour or so; just can't seem to figure out.
I have an asp:Button on an aspx page, required to complete a couple of functions, one of which is to change the text of an asp:Label. This seems like it should be simple and other online posts indicate that I'm approaching the problem correctly but...
The problem is simple but it's killing me. In an effort to debug/troubleshoot, I've stripped the code right back to very basics:
protected void Page_Load(object sender, EventArgs e)
{
allValidationMsg.Text = "Original text";
}
protected void btnRegister_Click(object sender, EventArgs e)
{
allValidationMsg.Text = "Text changed";
}
When the button is clicked, nothing happens. I'm sure it's something simple that I'm missing.
Update:
<asp:Label id="allValidationMsg" runat="server" height="22px" ForeColor="Red"></asp:Label>
<asp:Button class="navbutton" ID="btnRegister" runat="server"
Text="Register User" OnClick="btnRegister_Click" />
I think when you click on the Button, Page_Load is called again and the original text remains. Try this
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
allValidationMsg.Text = "Original text";
}
Apart from this, I assume you register the event handler for the button in the Markup as I cannot see it anywhere in your code-behind
<asp:Button id="Button1" runat="server" OnClick="btnRegister_Click" />
Possibly you have forgotten to bind button click to the handler.
You could do it something like that in code-behind:
mybutton.Click+=btnRegister_Click;
Or in aspx:
<asp:Button id="Button1"
Text="Click here for greeting..."
OnClick="btnRegister_Click"
runat="server"/>
Solved; the problem appears to have been with the use of a CompareValidator. Don't really understand why but when this validator is commented out, problem solved. Funnily enough, RequiredField and RegularExpression validators on same page cause no issues..
Hey I think I know what it is. In your markup does your label look like this??
<asp:Label ID="Label1" runat="server" >Some text</asp:Label>
You want this in your markup:
<asp:Label ID="Lable1" runat="server" text ="some text"></asp:Label>
I have had this happen before. If you change the markup to that. It should work.
I can see that you wrote "onclick" instead of "OnClick" in the markup (case). Possibly it is the cause of the issue.
UPDATE1
Could you try to do so (check if postback works):
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
allValidationMsg.Text = "Original text";
}
else
{
allValidationMsg.Text = "After postback";
}
}
If the text is changing after pressing the button?
UPDATE2 Also, please try to make some changes in the text to understand if a new version really deploys (to exclude strong caching issues).
UPDATE3 You can try to do binding in codebehind (and delete it from aspx).

Nested UpdatePanel refresh parent

I have a case that I'm using an update panel that when updated it will load a user control containing a child update panel, and its code is as follows:
<asp:UpdatePanel runat="server" ID="Parent" UpdateMode="Conditional" ChildrenAsTriggers="false" >
<ContentTemplate>
<asp:PlaceHolder runat="server" ID="ChildUPNL"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
In the code behind I got an object then use the Update method to refresh the parent update panel
Then It' should have my user control filled with data
in the user control I have an update panel also like:
<asp:UpdatePanel runat="server" ID="Child" OnLoad="ChildLoad">
<ContentTemplate>
<asp:LinkButton Text="Link button" runat="server" />
<asp:Button Text="Button" runat="server" OnClick="ButtonClick"/>
</ContentTemplate>
</asp:UpdatePanel>
When I click on the link button the full page reloaded even the parent and my user control disappered as it not in the page direct
and if I click on the normal button nothing done !
didn't go to the update panel on load event nor the button click event !
Any idea why this action done !
I need when click on button to change some values or save values to database so I need to go throw the server side code without post back the page
You have to load every time usercontrol on page_init or page_load. You can use MultiView to achieve your goal.
You can also use Events and delegate and call custom event and bind parent control after click on child panel button.
**//On Page Behind Code**
protected void Page_Load(object sender, EventArgs e)
{
UserControl11.EventCallfromOtherUserControl += new UserControl1.CallfromOtherUserControl(CallEvent);
}
public void CallEvent()
{
//Load Parent Control
}
**//On User Control Behind Code**
public delegate void CallfromOtherUserControl();
public event CallfromOtherUserControl EventCallfromOtherUserControl;
protected void Button1_Click(object sender, EventArgs e)
{
if (EventCallfromOtherUserControl != null)
{
EventCallfromOtherUserControl();
}
}

Cause an async postback when a listbox is double-clicked

I have an ASP.NET listbox, lstActivities. To edit an item in the list, users can either click lnkButton or double-click on the listbox. I achieve this with:
protected void Page_Load(object sender, EventArgs e) {
if (IsPostBack) return;
var refDblClick = ClientScript.GetPostBackEventReference(lnkButton, "dblClick");
lstActivities.Attributes.Add("ondblclick", refDblClick);
}
protected override void Render(HtmlTextWriter writer)
{
ClientScript.RegisterForEventValidation(lnkButton.UniqueID, "dblClick");
base.Render(writer);
}
I would like to change this so that the postback is asynchronous, using AJAX. At the moment, the listbox and button are in an UpdatePanel so there is an async postback when the button is clicked. But when the listbox is double-clicked, a full postback occurs.
<asp:UpdatePanel ID="up" UpdateMode="Conditional" ChildrenAsTriggers="true"
runat="server">
<ContentTemplate>
<asp:ListBox ID="lstActivities" runat="server"></asp:ListBox>
<asp:LinkButton ID="lnkButton" runat="server" OnClick="lnkButton_Click">
Edit</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
How can I make the double-click refresh only the UpdatePanel?
A few things to try:
<asp:UpdatePanel ID="up" UpdateMode="Conditional" ChildrenAsTriggers="true"
runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="lstActivities" />
<asp:AsyncPostBackTrigger ControlID="lnkButton" />
</Triggers>
.........
</asp:UpdatePanel>
or
protected void Page_Load(object sender, EventArgs e) {
if (IsPostBack) return;
var refDblClick = ClientScript.GetPostBackEventReference(lnkButton, "dblClick");
lstActivities.Attributes.Add("ondblclick", refDblClick);
ScriptManager1.RegisterAsyncPostBackControl(lstActivities);
}
I just a ran a quick test with the code you provided and I do get "partial postbacks" (for lack of a better term since updatepanels always do full postbacks) on both, the clicking of the button and the double clicking of the list.
If you set other panels in that page to UpdateMode="Conditional" as you are doing with your UpdatePanel "up", then only elements inside "up" will be updated. If you don't specify the update mode on the other panels, then they will always be updated on postback, because, again, update panels ALWAYS do full postbacks; what they really do is partial refreshes of the page.
Linking MSDN documentation regarding UpdatePanel as I think is a very helpful read.
I tried the solutions suggested, but with no luck. It's quite a complicated page with lots of UpdatePanels so hard to work out the exact problem.
In the end I went for jQuery:
$(document).ready(function () {
$(document).delegate('#ctl00_body_lstActivities', 'dblclick', function () {
eval($('#ctl00_body_lnkButton').attr('href'));
});
});

Categories