I'm trying to develop some web page in Asp.Net and C# which almost look like facebook login page where user can sign in at top of the page and sign up at the bottom, how should I do that? and when the focuses are on the textboxs of sign in part and the user hit the Enter it should go to Sign in method,
I already searched in stackoverflow about it and it seems all questions which already asked were about Asp.Net MVC which I'm not using.
You can assign a single event to multiple button clicks -
topButton.Click += SignInButtonClick;
bottomButton.Click += SignInButtonClick;
void SignInButtonClick(object sender, EventArgs e)
{
//your code here
}
I presume in your case the same logic will run for both buttons, you could find out which button was clicked using sender argument that is passed into the function.
As an example, let's say I have an aspx page and I add a button to it called 'Button1' I can then add a click event via Visual Studio and it will create a methiod to handle the click event for me called Button1_Click. The method will be automatically linked to the button as the AutoEventWireup up property in c# is set to true by default.
Now, if I add a second button and call it 'Button2' but I want that button to fire the same event handler as the one used for Button1, I could add this code to the pages 'Page_Load' event handler
Button2.Click += Button1_Click;
Both buttons would then cause the 'Button1_Click' method to run when clicked.
you rather use html Controls then Asp.net Control and can use Jquery to perform the loginb or signup
$('#login').click(function(event)
{
// call
// $.ajax(),$.get or $.post method for ajax call
});
$('#signup').click(function(event)
{
// call
// $.ajax(),$.get or $.post method for ajax call
});
</script>
<form id="form1">
<input type="button" value="login" id="login"/>
</form>
<form id="form2">
<input type="button" value="signup" id="signup"/>
</form>
you can connect multiple buttons to the same method by doing something like in the page load:
lbTest1.Click += Test_Click;
lbTest2.Click += Test_Click;
void Test_Click(object sender, EventArgs e)
{
//your code here
}
you can use the enter button to fire the method of a button by wrapping both the textbox as the button in a panel and set the defaultbutton property of the panel.
The simplest solution is to wrap the related controls (e.g. the signup controls) in a Panel control, and set the DefaultButton property on the Panel. You can read about it in this tutorial
Related
I am new in ASP.NET and I have a problem..
In my code, btn.Click event does not work for multi buttons what created in tab button control. Can you guys advice me?
Thanks,
<asp:Button Text="TAB MENU" BorderStyle="None" ID="Tab" CssClass="Initial" runat="server" OnClick="Tab_Click" />
<asp:Panel ID="panel1" runat="server" Direction="LeftToRight" HorizontalAlign="Left"></asp:Panel>
protected void Tab_Click(object sender, EventArgs e) {
foreach (...) {
Button btn = new Button();
btn.Click += Button1_Click;
panel1.Controls.Add(btn);
}
}
protected void Button1_Click(object sender, EventArgs e) {
**some code here! but does not work.**
}
Try this
Button btn = new Button();
btn.Click += new RoutedEventHandler(Button1_Click);
panel1.Controls.Add(btn);
Your problem probably lies along the lines of the fact that the button does not exist when the Button1_Click callback is fired... Asp.net webforms is a somewhat leaky abstraction over HTTP, and therefore it's a bit tough to decipher sometimes, but I expect what is happening is that when the postback from your button click occurs, the button doesn't actually EXIST in the control tree, because that button only gets added to the control tree when a tab control is clicked. So because the button does not exist on the postback, webforms doesn't know what to do with the event, so it ignores it...
If your buttons must be created dynamically, consider making them do some javascript to edit some hidden field or something, the value of which you can inspect in the Page_Load method, and then do whatever you want to do functionality-wise with that value.
If they are NOT dynamic, and instead will be the same for each page load (but different for each tab - ie, when the page is loaded, you're drawing some info from a db to decide what buttons to display, but that will not change from postback to postback of the same page), then consider creating the buttons in the Page_Load event instead of inside the event that is raised when the tab is created.
ALTERNATIVELY, since in your comment you suggest that the buttons will be used to open a new window, why not just make the buttons do that client side, ie with javascript window.open commands. Then you don't even need to postback to the server at all...
I currently have a master page that has a drop down menu which displays a different User control depending on the menu choice.
Inside of the User control I'm trying to add a submit button that processes the inputted forms inside the user controls using the "_Click()" event handler. My user control aspx looks like the following:
<asp:Button ID="configBttn" runat="server" OnClick="configBttn_Click" AutoEventWireUp="true" />
and then I have the following in the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void configBttn_Click(object sender, EventArgs e)
{
Response.Write("Hello world");
}
I have even tried to add an event handler:
configBttn.Click += new EventHandler(configBttn_Click)
However this is still not allowing for the button to be triggered. Any ideas?
I add the user control inside the codebehind inside a div container:
divContainer.Controls.Add(pPage.LoadControl("~/Controls/Control.ascx"));
Where are you creating the UserControl in the Page Lifecycle?
I suspect that it needs to be created in the Init method of its container page.
In addition, they should not be wrapped in a !IsPostback block, or else when the page posts back, the control event handler is not mapped, so nothing is there to catch the click event.
Without seeing the code that adds the user control, I'm guessing that it is not saved as part of the page's view state. Which means when you click the button inside the user control the whole page gets refreshed and the user control is either not created or a different instance of it is created.
I have some buttons on a web form in my ASP.NET project. The end user has to interact with these buttons like you would with a calculator. My issue is that ever time the end user clicks a button the page is refreshed because of postback. I tried to turn postback off like so:
if (!Page.IsPostBack)
{
protected void btn7_Click(object sender, EventArgs e)
{
const string stringNumber7 = "7";
var text = txtNumber.Text;
text += stringNumber7;
txtNumber.Text = text;
CheckMaxLength();
}
}
But then the click event will not fire. I have tried using images, image buttons, image maps. I can't get the result I want from those object either. I would like to be able to stop postback, but still have my button click event to fire like the would in a windows app. I have see some forums talking using a update panel. I am not sure how to implement an update panel correctly. I am not familiar with AJAX controls.
Does anyone know how I can accomplish want I want to do using an update panel? Or how to stop postback and still fire server side code?
If the botton is invoking local / client side JavaScript, return false within the control event attribute which will cancel the PostBack.
<button onclick="MyMethod();return false;">Click Me</button>
of if you are using a server side control:
<asp:button runat="server" OnClientClick="MyMethod();return false;" />
I wanted to create a "Click" event for a textbox in C# (as there isn't any).
So, this way
protected void Page_Load(object sender, EventArgs e)
{
if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "txt1OnClick")
{
txt1_Click();
}
txt1.Attributes.Add("onclick", this.Page.ClientScript.GetPostBackEventReference(txt1, "txt1OnClick"));
}
private void txt1_Click()
{
ImageMap1.ImageUrl = "guide/1.jpg";
}
Then I wanted to load the image without reloading the page.
So I used the AJAX UpdatePanel Control and this worked fine with
protected void Button1_Click(object sender, EventArgs e)
{
ImageMap1.ImageUrl = "guide/1.jpg";
}
But not with the event I created, because the compiler doesn't identify my new events as
a real event or something I couldn't figure out.
I added the button1_click event according to Step 8 of "Refreshing an UpdatePanel Control with an External Button".
The click event of textbox is not shown in this option:
So my question is is there any way to add this event within System.Web.UI.WebControls.TextBox class or, to make this event visible within the above option?
So that I can include click event of the textbox within the Triggers of the update panel.
If you try to create a Click event for a TextBox, every time a user clicks your textbox you'll trigger a postback to the server (even to evaluate if you need to do something as part of handling the event). This is very inefficient - you should handle clicks in the browser, using JavaScript and then trigger the UpdatePanel using client-side logic.
This lets you trigger a call to the server if you need it but avoid it when you don't. If you have a server-side event handler, your code will post back to the server (reloading the page) every time the user clicks the TextBox.
You can read this link (and others) about using __doPostBack() on the client side to trigger an UpdatePanel to perform a postback.
http://encosia.com/easily-refresh-an-updatepanel-using-javascript/
Practically I don't need it. But a little curiosity is running on my mind as my event handler function written for running in the server is not called. I have dragged a html button element and put it inside the form element with attribute runat="server"
Now if I double click on the button inside design view, it redirects me to a javascript block on which the event handler function is created. I erased it and write my own in the code behind file. Now when run the page on the server and click on the button, the event handler function is not called.
I want it to be run. Any way ?
You are confusing between two different events: the onclick provided in the editor is the client side click event, handled with javascript code.
The server side, c# based event handler can be added to such a button if you markup it with
runat="server"
,attribute and add a server side handler to it (assuming Button1 is the id of your button)
Button1.ServerClick += new EventHandler(Button1_ServerClick);
the markup for the html input doesnt expose the server side event to add a handler through the markup, but you can add the code i pasted above within your page_load/init events
What is your button like? try this:
<button runat="server" ID="myButton" onClick="YourFunction">My Button</button>
ASPX:
<button runat="server" id="htmlButton1" name="htmlButt" onserverclick="butt">
foo</button>
CS/Code-behind:
protected void butt(object sender, EventArgs e)
{
Response.Write("butt clicked!");
}
You'll have to write the handler (manually) - the IDE will not auto-magically wire it for you (you wire it yourself with onserverclick).