ASCX page Button click not firing server side - c#

I have a Master page which is inherited in a aspx page named Employee. Below is the relevant code of the aspx page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Employee.aspx.cs" Inherits="ICA_Nagerbajar.Employee" MasterPageFile="~/Master.Master" %>
<%# Register TagPrefix="e" TagName="Entry" Src="~/EmployeeView/EmployeeEntry.ascx" %>
<%# Register TagPrefix="v" TagName="ViewEdit" Src="~/EmployeeView/EmployeeViewEdit.ascx" %>
<asp:Content ID="ContentBody" ContentPlaceHolderID="MasterBodyPlaceHolder" runat="server">
<div class="tabbable">
<ul class="tabs">
<li>Entry</li>
<li>View/Edit</li>
</ul>
<div class="tabcontent">
<asp:HiddenField ID="TabData" runat="server" />
<div id="tab1" class="tab" style="display:inline-block">
<e:Entry ID="EmpEntry" runat="server" />
</div>
<div id="tab2" class="tab" style="display:inline-block">
<v:ViewEdit ID="EmpViewEdit" runat="server" />
</div>
</div>
</div>
</asp:Content>
Now in one of the ascx page (EmployeeEntry.ascx) Button is working fine. But in the other one(EmployeeViewEdit.ascx) it is not working. Below is the relevant code of EmployeeViewEdit.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="EmployeeViewEdit.ascx.cs" Inherits="ICA_Nagerbajar.EmployeeView.EmployeeViewEdit" %>
<div class="boxed">
<input id="TextBoxUserNameSearch" runat="server" class="textstyle" placeholder="Name" type="text"/>
<asp:Button ID="ButtonSearch" runat="server" Text="Search" class="btnsubmitstyle" OnClick="ButtonSearch_Click"/>
</div>
Server Side Code
#region Search Button Click
protected void ButtonSearch_Click(object sender, EventArgs e)
{
string userName = TextBoxUserNameSearch.Value.Trim();
GetEmployee(userName + "%");
}
#endregion
Few Observations i made
If i replace Button with Link Button its working fine
No Button is working in this ascx though its working in the other ascx
If i comment out the first call to ascx in the parent page then the button is working ie, <e:Entry ID="EmpEntry" runat="server" />

As suggested by #JBKing setting UseSubmitBehaviour property of the Button to "false" solved my problem
<asp:Button ID="ButtonSearch" runat="server" Text="Search" CssClass="btnsubmitstyle" OnClick="ButtonSearch_Click" UseSubmitBehaviour="false"/>

Related

asp.net c# customer control on content page child of master page

