How do I get around modal interfering with button click event? - c#

In my ASP.Net Web Form App, I have a button, and that button is suppose to launch an ajaxToolKit: ModalPopupExtender modal.
<asp:Button ID="uxTicketHistoryButton" runat="server" Text="Show Ticket History" style="color: blue;" OnClick="uxTicketHistoryButton_Click"/>
<ajaxToolkit:ModalPopupExtender ID="uxTicketHistoryModal" runat="server" PopupControlID="Panel1" TargetControlID="uxTicketHistoryButton"CancelControlID="btnClose" BackgroundCssClass="modalBackground"></ajaxToolkit:ModalPopupExtender>
<asp:Panel ID="Panel1" runat="server" CssClass="modalPopup" align="center" style = "display:none">
....
</asp:Panel ID>
I had asked a previous question on how to insert C# variable values into the modal, but it seems my problem there is that the modal seems to be interfering with my button click event function; essentially keeping it from firing. The function is suppose to get the data I need to plug into the modal once the button is clicked and the modal launched.
protected void uxTicketHistoryButton_Click(object sender, EventArgs e)
{
DataTable ticketHist = _dtMgr.GetContactInfoByName(uxContactDropdownList.SelectedValue);
string rName = ticketHist.Rows[0]["RequestorName"].ToString();
string rPhone = ticketHist.Rows[0]["RequestorPhone"].ToString();
....
}
I have noticed several odd things while debugging: 1) I put break points on the string variables (to see what the values were being returned) while debugging. After clicking the button, it goes right over the breakpoints (I assume that means its not "firing" that code.2) I can remove all of the code from inside the _Click function and the modal still launches when I click the button 3) If I comment out the modal code and click the button, the _Click event code fires fine and I can see the values for the string variables. So, I am assuming that the way I have the modal set (I suspect its related to using TargetControlID="uxTicketHistoryButton") is the problem. How do I get around my modal interfering with button click event? What am I doing wrong here?

Looks like you found one of those ajaxToolkit quirks. Try this:
Create a hidden button:
<asp:button id="hiddenButton" runat="server" style="display:none;" />
Change your Extender to target said button:
TargetControlID="hiddenButton"
Then in your _Click event, show the modal:
protected void uxTicketHistoryButton_Click(object sender, EventArgs e)
{
DataTable ticketHist = _dtMgr.GetContactInfoByName(uxContactDropdownList.SelectedValue);
string rName = ticketHist.Rows[0]["RequestorName"].ToString();
string rPhone = ticketHist.Rows[0]["RequestorPhone"].ToString();
....
uxTicketHistoryModal.Show();
}

Related

Set focus back to the proper textbox after autopostback function (Page.SetFocus doesn't solve the issue)

Now, this MAY look a duplicate, but it's not.
Every solution on the internet shows you how to get focus on the textbox that fired the event.
But what if the user presses tab? The textbox that should have focus is the next one.
So we do a workaround and focus on the textbox that have TabIndex higher than the one that fired the event.
But then what if the user presses Shift+tab?
Even worse: what if the user clicks on another random textbox?
This is the issue.
I don't think a code is required here, because it's a general solution to set focus on textboxes that have autopostback function.
If code is required, please ask in the comments.
The following will allow you to do what you want:
What we need to do is have js assist with what control will be next, in this case any control that is getting focus (whether it be via tab, shift-tab, click, or whatever control combination leaps out of the text box and onto a different control). By utilizing a WebMethod we can pass this information onto the server for AutoPostBack focus.
WebMethod:
[WebMethod]
public static void set_nextFocus(string id)
{
_toFocus = id;
}
Simple enough, _toFocus is class variable static string _toFocus, it holds the value of the next control to focus.
Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
//sets focus to the proper control
Page.SetFocus(Page.FindControl(_toFocus));
}
}
JavaScript
in <head>
<script type="text/javascript">
function setFocus(x) {
PageMethods.set_nextFocus(x);
}
</script>
ASP controls
In this example, a TextBox. Note the use of OnFocusIn. It is an expando attribute of the ASP control which will realize there is no server-side definition, and revert to javascript's onfocusin attribute.
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" TabIndex="1"
ontextchanged="TextBox1_TextChanged" OnFocusIn="setFocus(this.id)" >
</asp:TextBox>
Also, in order to use PageMethods you must enable it within the form, like so:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
You can check the __EVENTTARGET property of form which will tell you the textbox name from which the event has been raised.
Scenario, say I have two textboxes named TextBox1 and TextBox2 for both AutoPostBack set to true and hooked up textChanged event to single handler TextBox1_TextChanged. You can have below code and set the focus back to the specific textbox control
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
string target = Request.Form["__EVENTTARGET"];
if (target == "Textbox2") //conrol name should be exact
{
Page.SetFocus(this.TextBox2);
}
}

Input "Reset" button fails to work when a grid is shown in page

