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();
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')
I have a textbox and a button with an onClick event connected to it, and when the button is clicked once I want to change the text of the button and clear the textbox. How can i accomplish this?
If you want to do this on a postback (a complete refresh of the page), simply put code in your button click handler to set these values. For example, textbox.Text = string.Empty;
If you want to set it on the client side (without refreshing the page), then you'd need to use client script such as JavaScript.
This needs you to know about TextBox and its properties. Normally the value of a textbox you use its Text property as TextBox1.Text. In your case you put this statement in the button's click event
string txtValue=TextBox1.Text;
TextBox1.Text=string.Empty;
To see how to use TextBox and its properties. This can help
Good day,
I have a problem in a .NET page where I am using an asp:textbox in combination with an OnClick action on a link button.
What happens is that after text has been entered into the textbox, if you directly click on the link button, more often than not the textbox is considered to be null.
If you click off the text box first then click the link, all is well and the save function performed by the link button proceeds as expected.
My assumption is that there is a lifecycle event that is being missed, or not applied which is not binding the text to the textbox for use in the codebehind when the link button is clicked.
The question is, what can i do to enforce that binding short of doing something like adding an onkeypress event to the textbox to force a postback.
There must be a more elegant solution.
Thank you in advance for your help.
Do you have initializations on your textbox inside Page_Load event? If so, use IsPostBack=false and put the initialization inside.
If IsPostBack =False Then
TextBox1.text=""
End If
I have a jQuery Pager control, for each page there is a set of textboxes and a submit button. When this submit button is clicked I want it to fire off some jQuery to update the controls on the front end (i.e. current page, set visibility of controls etc). The button is an asp.net web forms postback button.
Does anyone know any way of doing this?
Merry Christmas!
Inject js from code-behind after your postback success like:
string script = "$(function(){setPage(\"" + yourpagenumber+ "\");});";
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "Pager", script, true);
And in the js you will have a setPage function that does the job of setting page with your jquery pager plugin.
function setPage(pagenumber){
alert(pagenumber);
//do your page setting here
}
Note: you can use Page.ClientScript.RegisterStartupScript if that fits your needs but the idea remains same.
Don't use a submit button, use an input of type button with an onclick event instead.
Use the OnClientClick attribute of the asp:button i.e.
OnClientClick="JQueryFunction();return false;"
for no postback and
OnClientClick="JQueryFunction();return true;"
for a postback. I'm assuming that that JQueryFunction() returns true.
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)