My application was working before converting it to an asp.net Master/Content page architecture. There is a custom control that has 3 buttons in one DIV and a chart in another DIV. The custom control is added to the content page many times with a different chart in each. The same event handler services a button, regardless of how many times it appears on the screen.
After converting to a Master/Content page architecture, the event handler no longer receives the onClick event.
The Content Page looks like this...
<%# Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm7.aspx.cs" Inherits="OperationalStats.Forms.WebForm7" %>
<%# Register src="../Classes/UserControls/ChartWidget.ascx" tagname="ChartWidget" tagprefix="uc1" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>
And the user control page looks like this
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="ChartWidget.ascx.cs" Inherits="OperationalStats.Classes.UserControls.ChartWidget" %>
<div id="TestDiv" class="floating-box" runat="server" style="border: medium solid #00FF00; height: 270px; width: 250px">
<div id="MenuDiv" runat="server">
<asp:Button ID="FilterButton" runat="server" Text="Filter" class="btn btn-info btn-sm" data-toggle="modal" data-target="#myModal" OnClick="FilterButton_Click" CommandName="Tests Delivered By Test Center" />
<asp:Button ID="ZoomButton" runat="server" class="btn btn-info btn-sm" Text="Zoom" data-toggle="modal" data-target="#myModal" Width="50px" OnClick="ZoomButton_Click" />
<asp:Button ID="ShowTableButton" runat="server" class="btn btn-info btn-sm" Text="Table" data-toggle="modal" data-target="#myModal" Width="50px" OnClick="TableButton_Click" />
</div>
<div id="ChartDiv" runat="server" style="position: relative; top: 6px; left: 2px; ">
</div>
</div>
The content declaration in the Master page looks like this
<div class="container body-content">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
<hr />
</div>
The following is an example of a button's event handler
namespace MyApp.Classes.UserControls
{
public partial class ChartWidget : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void FilterButton_Click(object sender, EventArgs e)
{
...
}
As I say, the event handler never receives the click event after converting to a Master/Content page architecture.
Thoughts?

ModelPopupExtender not showing from code behind on pageLoad

I need to display popup when page loads. I have used ModelPopupExtender. It does shows on it's TargetControlID click but not showing on page load from code behind.
I have check many solutions on stack but none of them are working for me.
protected void Page_Load(object sender, System.EventArgs e)
{
this.mpOTP.Show();
}
<asp:Button ID="activateMpOtp" runat="server" Text="Open" ClientIDMode="Static"/>
<asp:ModalPopupExtender ID="mpOTP" runat="server" BackgroundCssClass="popup-overlay" PopupControlID="pnlOTP" CancelControlID="closeOTP" TargetControlID="activateMpOtp"></asp:ModalPopupExtender>
<asp:Panel ID="pnlOTP" runat="server" CssClass="popup-dialogue">
</asp:Panel>
Getting this error in console
Your markup is not correct.
First of all, you need to register AjaxControlToolKit controls by using the following directive at the top of your page markup, just after the page directive.
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="act %>
Then, your markup should look like the following. Problems with your markup are listed below.
You have not included markup for the cancel button
and also your control prefix is wrong since it should not be asp which is the prefix is for ASP.Net Microsoft controls and not for AJAXControlToolKit (NOTE: you give this prefix in your register directive for AjaxControl,Toolkit, which is act in code sample given, but you could give any prefix and use it for modalpopuextender)
Correct markup for ModalPopupExtender
<asp:Button ID="activateMpOtp" runat="server" Text="Open" ClientIDMode="Static"/>
<act:ModalPopupExtender ID="mpOTP" runat="server" BackgroundCssClass="popup-overlay"
PopupControlID="pnlOTP" CancelControlID="closeOTP"
TargetControlID="activateMpOtp"></act:ModalPopupExtender>
<asp:Panel ID="pnlOTP" runat="server" CssClass="popup-dialogue">
<div class="header">
Modal Popup
</div>
<div class="body">
Your content goes here
<br />
<asp:Button ID="closeOTP" runat="server" Text="Close" />
</div>
</asp:Panel>

UserControl in asp .net is not working?

I have created a two usercontrols in asp.net and one webform . Now i want these two usercontrols to show in form in webform but it say that there must be one head with runat="server"
this is webform where i am using UserControl!
<%# Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Light.master" CodeBehind="AdministrationPage.aspx.cs" Inherits="DXApplication5.AdministrationPage" %>
<%# Register src="~/AdminPage/AssignmentTab.ascx" tagname="AssignmentUC" tagprefix="uc1" %>
<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
<table border="0">
<tr>
<td>
<div class="accountHeader">
<uc1:AssignmentUC ID="CalendarUserControl1" runat="server" />
</div>
</td>
</tr>
</table>
</asp:Content>
This is UserControl below:
<%# Control Language="C#" ClassName="AssignmentUC" AutoEventWireup="true" CodeBehind="AssignmentTab.ascx.cs" Inherits="DXApplication5.AdminPage.AssignmentTab" %>
I would add a single form to your masterpage, this may be the cause of your error.
I would also remove all other form server controls from your user controls and pages.
Try these steps:
Go to Light.master master page file and make sure that this is in there somewhere <form id="form1" runat="server"> and a closing tag, the id may be different.
Go to the following files AssignmentTab.ascx and AdministrationPage.aspx and remove any <form id="form1" runat="server"> and closing tags </form>
Check user control code if it contains a head element. Also check all dependencies to see if there are head elements present in them.
I think you have use <form id="formID" runat="server"> in both of your page and user Control .
Just remove runat="server" from your user Control Form tag .
Make sure you have in your user control cs file:
public partial class WebUserControl1 : System.Web.UI.UserControl
In ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebUserControl1" %>
In aspx parent:
<%# Register Src="WebUserControl1.ascx" TagPrefix="uc" TagName="WebUserControl1 " %>
<uc:WebUserControl1 runat="server" ID="mycontrol" />

How to change the value of an asp:textbox from javascript

I have an asp:textbox
<asp:textbox ID="NewName" runat="server" />
and when a user clicks the "Cancel" button, I want to erase whatever is in that text box by making the value just be "". If it were a an input field of type text, I could just do this in javascript:
document.getElementById('NewName').value= "";
But I can't just do this because it is an asp:textbox.
When your asp:textbox is rendered to the UI, then yes, it is just an input field of type text. The only part that may be an issue is the ID, but that can be set using ClientIDMode:
<asp:textbox ID="NewName" runat="server" ClientIDMode="Static" />
renders to:
<input type="text" id="NewName" />
and thus the JavaScript you use should work perfectly.
<asp:TextBox> renders to an <input type="text"> which you can use as you would, the only complication is the id="" attribute is rewritten, but you can do this:
document.getElementById("<%= NewName.ClientID %>").value = "";
Assuming that this script is in the same page as the textbox. If this is in a referenced <script src=""> then you'll need to use a global script variable of the element's ID.
Try setting the textbox's ClientIDMode to Static
That way the javascript should be able to find them with the same ID as in ASPX.
Here a whole test page
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript">
function ClearTB1() {
document.getElementById("TextBox1").value="";
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<p>
<asp:TextBox ID="TextBox1" runat="server" Text="test" ClientIDMode="Static"></asp:TextBox><br />
<asp:TextBox ID="TextBox2" runat="server" Text="test" ClientIDMode="Static"></asp:TextBox><br>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="ClearTB1()"
CausesValidation="False" UseSubmitBehavior="False" /><br />
</p>
</asp:Content>
I had the same problem. I solved it once I remembered there is a mangling going on with asp.net on the Ids.
within my aspx, i had:
<input type="text" id="sessSubs"/>
further down, within a gridview (hence the use of "class" instead of "id"), i had a column which had an asp:TextBox:
<asp:TextBox runat="server" class="paid" />
once i recalled the id mangling issue, it was just a case of changing the way i referenced the textbox:
<script type="text/javascript">
$(document).ready(function() {
function updateSubs()
{
var sub = parseFloat($('[id$=sessSubs]').val());
$('.paid').val(sub);
}
$(document).on('change, keyup', '[id$=sessSubs]', updateSubs);
});
</script>

User Control button not firing from webform page

I have created a user control for visitor to subscription of newsletter.
UserControl is withing the update-panel and is added to the main master-page.
Problem with the control is that Subscribe button is not firing for some reason
User control markup:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table cellpadding="0" cellspacing="0" class="PrayerTimeWrapper">
<tr>
<td align="center">
<table cellpadding="0" cellspacing="0" class="PrayerTimeInnerWrapper" border="0">
<tr>
<td valign="top">
<div class="dHeading"><asp:Label ID="lblTitle" runat="server" Text="JOIN US"></asp:Label></div>
<div class="dName">
<asp:TextBox ID="txtName" CssClass="txtSubscribe" runat="server" Text="NAME" onfocus="if(this.value=='NAME')this.value='';" onblur="if(this.value=='')this.value='NAME';"></asp:TextBox>
</div>
<div class="dEmail">
<asp:TextBox ID="txtEmail" CssClass="txtSubscribe" runat="server" Text="YOUR EMAIL" onfocus="if(this.value=='YOUR EMAIL')this.value='';" onblur="if(this.value=='')this.value='YOUR EMAIL';"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEmailSub" runat="server" ErrorMessage="*"
ControlToValidate="txtEmail" ValidationGroup="SubEmail" ></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revEmailSub" runat="server"
ErrorMessage="*" ControlToValidate="txtEmail" ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
ValidationGroup="SubEmail" ></asp:RegularExpressionValidator>
</div>
<div class="dSubmit">
<asp:Button ID="btnSubscribe" CssClass="btnSubscribe" runat="server" Text="Subscribe" onclick="btnSubscribe_Click" />
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
User control code-behind:
protected void btnSubscribe_Click(object sender, EventArgs e)
{
Response.Write("Test");
}
Markup of the page which is using master-page:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="PrayerTiming.aspx.cs" Inherits="PrayerTiming" %>
<%# Register Src="~/en/UserControls/ucSubscribe.ascx" TagName="Subscribe" TagPrefix="uc"%>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
<div align="center" id="table"></div>
<uc:Subscribe id="ucSB" runat="server" UpdateMode="Conditional" />
</asp:Content>
I am doing something wrong somewhere but i am not sure what. I would appreciate help on this.
Using Resonse.Write during an asynch resquest can altere the data used to update proprely the controls within the update panel.
so instead of Response.Write("Test") use a Label.Text = "Test"
Set the ValidationGroup for the btnSubscribe button also
<asp:Button ID="btnSubscribe"
CssClass="btnSubscribe" runat="server"
Text="Subscribe"
onclick="btnSubscribe_Click"
ValidationGroup="SubEmail" />
UPDATE
From what I've seen in the comments and in the other answers, the only reason why the button didn't post the content to the server is because it didn't subscribe to that validation group. The ValidationGroup is used to separate certain views (group of controls) in a page so they use their own validation. For example a forgot password section and a login section would have two different validation groups so when a certain button is clicked only its section is validated.
I did this update, because I truly think that the accepted answer is more a debuging advice than an actually answer. A future SO reader might jump to use this guideline instead of seeing the problem.
Try puting UpdatePanel in form tag with runat="server"...

Categories

Resources