Getting source control id in JavaScript function - asp.net

I have taken three textboxes over aspx page. A JavaScript function is associated with these textboxes as:
TextBox1.Attributes.Add("onkeypress","MyFunction()");
TextBox2.Attributes.Add("onkeypress","MyFunction()");
TextBox3.Attributes.Add("onkeypress","MyFunction()");
The MyFunction() is defined within a JavaScript file.
What I want is to get the control id of the textbox on which the function is associated within the JavaScript function itself. That is, the JavaScript function should look like this:
function MyFunction() {
var myControl = ?;
alert("Key press event occured in "+myControl.ID);
}
I need the value of ?. What should I write there so that I could get the control id, i.e. TextBox1, TextBox2, or TextBox3 on which the event occurred?
I know that <% =TextBox1.ClientID %> will provide the id of TextBox1 on client. But I think as I am attaching the onkeypress event with the textbox I should get the source control id within JavaScript function itself.

Try:
TextBox1.Attributes.Add("onkeypress","MyFunction(this)");
JS will put the reference to the widget into this when the code for onkeypress is evaluated.

Related

Access a control on the particular Content Page from a MasterPage using JavaScript

In asp.net I took one master page and one Web Form selected with the master page.
In first web form i have one textbox and button.
When button click then OnClientClick property contains validate() function
Now In master page's coding I written as following :
function validate() {
var no = document.getElementById('<%=Page.Master.FindControl("ContentPlaceHolder1").FindControl("TextBox1").ClientID %>').value;
if (isNaN(no)) {
alert('not a number.');
}
}
Now i took second web form
In that i put one calendar control and just run it
error occurs as following :
Object reference not set to an instance of an object.
Line 11: function validate() {
Line 12: var no = document.getElementById('<%=Page.Master.FindControl("ContentPlaceHolder1").FindControl("TextBox1").ClientID %>').value;
Line 13: if (isNaN(no)) {
Line 14: alert('not a number.');
I am seeing Line 12 : as a red line.
So How to run this second Web Form
Inside content page declare an in-line Javascript variable.
<script>
var jsTextBox1Id = <%=TextBox1.ClientID%>;
</script>
Now you can access the control ID in Validate() using global jsTextBox1Id variable.
Like this:
var no = document.getElementById(jsTextBox1Id).value;
(Remember this type of approach is not a recommended practice.)
Update
To get a reference to the HTML element that is rendered for any control in client script, you must know the value of the control's ClientID property. However, because the user control can be put anywhere in a Web page, it is impossible to know in advance which naming containers will contain the controls. To make sure that the ClientID value will be the same as the ID value, the just set the ClientIDMode value to Static.
<asp:TextBox ID="MyStaticId" runat="server" ClientIDMode="Static">
</asp:TextBox>
Now you can access it using the ID defined by you MyStaticId, it will not have prepended parent container name to it.
document.getElementById("MyStaticId").value
Source MSDN

how to get javascript calender value on server side

I am new on Asp.net. I am using Javascript calender It use Input tag.if I use runat="server" so that I can get value on cs page then it does not display calender. How can I get calender value from input tag to .cs page
Simplest way:
Have a hidden field on your control.
Set its in javascript by using :
$('#<%=hdDate.ClientID %>').val("date value here");
and then you can access this hidden field in cs file.
create a hidden field as in your aspx page which should be runat="server".
then
On button click call javascript function say jsOnclikfun() and write this function as
<script type="text/javascript">
function jsOnclikfun() {
var dateInputtext = document.getElementById("Id Of input textbox which you are trying to make runat=server");
var hdDateValue = document.getElementById('<%= hiddenField_Id.ClientID %>');
hdDateValue.value = dateInputtext.value;
return true;
}
</script>
Now on server side where ever you need data get it from that hidden field as hiddenfieldID.Value.
Hope this helps.

Refresh updatepanel through triggers when other updatepanel does because the first must have UpdateMode=Conditional

I have some updatepanels in asp.net page. I want updatepanel 2 get refreshed when updatepanel 1 does, but UpdatePanel 2 has his UpdateMode attributed set to Conditional and ChildrenAsTrigger=False, because it has another updatePanels inside and i need to control the way it is refreshed. I was trying to make a trigger for the other updatepanel but this seems not work, maybe i am missing something
Is there a way to make this happen?
If you can't figure a valid example, imagine product category list and product list, when selected category change (linkbutton inside updatepanel) product list is refreshed which is inside another updatepanel, product list change for example when you order by price (order is inside other updatepanel) and i don't want category list gets refreshed.
thanks in advance.
You can call updatePanelProdList.Update() inside the code-behind file of your page. The place to call this method in your scenario would be the event handler for the category change LinkButton.
Thank you, that's what i am looking for! I have 2 "Update Panel" on my page, one keeping my form and other one keeping flexiGrid (non .net version,pure jQuery).
I do manual AsyncPostBack from my grid to get in edit or delete mode.
function doPostBackAsync(eventName, eventArgs) {
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (!Array.contains(prm._asyncPostBackControlIDs, eventName)) {
prm._asyncPostBackControlIDs.push(eventName);
}
if (!Array.contains(prm._asyncPostBackControlClientIDs, eventName)) {
prm._asyncPostBackControlClientIDs.push(eventName);
}
__doPostBack(eventName, eventArgs);
}
<a title="Edit" href="javascript:doPostBackAsync('Edit','2');">Edit</a>
and then i handle it like
string EventName = Request.Form["__EVENTTARGET"].ToString();
int EventValue = Request.Form["__EVENTARGUMENT"].ToString();
after determinate EventName and EventValue calling the method below
protected void Edit(int id)
{
//load the form here
UpdatePanel1.Update(); //do the trick right, without this line unable to show form fields and other stuff with newly loaded data
}
regards.
(sorry for my English)

Trigger function inside a usercontrol from a page

I have created a usercontrol to capture education details, it contains 5 textboxes and an functionto insert that values into my db. I have added the usercontrol 5 times to a page. I have a button on my aspx page which I want to be able to click and call the function to insert the values.
Can anyone suggest how I can do this?
The function which inserts values into your db should not be in your usercontrols but in your page. Does your usercontrol has a save button itself or only the page? On first variant you should raise an event and the page could catch it to save the values. On second variant you should iterate through your usercontrols(added dynamically or fixed?). You should add public properties which return the textbox's values to the page, then you can save these values into the db.
Put a public method on your user control.
// Inside MyUserControl
public void SaveIt() {
// logic to perist the values to db.
}
From your ASPX page (or code behind) when the button is clicked, loop over your user control instances and call the method:
// On Click...
MyUserControl[] arrControls = new MyUserControl[] {myUC1, myUC2, myUC3, myUC4, myUC5};
foreach (MyUserControl c in arrControls)
c.SaveIt();

Passing Listbox Text to Javascript Pop-up Window URL

Is it possible to pass the contents of a textbox or Listbox into the URL portion of the javascript code window.open(URL)? I have an asp.net listbox control that displays URL values. When ever an end user clicks on another listbox, the URL listbox provides the specific URL. I am trying to pass this URL into the above javascript code, but I do not know the correct syntax to do this. This code will execute as an onclick event.
For clarification, similar to typing “+ ListBox.Text.ToString() +” or ‘” & List.Text & “’” to add the content of a listbox into something else, such as a textbox. Is there specific syntax to do the same, but add the listbox.text into javascript?
Thank you,
DFM
Simply add a client-side onclick handler to your listbox as shown below:
<asp:ListBox id="ListBox1" runat="server" .....
onclick="openPopup(this)">
........
</asp:ListBox>
Then add the following javascript code:
<script type="text/javascript">
function openPopup(e){
window.open(e.value);
}
</script>
Sure, this should be pretty easy with jQuery. Obviously the URL generation could be reduced to a single statement, but should give you the general idea.
$(document).ready(function() {
$("your-element").click(function() {
var str = $("#listbox-id").val();
var url = "your-url.com/" + str;
window.open(url);
});
});

Resources