Set default button inside div - but I don't know ID / class - c#

I have a page where user controls are loaded dynamically into a div/panel. I don't know anything about the content of these user controls, besides they have a button.
When the user is inside this div / panel (using the user control), I want the button inside the div/panel, to be the default button.
Right now, my sidewide search button is the default button no matter what.
I can't add "defaultButton=mybutton" in the user controls, and I can't add a class to the buttons.
How do I solve the problem?
Thanks :)

I ended up doing the following:
$("#CalculatorDiv").keypress(function (e) {
if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
var div = $("#CalculatorDiv");
var firstInput = div.find("input[type=submit]");
firstInput.click();
return false;
} else {
return true;
}
});
:-) So a combination of find and keypress event.

You have to be more specific. Better yet, post the code.
By default button, do you mean pressing "Return" will make the button submit? In that case, you can enclose it in a form.
To access the button, you can do that via javascript's document.getElementById
https://developer.mozilla.org/en-US/docs/DOM/document.getElementById
You can also use jQuery to access the element by class, type or nearest button element to the div tag. Maybe use the "find" method in jQuery:
http://api.jquery.com/find/

I'm not certain how you are accomplishing this based upon your description but what you could do is set the Control.ID and then you can use FindControl to locate the UserControl.
UserControl uc = (UserControl)PANELID.FindControl(Control.ID);

I'm not sure, but can't you search for <input type="button" .../> and than add the default code part to it?
EDIT: read your question wrong, asumed it was a webpage 'cuz of the 'div'.
Normally in C#, you can select the Controls inside a panel and then search on the type to find the button

$("your div").children().blur(function(){
$(#button).focus();
});

Related

Only enable RequiredFieldValidator when parent element is visible?

I have an asp.net webform with a few Panels that each have several textboxes inside of it. I am currently hiding or displaying the panel using jQuery based on which item in a DropDownList is selected.
I have run into an issue where the required field validator is still firing even when the elements it is attached to is not showing because it's parent panel has display: none.
Is there any way to disable the RequiredFieldValidator when the element it is attached to is not showing because of CSS?
I know that if set Visible=false on the server side the elements wouldn't render at all, but I would prefer to keep the show/hide logic on the client side for user experience reasons.
I agree that a custom validator would be best, but if you do need to do it on the client side, you can use the ValidatorEnable function.
ValidatorEnable(document.getElementById("<%= RequiredFieldValidator.ClientID %>"), false);
(Note that I have never actually tried this myself, but I have heard of it successfully being used to disable validators on the client side.)
As this thread says, you can use that function. So modify your method like below
function cbSearchOption_SelectedIndexChanged(sender, args) {
var x = document.getElementById('cbSearchOption').value
if (x == 'Date')
{
ValidatorEnable($get(‘<%=DateValidator1.ClientID %>’), true);
document.getElementById('test').style.visibility = 'visible';
}
else
{
ValidatorEnable($get(‘<%=DateValidator1.ClientID %>’), false);
document.getElementById('test').style.visibility = 'hidden';
}
}

Page doesn't postback after clicking OK in confirm box for Radiobuttonlist

I have a RadioButtonList with 2 items. I want a postback when any of the items is selected. I have added a confirm box for one item of RadioButtonList. But, when I click OK in the confirmbox, there is no postback and SelectedIndexChanged event is not getting fired.
AutoPostback property of RadioButtonList is set to true.
This is a code fragment from my Page_Load method:
RadioButtonOpenRestricted.Attributes.Add("AutoPostBack", "True");
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("AutoPostBack", "True");
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "javascript:return confirm('Are you sure?');");
Earlier, I had added confirm box for entire RadioButtonList and postback was working as expected. But I want the confirm box to be displayed only when user clicks on "Open Access" item.
Please help!
I tried a few things. The new code lines look like:
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "javascript:showConfirmBox(0,'" + RadioButtonOpenRestricted.ClientID + "')");
RadioButtonOpenRestricted.Items.FindByValue("Restricted Access").Attributes.Add("OnClick", "javascript:showConfirmBox(1,'" + RadioButtonOpenRestricted.ClientID + "')");
The javascript method is:
function showConfirmBox(i,id)
{
if(i==0)
{
if (confirm("Are you sure you want to provide Open Access? All existing individual permissions will be removed!")==true)
{
var ctrl1 = document.getElementById(id + "_0");
ctrl1.selected=true;
}
else
{
var ctrl2 = document.getElementById(id + "_1");
ctrl2.selected=true;
}
}
if(i==1)
{
var ctrl2 = document.getElementById(id + "_1");
ctrl2.selected=true;
}
}
The problem with this code is that it is treating both OK and Cancel as same. The confirm box is getting displayed but if-else part of the javascript method is not getting called. I tried using OnClientClick also...this doesnt even display the Confirmbox.
Help!!!
This is because your on click script does not play well with auto-post back script generated by the ASP.NET. A quick hack solution can be
RadioButtonOpenRestricted.AutoPostBack = true;
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "if (!confirm('Are you sure?')) return false;");
Although, this will still give you an issue when you cancel on your confirmation box (in such case, you have to add script to select the previous radio button again).
As such I am not a great fan of radio button list - you may consider alternate mechanism such as repeater with radio button in item template and having your own java-script for confirmation.
I think you want to use OnClientClick to show a "confirm" window.
I don't think you can have a javascript confirm window perform a postback, at least not the way your code is set up.
You should set the OnClientClick to show a confirm modal or window with an <asp:Button/> and have that button perform the postback your looking for.
Khushboo, it used to happen with me many times. The reason behind that was I was missing some closing tag somewhere in my aspx page. I used to copy my whole aspx page to some other text editor and paste all the elements one by one to my aspx page. It always solved my this problem. I am sure you must be missing some closing tag, please cross check all elements.