I have an Advanced Find Form that contains a few asp:textbox controls as well as asp:dropdownlist controls. When I hit my reset button, it works well in clearing or resetting my textboxes and dropdownlists.
However, when the user clicks "Go" to submit the search query and a grid is shown with the results, the reset button no longer works.
Here's my input button:
<input type="reset" value="Clear All" />
EDIT:
Please note that I need to reset the fields to "Default Values" and also I need to be doing without a postback, to be doing it on client side
Have you try with normal button :
<asp:Button ID="txtResetbtn" runat="server" OnClick="txtResetbtn_Click" />
protected void txtResetbtn_Click(object sender, EventArgs e)
{
TextBox1.Text = string.Empty;
}
I think the problem is that HTML button don't reset after a postback
Or you can try to do this in the :
<asp:Button runat="server" ID="reBt" Text="Reset"
OnClientClick="this.form.reset();return false;"
CausesValidation="false" />
From : Reset a page in ASP.NET without postback
That link can help you too : Is there an easy way to clear an ASP.NET form?
The input type "reset" does not clear the form, it resets the form to its default values. To clear the form after submitting, you'll have to use javascript to set all values to empty.
Edit:
Since you're using asp.net, you could also use an <asp:button /> to call a method that clears the values of each control in the form.
Edit 2:
If you need to keep this function client-side, you'll have to use Javascript. Also, I think it's important to make a distiction between resetting to a default value (what the "reset" input type does) and clearing the values in a form, even after a submit. To do the later on the client side, you'll have to write a little javascript.
I managed to workaround the problem. As TenneC and Nikolay have mentioned in previous answers, I used an ASP.Net button but I've performed the reset function using jquery as follows:
$(document).ready(function(){
$("#BtnClear").click(function () {
$('#FillForm input').val(function () {
return this.defaultValue;
});
$('#DDLId1').val(function () {
return this.defaultValue;
});
$('#DDLId2').val(function () {
return this.defaultValue;
});
});
});

Changing button label from c# code-behind

I am sorry I can't solve what must be simple to everyone else even after reading so many posts. Simply stated... I have an ASP webpage I created in VS2010 and when the user clicks the 'Send Email' button, I want to change the button either using JQuery then calling my code-behind to do the actual send. Alternatively, I want the C# code behind to change the button text while it is in the process of sending so the user knows to hold on.
I am new to JQuery and still pretty new to C#. My website is www. themilwaukeehandcenter.com. If you click the contact us... this is where I want the code. I have tried
$("ContactUsButton").click(function () {
ContactUsLabel.Text = "Processing";
});
on the main page. I have also tried
protected void ContactUsButton_Click(object sender, System.EventArgs e)
{
ContactUsLabel.Text = "Sending... Please Wait";
ContactUsLabel.Refresh();
I know this should be simple but hours later.... reading so many posts later.... I feel no farther. I can't quite figure out how the JQuery onclick and the ASP onclick interact. I also don't know why C# flags the Refresh() as not valid. Any help welcome.
I actually placed a ContactUsLabel on the form too when I couldn't get the ContactUsButton to do what I intended. I am too knew to understand what you mean by 'How have you declared your button, but this is the code that creates it:
<asp:Button ID="ContactUsButton" runat="server"
OnClick="ContactUsButton_Click" Text="Send Email" Width="120px"/>
<asp:Label ID="ContactUsLabel" runat="server" Text="" Width="120px">
</asp:Label>
Your WinForm Code:
<asp:button id="ContactUsButton" runat="server" onclick="ContactUsButton_Click" cssClass="myButton" text="Contact Us" />
On submit use the jQuery script to change button text.
$(document).ready(function() {
$('.myButton').click(function(){
$(this).attr('value', 'Processing...');
});
});
View in fiddler: view
Finally at the server side:
protected void ContactUsButton_Click(object sender, System.EventArgs e)
{
ContactUsButton.Text = "Thank You";
}

asp.net button postbackurl call from every button

In an asp.net forms website I have a button which has a postpack url:
mailBUtton.PostBackUrl = "mailto:..."
As expected a click on the button opens the local e-mail software, but If an other button on the page is clicked after the mailButton was clicked, the onClick event of the second button is ignored and instead of the onClick event, the PostBackUrl of the mailButton is called.
What is the reason for these behavior?
This is the mailButton:
<asp:Button ID="mailButton" runat="server" Text="Neuer Beitrag" ></asp:Button>
and this the second button:
<asp:Button ID="btnSpeichern" runat="server" Text="Speichern" OnClick="btnSpeichern_Click"/>
and this the event in the code behind:
protected void btnSpeichern_Click(object sender, EventArgs e)
{
speichern(false);
}
According to w3schools the PostBackUrl of a button is
A string specifying the URL of the page to post to. Default is an empty string, this causes the page to post back to itself
So when you click the mailButton you're setting the pages PostBackUrl to mailto:.... By clicking on the second button a postback is performed which now calls the mailto: url instead of the original one, where also is no btnSpeichern_Click method.
Instead of using a button and PostBackUrl you could do the following:
<asp:HyperLink runat="server" NavigateUrl="mailto:abc#abc.com" Text="abc#abc.com"></asp:HyperLink>
If you want your link to appear like a button, you can do that in your css.
Here are some more approaches of using mailto.

Triggering AjaxControlToolkit ModalPopupExtender Cancel Control

My ModalPopupExtender has:
CancelControlID="btnClose"
<asp:Button ID="btnClose" runat="server" Text="Close" Width="50px" />
btnClose can not have an OnClick event. System simply does not work if it does.
My popup form has 3 other buttons, Save. Delete and Cancel.
If any of these are used, the Wizard Next button and the Sidebar Links no longer work. They only work if btnClose is used. I assume it does something to tell the system the popup is closed. How do I make my Save / Delete / Cancel buttons do the same thing? cam I trigger a btnClose.Click?
you can close ModelPopupExtender from code behind as well as from java script based up on your requirements.
C#:
ModalPopupExtender1.Hide();
Javascript:
$find('ModalPopupExtender1').hide(); //keep this in a function an call the same function where ever you want

Categories

Resources