checkbox not firing event after checked is set programmatically - asp.net

I am iterating through a string of characters and finding a checkbox. I find the checkbox without any problems and if the character of the iteration is equal to 't', I set the Checked property of the checkbox to true.
However, for whatever reason, although it does check the appropriate checkboxes, executing this code also means that when I click any checkbox myself, nothing happens (everything works just fine and I can check anything until I execute this code). Also, the submit button on my page no longer does anything if I execute this code. I'm puzzled as to what may be the reason for this.
The buttons are created programmatically on init and are located inside an update panel.
DateTime date = new DateTime(2015, 11, 18);
foreach (char c in attendee.accommodation)
{
ID = "accommodationCheckBox" + date.ToShortDateString();
CheckBox checkbox = (CheckBox)FindControl(ID);
if (checkbox != null && c == 't')
{
checkbox.Checked = true;
}
date = date.AddDays(1);
}
Here is an example of how the checkbox looks like in the end: https://jsfiddle.net/Lzxo047o/
EDIT: Scenario: You fill in a dynamically generated form on A.aspx, then you see a "registration overview" on B.aspx. If you want to modify your registration at that point, you hit a button which creates a new instance of A.aspx and fills it with your registration data from database, which is what I'm solving right now. I dynamically create some checkboxes and based on a value of string, either check them or not. They get checked as they should, but I'm unable to manually control them / click them.

checkbox.CheckedChanged += yourcheck_event;
If the checkbox is created dynamically, you have to add the event to it.

Make sure your UpdatePanel has it's UpdateMode property set to "Always".
Take on count that:
the UpdatePanel control’s content is only updated when one of the
following is true:
When the postback is caused by a trigger for that UpdatePanel control.
When you explicitly call the UpdatePanel control's Update method. When
the UpdatePanel control is nested inside another UpdatePanel control
and the parent panel is updated.
…

Related

How to Copy Content and Add New Record in Telerik Radgrid

Okay, basically I have a master page that only contains a RadGrid. I am using a custom popup WebUserControl form to handle the input for the update/insert controls. When I enter the edit mode I have a button called "Copy & Add New Record". When the user clicks on this button I want to copy 2/3 of the page content, open a new record, and then paste that information in the appropriate textboxes.
I have no problem copying or pasting the information. The problem lies with closing my current edit form and then opening a new record form. I tried closing the form using:
Dim temp As RadGrid = Parent.Page.FindControl("rgRT")
temp.MasterTableView.ClearEditItems()
temp.MasterTableView.IsItemInserted = True
And then setting the above statement to true to try to open the new record form. However it did not work. The popup edit form was still in the same position, I received no errors, and the only event to occur was an autopostback. I feel like this is something extremely easy but I cannot for the life of me figure it out.
Try adding a Rebind() call after clearing the insert items. This should put your grid in the defauilt (view mode) and remove the IsItemInserted = True call.
Try registering a script to init insert on the client so it goes into edit mode: http://www.telerik.com/forums/how-to-add-new-row-in-radgrid-clientside-which-should-be-inline-and-stay-in-editmode#hJ-0yiaxakaxzdOf-UXDYw (tip: you would want to use the Sys.Application.Load event for your code to execute, earlier calls may cause errors, something like below).
string script = "function f(){$find(\"" + RadGrid1.ClientID + "\").get_masterTableView().fireCommand("InitInsert", ""); Sys.Application.remove_load(f);}Sys.Application.add_load(f);";
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", script, true);
then, use the ItemCommand event to set default values: Telerik RadGrid - How do I set default data on insert?

check any data in the webform changed or not asp.net

I have a webform with a lot of controls in it mainly Textbox and dropdowns. When the user clicks on the Update button I am supposed to update the values in Database. But I don't want to run the Update code on every update button click. I just want to do it if the user have made any change in the data inside any of my control. Is there any way to understand theuser have have changed the data or not in the server side?
Create a hidden field with value=0 and set a class name for all controls then using .Change function in JQuery and every time a change value occur in any control, set the value of hidden control to 1.
Then in codebehind check the hidden field value on every submit and if the value is 1, run your update code.
$(".ClassNameForAllInputs").change(function() {
$("#<% = HiddenFieldID.ClientID %>").val(1);
});

Focus on textbox inside gridview and updatepanel

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')

Programmatically created RadioButtonList needs 2 clicks

I have a page (using MS Ajax). depending on other options, I may need to create a radio Button List inside a Panel.
This is done as per below.
RadioButtonList rbl = new RadioButtonList();
rbl.SelectedIndexChanged += new EventHandler(answer_Click);
rbl.AutoPostBack = true;
foreach (KeyValuePair<string, int> d in _answers)
{
ListItem li = new ListItem(d.Key.ToString(), d.Key.ToString());
li.Attributes.Add("class", "radio");
rbl.Items.Add(li);
}
p.Controls.Add(rbl);
this works fine, unless after the postback I need another RadioButton List. The list is drawn correctly, with all the correct options, but now when I click on an option the first time, it fills in and then resets. It takes a second click to get it to set and trigger the SelectedIndexChanged Event.
Im destroying and recreating the rbl once answer_Click is triggered (I know this as the next question that is created has different answers and options).
So, any ideas as to why it is I need to click twice on the second List?
Matt!
Looks like you have a ViewState issue. Controls are created after the LoadViewState trigger, so they doesn't get their values from the browser to load. You must save this value somewhere, using JS, ViewState, QueryString, Session, DB to persiste the state, and then load the selected value into each one.
Hopes this help you! Take a look here for more info about ASP.NET Page Life Cycle.

ASP RadioButton Client Event Not Triggering

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)

Resources