I have a textbox and a gridview right below that on an aspx page. When ever I get to the page and press enter, it fires gridview onrowcommand event. Can anyone tell me what is going on here? I did google everyone suggested me to add a piece of javascript code to handle enter key event (13) . I want to know why would a page do that. Is it a web form thing or something else?
What you're witnessing is the default behavior much like hitting enter on an HTML input tag nested within form tag.
If you don't want the behavior, then you can handle it either in JavaScript as your research suggested it or in your C# code as follows:
protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("onkeypress", "javascript:if (event.keyCode == 13) {
// do whatever you want with the event
}
}
Related
I have a problem with listbox. I would like to fill it with data and select Value (listbox.SelctedValue), using postback, after textbox is filled and Page.Validate() is fired. I try to use it in Page_Load. Everything works fine until I dont mark another user. It goes back to the the first one. I know its because I mark the first one again and again it in Page_Load, but how can I mark user after postback in other place? I cant use any buttons.
To be more clear, I have one text box, which causes postback after user put text there. After that I would like to check if Page.Isvalid and is yes, add that user to listbox (which also causes postbacks) and mark him. Without any buttons. How can I do it only once, using autopostback, not every PageLoad ?
Try this
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (Page.IsValid)
{
//Mylistbox.SelctedValue = set Your Selected Value
}
}
}
It seems like a straight forward thing, but I can't figure it out.
How can I check if a certain button was clicked from user control?
my user control is uc_test and button name is btnTest .
I assume its some sort of event handler added to uc_test.btnTest ?
I'm working in WinForms.
It sounds like you've created a button but want to make it do something on click. I don't often use Winforms but from memory double-clicking on the control in the form should automatically create a btnTest_Click method. If not, just go to code and put in a method like:
protected void btnTest_Click(object sender, Eventargs e)
{
//do something
}
Then set in the properties of the button the OnClick event to btnTest_Click.
I'm trying to add an event handler to a TextBox control that is called when the text box loses focus. Is this possible?
I know WinForms has a LostFocus event. I'm looking for something similar in ASP.NET WebForms.
You can Use OnBlur
<asp:TextBox id="TextBox1" runat="server" onblur="Javascript:alert('1234');" />
Or
EDIT:
Server side
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Attributes.Add("onblur", "alert('hi User')");
}
}
I think you are going to have to use javascript for this and the event you will need to search and look up would be onBlur() I believe. If you need an example, I might be able to scrounge one up for you. But here are some links..
W3Schools
Stack
Java Page
Hope these help.
This would be the blur event, and it's a client-side event on the resulting input element instead of a server-side event on the TextBox control.
For example, let's say you have a TextBox with the ID of "TextBox1", and let's assume jQuery is included in your project template (because it probably is), then in your client-side code you might have something like this:
$('#<%= TextBox1.ClientID %>').blur(function () {
// code to respond to the event
});
This would execute any code in that function whenever the input for that TextBox control loses focus. Notice the use of a small block of server-side code to output the server-side control's client-side ID.
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/
I would like to validate my textbox when the textbox is deselected.
It should validate the textbox value in the same way as Google's Create Account page.
If the textbox value passes validation it should proceed further otherwise it should show message like: This value is not valid.
Events of Validation And LostFocus can be you here.
Here is some code for validate textbox value
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus.aspx
if its a desktop app/WinForms u can use what Suspitsyn Kirill said, and for a web app it would be quite close.
The textbox has event handlers for on focus etc. using the code behind you can assign what the events will do.
Dont remember the C# ones exactly but it should be
private void textBox1_LostFocus(object sender, System.EventArgs e)
{
textBox1_Validating()
}
add the lostfocus on the element to direct to this method.
You could also do it with javascript same concept.