Manually selecting web page radio button

i want to programatically select a radio button in code. The radio button lives on an internal webpage.
I am trying so far like this,
HtmlElement rButton = b.Document.GetElementById("nameOfButton)
which i assume gets me a handle on the radio button, but i cant find a .selected = true or something similar. I need to do this because on the page, when this button is selected more values appear on the page which i then need to fill out.
edit: i think i have managed to do this using
rButton.invokemMemeber("click") - however i think i am still going down a cul de sac with this one.
i would like to add, i think that the page is using postback. if i click the radio button, a set of new options on the page appears, its one of these dynamically created text boxes i need a handle on. is this going to be impossible to get?
You could do,
var rButton = b.Document.GetElementById("nameOfButton");
if(rButton != null)
rButton.checked=true;
Hope this help.
This is what worked for me:
if (doc.GetElementById("nameOfButton") != null)
doc.GetElementById("nameOfButton").SetAttribute("checked", "true");
Good luck!

default LinkButton on enter in a Master Page

I looked through and through and found no clean solution for this. A colleague of mine did find a pretty dirty solution but I don't see why the solutions in the links reference below does not work when I place it in a master page. I have a master page that basically has a textbox and a linkbutton, when I hit the enter key, the default button for the child page gets called instead.
http://weblogs.asp.net/jeff/archive/2005/07/26/420618.aspx
set linkbutton as default button for asp:panel in asp.net (Ahmad's Solution)
any insight on this please? thanks :)
I will post the dirty solution when I get to work tomorrow.
EDIT: here's the dirty (i think) solution:
string id = ctlToClick.UniqueID;
string someJavascript = //see below
EmailTextBox.Attributes.Add("OnKeyPress", someJavascript);
The javascript (placed it here so StackOverflow can format it better):
javascript:if (window.event){
if(parseInt(window.event.keyCode) == 13){
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('" + id + "', '', true, 'Login', '', false, true));
return false;
}
}
Is this really the only solution for this?
Did you try setting the defaultButton property?
<form id="Form1"
defaultbutton="lnkNextPage"
runat="server">
Where lnkNextPage is the LinkButton control's id.
I think you need a simple javascript to control your tab/enter keys. I have run into this before an there was no way to make the default button solution work.
The idea is simple - check each key that is entered while in the text box. If the key presed is enter or tab, find the button either by jquery or document.GetElementById('id') and invoke the click event on the button.
the form's default button doesn't work in asp.net since a): there is only 1 form, and b): for different text boxes you want to be able to control which button is actually clicked (on tab/enter).
A simple javascript file solves this.
<bleepzter/>

Call a specific button onClick event when the enter key is pressed C#

I'm trying to get a specific asp:button onclick event to fire when I press the enter key in a specific asp:textbox control.
The other factor to be taken into account is that the button is within a asp:Login control template.
I've no idea how to do this, suggestions on a postcard please.
You could look at the DefaultButton property of the panel control.
You could set the DefaultButton property on the form. Either as an attribute of the form tag in your markup DefaultButton = "btnSubmit" or using something like this in your code-behind:
Page.Form.DefaultButton = "btnSubmit"
Its HtmlForm.DefaultButton
You need to do it with javascript. It's really easy with jQuery.
You can do something like (off the top of my head, not tested):
$('#myTextBox').keypress(function(e){
if(e.which == 13)
$('#myBtn').click();
});
Edit: Be aware that although jQuery works exceptionally cross browser, there are some quirks with keypress described here.
Whoops i didnt see you said the "enter key" i thought you said "any key", yeah in that case use DefaultButton on asp:panel

Categories

Resources