I have a repeater in UpdatePanel, and in it is a linkbutton that needs to set some values in a hidden div (display none) and then call javascript method that would make that div visible.
I am using ScriptManager.RegisterStartupScript and it is calling js method but the problem is that the code behind code is overwritten - code behind code is setting values for some fields from database (in the hidden div) and after it appears the fields are empty. If i click on a different button in repeater in updatepanel the div appears with the values set for a previous click. (the customer demand is that they click on a record in repeater and they can change it in a modal dialog).
how can i get the javascript method to make the modal dialog appear with the proper values?
current code is something like this in oncommand event for a repeater linkbutton
...
txtName.Text = row.Name;
ScriptManager.RegisterStartupScript(this, typeof(string), "showEdit", "showModalPanel('pnEdit')", true);
I recently had some issues with getting ScriptManager.RegisterStartupScript to work with partial postbacks using an UpdatePanel. Try switching your code to use something like this instead...
ScriptManager.RegisterClientScriptBlock(updPnl, updPnl.GetType(), updPnl.ClientID, "alert('hello world';", True)
You also may have to manually update your UpdatePanel on each click so that the hidden div gets the refreshed values. To do this, you'll have to set the UpdateMode on your UpdatePanel to Conditional and then be sure to go back and manually update it whenever you need to in your codebehind.
Related
I have an html page where clicking on a checkbox causes a new section of the page to become visible. The HTML looks like this:
<input id="formcheck" ng-model="collapseform" type="checkbox">Fill in the Test Form
<div id="mainformdiv" class="wizard-div" ng-show="collapseform">
<div>
<div class="main-block">
...and so on
</div>
</div>
</div>
This part works fine - by default that part of the page is hidden, and when I select the checkbox, the rest of the page appears. When I deselect it, the rest of the page is hidden again.
Further down on that same page there is a table, and when you select a node in that table it calls a controller function that causes some internal values to be set - this part works as well, so the controller seems to be set up correctly and all the functions can be called.
The problem I'm seeing is this: I want to add a bit to the controller function that gets called when the table node is selected, and what I want it to do is, in addition to setting the internal values (which it does correctly), also check the checkbox, and cause the hidden part of the page to be displayed in response. I thought if I checked the checkbox through the javascript that would accomplish this, so I added this to the internal value setting function:
if (document.getElementById("formcheck").checked == false) {
document.getElementById("formcheck").checked = true;
}
That kind of works - the checkbox does get checked off when that piece of code is called, however, unlike when I click the checkbox with a mouse, it does not seem to trigger the ng-show, and so the hidden part of the page is not made visible. Is there something else I need to do in order to make this work?
Thanks!
You should be able to change the value of the model. Since NgModel provides two-way binding, it watches the $scope variable as well.
For example do
$scope.collapseform = false
instead of
if (document.getElementById("formcheck").checked == false) {
document.getElementById("formcheck").checked = true;
}
From a list of names i dynamically create anchor tags on an aspx form page. The result of each is as follows with different names/IDs/titles.
From a list of names i dynamically create achor tags on an aspx form page.
<a target= href="mailto:johnbrown#site.net" onclick="updateSql(document.getElementById('PersonJB'))" runat="server" id="PersonJB" title="JB">John Brown</a>
Java function updateSql is
function update(passLink) {
/* alert("this dialogue has been invoked through codebehind."); */
document.getElementById('<%= btnUpdateSql.ClientID%>').click();
}
btnUpdateSql is defined hidden
<asp:Button ID="btnUpdateSqlt" runat="server" Text="Button" CssClass="hidden"/>
It all seems to work. except I want the click event to be aware of the different link that was clicked.
What I want to achieve: Update btnUpdateSql text with the ID or title of the passed link before called the click event in the javascript function.
How can I set the button text in the Java function? Or how can i use another hidden field?
The purpose being to update SQL table in the click event using the initials as a key.
First of all, multiple links triggering a button click event under the hood is very fragile.
Why not use LinkButton since you are posting back to server anyway.
The advanatage is LinkButton has CommandArgument and CommandName. All links can use same Common Event, and you can see which link trigger the event by looking at the CommandArgument.
If you do not plan to postback to server, you will need to redesign the application to use Ajax. It is out of the scope of original question.
On my page, I have:
txtCalendar, an asp:TextBox used to enter a date, with an attached JQuery datepicker
btnSave, an asp:Button to save the form
btnAdd, an asp:LinkButton that opens a new form for a different part of the page.
When either button is clicked, txtCalendar_TextChanged is called before the btnSave_Click or btnAdd_Click function is called. txtCalendar_TextChanged shouldn't be called at all when these buttons are clicked - before or after.
It is possible that the JQuery datepicker is somehow causing this - when I comment out the code instantiating datepicker and binding it to the textbox, txtCalendar_TextChanged is not called.
Additionally, I have verified:
Even when I prevent the $(function() {} script from running after a postback, the txtCalendar_TextChanged method is still called when a button is clicked
I do not have any JavaScript that looks for the ID #btnSave or #btnAdd or anything that could match/respond to those IDs
the sender for txtCalendar_TextChanged is my txtCalendar textbox
eventargs for txtCalendar_TextChanged is empty (not sure if this is significant)
the text in txtCalendar doesn't actually change (verified visually in the UI, not by adding code on the backend to check)
txtCalendar_TextChanged is not called when I remove OnTextChanged='txtCalendar_TextChanged' (so - it is being called by txtCalendar, somehow...)
txtCalendar_TextChanged is called even when I remove AutoPostBack='true'
txtCalendar_TextChanged is called even when I disable the btnSave_Click function by commenting out Handles btnSave.Click (both buttons use Handles btnX.Click in the aspx.vb rather than OnClick="btnX_Click" in the aspx)
Page_Load is hit before txtCalendar_TextChanged, but the first check is If(Page.IsPostBack) Then Exit Sub - the rest of Page_Load doesn't run (so - other backend functions are not being hit)
Solved:
In vb.net, I use myDate.ToShortDateString to set the initial value in txtCalendar. ToShortDateString formats dates without leading zeroes, while datepicker's default format does have leading zeroes
When datepicker is instantiated, it changes the date format in txtCalendar, but since this is done via code, it doesn't trigger a postback. When the user clicks a button and a postback is triggered, txtCalendar_TextChanged is also triggered because of the new date format (the text string has changed).
Changing datepicker's dateFormat to m/d/yy fixed the problem.
I'm changing the items in an ASP.NET DropDownList using jquery, but when I submit the form, the SelectedItem property of the DropDownList is null. Any idea how I can modify the list of items in the DropDownList with jquery without causing this error in the code behind?
Here's the way I'm setting up the DropDownList. Initially, it's an empty ASP.NET DropDownList.
var mailType = $("#ContentPlaceHolder1_ddlMailType");
mailType.empty();
mailType.append("<option></option>");
mailType.append("<option value='D'>Direct Mail</option>");
.
.
.
The DropDownList does end up showing the added items after the jquery executes.
Sorry, you cannot use jquery or Javascript to add items to a server-side ASP.NET web forms list control. The contents of the list control are stored in ViewState, and unless you've done something very strange, your jquery is not updating the ViewState. If you have validation enabled, ASP.NET will throw an error if the form post tag/value does not match any of the known values that are stored in ViewState.
If you wish to have listbox with items that are added dynamically on the client side, don't use a server side control. Just type the HTML into the ASP web form directly (don't use the runat=server tag). To read the contents of the control on postback, don't reference the control; instead, use HttpContext.Request.Form["tag name"]. Note that there is no way for your code behind to know what your jquery has added to the list box, as this information is not passed when the browser posts the form-- only the value attribute of the selected item is passed.
For some more related information, see one of my other answers here.
Couple of things you should check. 1) Modify your JavaScript to get the drop down client id dynamically. The client id changes with every post back event.
var mailType = $("<%= ddlMailType.ClientID %>");
mailType.empty();
mailType.append("<option></option>");
mailType.append("<option value='D'>Direct Mail</option>");
Also, 2) If you are binding the original values of the drop down list in code behind make sure you are handling the post back event. Otherwise the drop down will reset to its original value each time there is a post back.
If Not Page.IsPostBack Then
ddlMailType.databind()
End If
your control is declared with no items in the aspx. since nothing is added to the items collection that way and no adding to items is done in page_init or page_load (when IsPostback = false) when a postback occurs to check SelectedItem, the items property of your DropDownList is empty.
you can still get the value from the forms collection Request.Form[MyDDL.UniqueID]
more in depth here.
Why does DropDownList.SelectedValue is relied on viewstate?
I have a table that I'm creating rows/cells in the code behind, and adding various controls to each cell. There is also as submit button on the form.
One of the cells contains an SPLinkButton. I have a Javascript function that changes the innerText/textContent based on user action from the default value set in the code behind. Prior to clicking the Button to run code behind, the end user does see the updated text of the SPLinkButton.
When the submit button is clicked, this is the only control that reverts its value back to the default value during the button's postback. A Label/TextBox/DDL all preserve their values. If I wrap the control in IsPostBack, the cell the control is added to will be null (or if I wrap the entire table, there will be no rows). Note that I'm using the value of another item to uniquely append the SPLinkButton ID on a per-table row basis.
What is different about LinkButton/SPLinkButton and postback behavior?
EDIT: I converted the SPLinkButton to a TextBox, providing similar functionality. While I dislike the presentation compared to the textbox (look and feel, as well as the SPLinkButton would automatically expand the modal size when needed), it does function. I would love to get SPLinkButton working and go back to using it, if possible.