I've created an ASP.NET button like so:
<asp:Button ID="btnSaveCoords" runat="server" OnClick="btnSaveCoords_Click" Text="Save Changes" CssClass="btn btn-warning" />
and the event in the OnClick is defined as follows:
protected void btnSaveCoords_Click(object sender, EventArgs
{
System.Diagnostics.Debug.WriteLine("Saving Coordinates...");
}
When I click the button, btnSaveCoords_Click isn't fired. What am I doing wrong?
The firs row in the .aspx markup file should be like this:
<%# Page Language="C#" AutoEventWireup="true"
CodeBehind="WebForm1.aspx.cs" Inherits="SportStore.Views.WebForm1" %>
Related
Control 1:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="cntrlIPSEntry.ascx.cs" inherits="ServiceManagement.Control.cntrlIPSEntry" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxtoolkit" %>
<asp:TextBox ID="txtModel" CssClass="form-control" runat="server"></asp:TextBox>
another control:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="CustomerInfo.ascx.cs" Inherits="ServiceManagement.Control.CustomerInfo" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxtoolkit" %>
<%# Register Src="~/Control/cntrlIPSEntry.ascx" TagPrefix="uc1" TagName="cntrlIPSEntry" %>
<asp:Button CssClass="btn btn-default" ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"/>
in C# i want to catch control 1 data of txtModel
protected void btnSave_Click(object sender, EventArgs e)
{
//??
}
I think what you are looking for is FindControl. You first find the Control holding the TextBox in the Parent, in this case the Control with ID WebUserControl1. Then it's just a matter of finding the correct TextBox and cast it as one.
TextBox textBox = this.Parent.FindControl("WebUserControl1").FindControl("txtModel") as TextBox;
Label1.Text = textBox.Text;
I am having an issue with the asp.net button. It is not firing event. I tried setting causes validation to false and removing the java script and validation but it still doesn't work.
<asp:Button ID="Button2" runat="server" Text="Save" onclick="Button2_Click" />
protected void Button2_Click(object sender, EventArgs e)
{
std.AddGuardianInfo(
Convert.ToInt16(DropDownList1.SelectedValue),
TextBox6.Text,
TextBox7.Text,
TextBox8.Text,
TextBox9.Text,
TextBox10.Text);
Response.Redirect("Std_FeeInfo.aspx");
}
Change onclick to OnClick in the markup of the asp:button. so the markup will be like this:
<asp:Button ID="Button2" runat="server" Text="Save" OnClick="Button2_Click" />
And an important advise for you: Use css for styling and arranging elements in your page, giving space using a sequence of will not be a good design
You want to check this steps.
#rabiya are you copied this code in some where else ? Then just remove it and double click on your asp button.
If you are using updatepanel on onclick event, this may happen.So Use 'EnableEventValidation="false"'
Example :
<%# Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
I declared Linkbutton control in Gridview but in code behind i could not access that. Below is my aspx page code.
<%# Page="" Language="C#" AutoEventWireup="true" MasterPageFile="~/MainMaster.Master"
CodeBehind="Page.aspx.cs" Inherits="IntakeLibrary.Page" %>
<%# Register="" Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="updatePanel" runat="server">
<contenttemplate>
<asp:GridView ID="grdView" runat="server">
<Columns>
<asp:TemplateField HeaderText="Text">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</contenttemplate>
</asp:UpdatePanel>
</asp:Content>
Below is my codebehind code.
protected void Page_Load(object sender, EventArgs e)
{
LinkButton1.Text = "Test";
}
Below is the error i am getting
The name 'LinkButton1' does not exist in the current context
You don't have a LinkButton on your page - you have a LinkButton in the ItemTemplate in one column of a GridView in the template of your UpdatePanel. You'll need to reach down past all those layers before you'll be able to reference the LinkButton itself.
You are using the wrong method, the correct way is to assign the text in the button control tag itself. This button is inside a gridview so it will be repeated. You cannot reference suppose 10 rendered buttons with one property right? You have to loop through and change their text on row data bound event.
You would probably have to use a DataGrid_RowDataBound event handler and get a handle to the control in the correct template type and then on the item passed into the event handler do a find control on the link button. I'll get a code example up shortly.
Below is my code:
default.aspx:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"
AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="TestAjax._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
//I set an onClick event on this button:
< input type = "button" id="btnCl" onclick = "doJob" />
</asp:Content>
default.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
//fires on page load;
}
void doJob()
{
//code here;
}
The question is:
Why didn't the onclick event trigger? (On default.aspx btnCL)
Thanks
You need to use an asp:Button control like this:
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
and then have the event method like this:
protected void Button1_Click(object sender, EventArgs e)
{
// your code here
}
When using a regular input tag, onclick is for javascript, you have to instead use the ASP.NET control asp:Button in order to hook up the C# method.
As stated in my comment in OP your current method is making a client-side call (so looking for a JavaScript function called doJob. You need to make a server-side call so need to use an <asp:Button> control. Some example of how you could achieve this:
Web-Page (.aspx)
<asp:Button ID="btnDoJob" Runat="server" Text="Do Job" OnClick="btnDoJob_Click" />
Code-Behind (.CS)
protected void btnDoJob_Click(object sender, EventArgs e)
{
// Do your action here...
}
Because dojob() is a server-side function, but onclick on that input element is a client-side event. You're probably getting an error in your JavaScript console saying that dojob() is an undefined function.
Use an asp:Button instead of an input to make use of server-side click events. Also, dojob() should be protected. By not declaring a protection level I think the default is private so the page controls might not even be able to see it. It should match the event handler for a button click:
protected void dojob(Object sender, EventArgs e)
{
}
In my Summary page, I have two CalendarExtender controls to enable someone to select Start Date and End Date for database queries.
In the head of my Summary.aspx page, I have the following declarations:
<%# MasterType VirtualPath="~/Site.Master" %>
<%# Page Title="ACP Sheet Metal - Summary" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Summary.aspx.cs" Inherits="AcpSheetMetal.Summary" UICulture="es" Culture="es-MX" %>
<% #Import Namespace="System.Globalization" %>
<%# Register TagPrefix="asp" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit"%>
For the page itself, I have a ToolkitScriptManager, two (2) TextBox controls, two CalendarExtender controls, and a GridView control:
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager2" runat="server" EnableScriptGlobalization="true" EnableScriptLocalization="true" />
<asp:TextBox ID="txtStartDate" runat="server" />
<asp:CalendarExtender ID="calExStartDate" runat="server" TargetControlID="txtStartDate" OnClientShown="ChangeCalendarView" OnClientDateSelectionChanged="checkDate" />
<asp:TextBox ID="txtEndDate" runat="server" />
<asp:CalendarExtender ID="calExEndDate" runat="server" TargetControlID="txtEndDate" OnClientShown="ChangeCalendarView" OnClientDateSelectionChanged="checkDate" DaysModeTitleFormat="MM/dd/yyyy" DefaultView="Months" Enabled="True" TodaysDateFormat="MMMM dd, yyyy" />
<asp:GridView ID="summaryGridView" runat="server" />
In the Page_Load event in the C# code, I have placed the following:
protected void Page_Load(object sender, EventArgs e) {
MasterPage = (SiteMaster)Page.Master;
if (!Page.IsPostBack) {
calExEndDate.TodaysDateFormat = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
calExStartDate.TodaysDateFormat = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
}
}
So, why are my Calendar controls not working? There are no values in the calendars and the language appears to be Spanish.
Just add EnableScriptGlobalization="true" in your ScriptManager like this:
Your language is Spanish and I believe it's so because your UICulture is UICulture="es" (ESpañol) and Culture="es-MX" (Español México).
See this line on your markup:
<%# Page Title="ACP Sheet Metal - Summary" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Summary.aspx.cs" Inherits="AcpSheetMetal.Summary" UICulture="es" Culture="es-MX" %>
The rest of your markup looks okay to me.