Master and content pages with javascript in asp.net 2.0 - c#

Hi i'm very new to .net programming.I want to run a javascript from a content page containing a button. I'm able to display a button but dont know how to bind action. Help me in this regard.

I think it should be easy to Attach a JavaScript function to the control by doing the following:
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="javascript:JSFunctionName();" />
If you have a specific problem then let me know ?

Related

How to handle two different forms in an asp.net web page?

I have a login page in my asp.net website (using C#) and it has a "login form" which has an email text box, a password text box and a login button.
In addition, I have a "search form" at the top of the web page which has a search text box and a search button.
*All of the controls are in the same form because of the asp.net limit for one runat="server" form.
The problem is that when I type something to search for and click ENTER (and not directly the button) it doesn't do anything, only runs the Page_Load again. Same thing when I click ENTER in the login section instead of directly on the login button.
I have tried different solutions but they were problematic because of the different functionality of the two "forms".
I have no idea how to solve this, any suggestions?
my web forms is a little rusty but I think something like this:
<asp:Panel ID="loginPanel" runat="server" DefaultButton="loginButton">
<%-- Login Stuff--%>
<asp:Button ID="loginButton" runat="server" />
</asp:Panel>
<asp:Panel ID="loginPanel" runat="server" DefaultButton="searchButton">
<%-- Search Stuff --%>
<asp:Button ID="searchButton" runat="server" />
</asp:Panel>
should work
Only one form with runat='server' available on aspx page. So, if you really need another form, you may use its without runat='server' and use for search jQuery ajax call.

Issue related to getting ClientID of asp controls in Javascript

I have asp.net webApplication having asp controls.
<asp:HiddenField ID="hdTime" runat="server" />
To Access above control in Javascript i have used $('#ContentPlaceHolder1_hdTime').val('AM');
it works fine in Mozila firefox but in InternetExplorer it takes
$('#ctl00_ContentPlaceHolder1_hdTime').val('AM');
i have also tried
$('#<%= hdTime.ClientID %>')
but above syntext works only on .aspx page but when i use javascript.js file it doesn't find as $('#<%= hdTime.ClientID %>')
so how to access asp controls in .js file??
Thanks
You can set Clientidmode="static" for the control..
<asp:HiddenField ID="hdTime" runat="server" Clientidmode="static"/>
Javascript:
//Accessing control in javascript
var abc=document.getelementbyid('hdTime').value;
Try using static client side ids:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode(v=vs.110).aspx
This issue is because the ids generated by webforms are dynamic, so you cannot hard code them. However, webforms 4 introduced static client side ids to solve the issue you are having.
For example, add this attribute to your control: ClientIDMode="Static", and then you can reference your control in JavaScript like this:
$('#hdTime')
If you can't use webforms v4 then you will have to put your JavaScript in the aspx page.
You can use like this : <asp:HiddenField ID="hdTime" runat="server" ClientIDMode="Static" />
you can find the ID of the control, also important is to give ClientIDMode="Static"
var id = Document.getElememtById("hdTime").value;
or
var id = $("#hdTime").val();

Multiple forms on page asp.net

I have a problem with multiple forms on my asp.net website. On the Master page i have a small form for every page and on some pages i also want to have a form. I know it is not possible to have for form tags with runat=server, so i am searching for a solution.
Is it possible to deactivate the validators from one of the forms, so the other form can pass? Then i could manage the 2 forms together in the server-side c# code.
Is this possible or are there any other solutions u know?
you can give your validation control a group name like
<asp:RequiredFieldValidator ControlToValidate="TextBox2" ValidationGroup="Login" ID="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
and use that group name to the button you want to validate on click like
<asp:Button ID="Button1" runat="server" Text="Login" ValidationGroup="Login" />

Auto Fill Textbox in ASP.NET depending on the text typed in another textbox

how to fill textbox in asp.net while i am typing in another text
.. changes in one textbox will affect in another textbox auto.. and without refreshing my page.
Ok, try this. You will need the AJAX Control Toolkit. So read the article Installing AJAX Control Toolkit 4 in Visual Studio 2010 to see how to install it in Visual Studio.
Then you need to add a ScriptManager to your ASPX page. You will need to add the following code:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
What you then need to do, is add an UpdatePanel to your page. Inside this update panel, you need to place the textbox. This means that only the controls inside the update panel will refresh, and not the whole page. To do this, add the following code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<!--Add your Textbox Control to update here: Textbox1-->
<asp:TextBox ID="Textbox1" runat="server" ReadOnly="True"></asp:TextBox>
<asp:TextBox ID="Textbox2" runat="server" ReadOnly="True" ontextchanged="Textbox2_TextChanged"></asp:TextBox>
</ContentTemplate>
<Triggers>
<!--This is the textbox you will be typing text into: TextBox2-->
<asp:AsyncPostBackTrigger ControlID="Textbox2" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
The Trigger tells your page which control on the form needs to initiate the postback. Now in your .cs file, you need to add the event handler for the Textbox2 TextChanged event. Add the following code:
protected void Textbox2_TextChanged(object sender, EventArgs e)
{
// Set the text of textbox1 = textbox2
}
I hope that this helps.
No need for AJAX. JQuery is enough. The code will do it
$('#text1').bind('keyup', function(){
$('#text2').val($('#text1').val());
});
Assuming
text1 id of box writing into.
text2 textbox that text gets copied to
in .Net you will have to use client id to get the correct id so it may look like this
$('<%=text1.ClientID%>').bind('keyup', function(){
$('<%=text2.ClientID%>').val($('#text1').val());
});
Oh and wrap it up in $(document).ready as per standard. And of coures you need to include the JQuery library to your page.
No posting back or page refreshes at all. It's your lightest solution and easy to implement.
You will need to use Javascript to accomplish this. ASP.Net code runs on the server side, which means it cannot affect the page without a postback happening first. Read up on the OnTextChanged event and how to hook into it with javascript. There is a javascript library called jQuery which makes everything easier, though it isn't strictly necessary.
Use JQuery. You may need to make an AJAX call to the server if you are relying on a datasource such as a database to autofil this field.

Linkbutton OnCommand not firing after upgrading to .Net 4

I have a webforms project that was written in ASP.Net 3.5 and works fine. I decided to upgrade it to 4.0 for various reasons. The upgrade went fine. However, on one particular page, I have certain linkbuttons that fire an event in code behind. The event never fires on click. Other controls on the page, like an link button with client side code and Telerik radgrid work fine.
This Works
<asp:LinkButton ID="InsertLinkButton" runat="server">
<asp:Image ID="ImageButton1" runat="server" />Add New Employee</asp:LinkButton>
//Code behind adding javascript event
InsertLinkButton.Attributes["onclick"] = String.Format("return ShowInsertForm();");
This Doesn't
<asp:LinkButton ID="ALinkButton" runat="server" CommandName="A" OnCommand="LinkButton_Command" ">A</asp:LinkButton>
The page is a content page with a master page. Any ideas?
I'm not sure if this is just a typo on your post but your markup isn't correct on the LinkButton.
It is currently:
<asp:LinkButton ID="ALinkButton" runat="server"
CommandName="A" OnCommand="LinkButton_Command" ">A</asp:LinkButton>
It should be:
<asp:LinkButton ID="ALinkButton" runat="server"
CommandName="A" OnCommand="LinkButton_Command">A</asp:LinkButton>
The panel containing the controls was referenced as an updated control in a Telerik RadAjaxManager buried in the mark up. If I removed the RadAjaxManager, it worked. Apparently the linkbutton command event didn't fire the ajax callback. Since this worked in 3.5, I'm wondering if some metadata didn't get converted properly during the 3.5>4 upgrade?
Anyway, I updated the references of what was supposed to fire an ajax call from the RadAjaxManager and it now works. Thanks for the help!

Categories