I'm dynamically generating radio buttons in my code behind and assigning javascript to them as they are created. This javascript will change the value in a hidden field for when a postback is eventually triggered (autopostback on the buttons is disabled). I am using the exact same method with ASP ImageButtons and it works fine but when I do it with the radio buttons the event never triggers the Javascript. I suspect that I may be using the wrong event name but I have tried several (onclick, onCheckedChanged, etc).
Here is a sample of the VB.NETcode - how come this works fine with my button but not my radiobutton!
//This is my hidden field
ClientScript.RegisterHiddenField(Me.UniqueID & "_someVariable", "")
Dim radDefault As RadioButton = New RadioButton()
radDefault.GroupName = "radio buttons"
radDefault.AutoPostBack = False
//This adds the Javascript to set the hidden field with an onClick event
radDefault.Attributes.Add("onClick", "document.forms[0]." & Me.UniqueID & "_someVariable.value='0';document.forms[0].submit();")
The code is simplified as the button generation is actually through an iterator but the same properties are given to each generated radio button.
the postback caused by pressing the button was actually clearing the value of the hidden field (different behaviour from assigning the javascript to a button)
Related
After breaking my head over something apparently simple, here I am:
I have an ASP.NET GridView wrapped inside an UpdatePanel. Once the user enters something in the last textbox of the last row and moves out, I add a new row to the gridview. This is done server-side by firing the OnTextChanged event of the textbox and setting its AutoPostBack property to true. This bit is Ajaxified by using the UpdatePanel.
My simple requirement is: I need to set the focus on the first textbox of the newly added row once the partial refresh is over.
What I tried:
//Get the newly added row (basically the last row)
GridViewRow newRow = myGridView.Rows[myGridView.Rows.Count - 1];
//Get the TextBox control on which I want to set focus
TextBox textBox = newRow.FindControl("txtMyCode") as TextBox;
//Set the focus
ScriptManager.GetCurrent(this.Page).SetFocus(textBox);
On stepping through the code, each of the above lines execute, and yet, when the partial postback completes, the textbox doesn't have the focus. The ScriptManager.GetCurrent(this.Page) returns an instance of the AjaxControlToolkitScriptManager, which is there on the Master page.
Any ideas?
You could write jquery to achieve this bit if you were not able to set focus from server site.
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(BindEvents);
function BindEvents() {
//write your logic here lets say
//if your grid view empty row is visible then set focus
}
</script>
You can use javascript pageLoad function which is Ajax-specific function fired every time the content of ajax update panel is refreshed. Inside that function trigger 'focus' on your text box.
function pageLoad() {
document.getElementById('ClientIdOfYourTextBox').focus(); }
You need to get ClientId of newly added text box in javascript. You can achieve it by saving it in hidden field server-side once it's added and then get hidden field's 'value' client-side or by building jquery expression (e.g. get last from all 'input' controls with id containing 'txtMyCode')
Hi I'm having a really strange issue here. I have an ImageButton and Gridview, both of which I create on runtime. The Image button is a seperate control on the webform and is not linked in anyway to the Gridview. What I am trying to achieve is when the user completes the editing of a line in the GridView and clicks on the update row button, I perform a set of calculations on the input entered. If an error is found with the data entered, I then make visible the ImageButton which when clicked displays the error message. The function I am using to make visible or hide the Image button through
picCross.Style.Value = "display: none;"
and
picCross.Style.Value = "display: block;"
gets called on the Page.PreRender function. What I have discovered is that I am able to make changes to the ImageButton (changing the button's tooltip, setting its Style's value ) during a postback not fired by the GridView, such as when the user clicks on a button else where on the web form. If however I try making changes to the Imagebutton during the post back event of the GridView, such as when the user clicks on the edit row or update row button/link. The changes are not saved. What is even more strange is I do not have such an issue with labels. I can change the text of labels regardless on whether the change was made during the postback fired off by the Gridview control.
I have tried setting the ImageButton's EnableViewState to true and false but neither makes a difference. I have tried the same approach with panels and the same thing occurs, I can't change its properties during a postback caused by the gridview. My GridView has EnableViewState = True
P.S I'm binding the GridView on runtime too. Only my GridView's RowUpdating event fires. The RowUpdated event does not fire. I was hoping to try changing the ImageButton on RowUpdated, not that I think it would have made much of a difference anyway.
You should create the dynamic controls (Gridview and ImageButtons) in the PreInit event. Init event if you have a Master Page.
I'm rendering a DropDownList in my Visual Studio 2005 ASP.NET code behind page out to an HtmlTextWriter, but when I try to add the SelectedIndexChanged event that doesn't get rendered.
Any ideas? Is this even possible?
Update: I've tried setting AutoPostBack=true. Is it possible trying to render the control via the HTMLTextWriter isn't supported?
Adding an event handler to the SelectedIndexChanged event (or any other server side event) will not affect the markup produced when rendering the DropDownList control. The event handler is defined and executed only on the server, and needs nothing extra in the client side markup.
The SelectedIndexChanged event will be triggered on postback, if the selected value in the list has changed between since the last rendering. The view state is used to track the previously selected value, and the posted form value from the <select> holds the new value to be compared.
If you want your page to perform an automatic postback when the selected index of the DropDownList changes (on the client side), set the AutoPostback = true on the control. This will cause a minor change to the rendered markup, which will now include a client side (JavaScript) event registration on the <select>, triggering a submit of the surrounding form when its selected index is changed.
I have a modal popup that has a targetId to a hidden button. I want the popup to occur when a button in a grid is clicked but that button is programmed behind the code and therefore the targetId would be invalid...
So I wanted to attempt to set the gridview's button's onclientclick event to be the onclientclickevent of that hidden button. Is this possible or should I be going about this another way.
here is how i created the grid button
If Not IsPostBack Then
Dim field As New TemplateField
field.HeaderText = "Sub Departments"
Dim col As DataControlField = field
GridView1.Columns.Add(col)
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim btnview As New ImageButton
btnview.ImageUrl = "\images\icons\xp_ico_search_24x24.gif"
GridView1.Rows(i).Cells(3).Controls.Add(btnview)
Next
End If
I am assuming you are using web forms. If so then yes, it it very possible.
Do the following.
Create a javascript function on the page
function openModal(btnId){
btn = document.getElementById(btnId);
btn.click(); // this should fire the click even of the button
}
on the grid button add the onclientclick event:
gridButton.OnClientClick = String.Format("openModal('{0}');", modalButton.ClientId))
This will set the client Id of the button that trigers the modal window into the javascript function. If you need to populate the modal window with other data, you should do it in this function as well.
Are you using the ASP.Net AJAX Control Toolkit? Or something else? This assumes the toolkit.
Also, you have set the visibility of the button to hidden, but do not the the Visible=False property on the server side, as this will not render the button. To hide it you will need to use the client side property style="display:none"
This link may help: http://forums.asp.net/t/1066506.aspx
It is possible doing that, it requires you to fire off the buttons "DoPostback" function.
Try looking into the Page.ClientScript.GetPostBackClientHyperlink method.
Alternatively you can use javascript and the .click() event, but I think that limits you to IE as a browser, not sure on that though.
It would look like this
javascript:document.getElementById('clientSideID').click();
I have an gridview that I am adding onclick events to a checkbox column via:
cb.InputAttributes.Add("onclick", "checkClick()");
everything works fine, but when the user clicks the save button on the form, (which is within the updatepanel), suddenly the onclick event of the checkboxes stops firing!
Is this a problem with the ASP.NET AJAX?
The weird thing is that I am seeing the onclick event on the source, it just doesn't fire.
Help!
The source will show you the state of the document when first received from the server, not the current state of the DOM. What is likely happening is the update panel content is being replaced by new HTML content. The elements to which the original click events were bound are no longer in the dom.
The onclick events will need to re-bound to wire-up to the new elements that have arrived.