How to prevent the page loads by linkbutton? - asp.net

I m making a quiz game like KBC in ASP.NET (C#). I have used textbox as question. And four radiobuttons as four options. Radiobuttons are placed in a groupbox. I have given 3 linkbuttons for ‘Back’, ‘Next’, ‘Submit’.
The questions are generated randomly via the database. Code Sample:
page_load(…) {
// database connectivity
RandomFunction();
}
RandomFunction() {
//random numbers generated
Question();
}
Question() {
//displays question on textbox & options on radiobuttons
}
Now my question is how can the ‘Next’ or ‘Back’ linbuttons can show the next or exactly the previous question respectively? Since as soon as they get clicked then the whole page is loaded which i don’t want to happen. (i have already used ‘isPostback’ in ‘RandomFunction()’ so as to prevent the change in indexing of the questions in each postbacks. )

Related

Google Appmaker: Update form based on selected dropdown option

I have an AppMaker app that has a from based off of one address table/datasource. I can get a form with next/prev buttons, but replaced the key field (name) with a dropdown list of all names (a user can start typing names to jump there, with the dropdown showing).
My hope is that when a user selects from the dropdown, the entire form updates and the next/prev buttons work properly as well (there too many records to page thru with next/prev only). I don't have to have next/prev functionality if it complicates things too much.
Currently the dropdown is working, but I cannot get the index for the next/prev buttons set or the rest of the form to reflect the selected dropdown record.
I've tried to set the "onValueEdit" event to something like this...
var selected = widget.value;
var idx = widget.options.indexOf(selected);
console.log("Selected: "+selected+", index = "+idx+"\n");
if(idx < 0) { //...this error is never hit
console.log("Index error - setting to zero!\n");
idx = 0;
}
widget.datasource.loadPage(idx); //...update form?
Two observations via console logging:
The "idx" var is never set to the selected dropdown index reliably, and is
often "0" (tho no error msg ever shows), so the "indexOf()" function
isn't working as expected.
The "selected" var (name) is always correct.
If I call widget.datasource.loadPage(...) with a fixed value (say 5) it has no effect on what is shows in the form either (previous loaded data remains) - obviously not the way to do it :v/
Can you steer a noob in the right direction?
If you are using default App Maker form, then you can see that so-called pager, doesn't actually paginate. It triggers prevItem/nextItem datasource methods, in other words it navigates through datasource items, not pages. Here is a quote from App Maker docs:
nextItem: Selects the next item. For Query Datasources, if the current item is the last item on the page, then this loads the next page and selects the first item on the newly loaded page.
So, if you already have all your items loaded(you set query page size for your datasource to 0), then you need just to change selected item within datasource:
// onValueEdit dropdown event
// assuming, that form and dropdown share same datasource
widget.datasource.selectKey(newValue._key);
If you really have lots of items and it is not feasible to load all of them in one call... then it will be all another story...
UPDATE:
It's important that Options and Value are set as shown in the image below!
However, I had trouble setting them that way (read: wasted hours!) until I wiped them both completely using More options in the binding picklist, and tried again (I had even tried on a brand new app!). I was being forced to choose ..projections.. and then a final field before the OK button would be available.
Not sure if AppMaker is buggy here or there is something simple I'm not understanding!
None of the coding in my original question is required.
Once set this way, binding just works as you would expect it!!
All other fields are set as #datasource.item. and are bound to whatever item is chosen. No Events settings are necessary for the dropdown either, as I thought they might be.
I deleted this page and started again, and replaced the default business name data field with a drop down, I set the dropdown as:
Options: #datasources.Addresses.items
Value: #datasources.Addresses.item
It works fine?! Not sure what happend in my original page!
UPDATE:
So it seems you need to delete both the Value and Options and then re-enter these. The OK will light up when you do.
Also, my original take on App Maker was to build the UI and attach data. That was my first mistake. You build the data then have App Maker build edit/add pages for you.

The name 'grbFiltro' does not exist in the current context

i want to assaign value to string a according to radio button selection list....for that i used this code but it showing the error...
"The name 'grbFiltro' does not exist in the current context"
This is my code...
IEnumerable<RadioButton> buttons = grbFiltro.Controls.OfType<RadioButton>() ;
foreach (var Button in buttons)
{
if (Button.Checked)
{
//Do Somethingrb
}
}
what is grbFiltro? i searched in google but no satisfied answer....for me
The question which you have posted is based on winforms and not asp.net, the tag which you have given in your question. In the said question, it is talking about a groupbox (I think) which is not available in asp.net. You can achieve a similar functionality though. Check the following links. What made you copy the code in that link in the first place is what intrigues me though.
http://weblogs.asp.net/kencox/archive/2006/09/14/Creating-a-GroupBox-in-ASP.NET.aspx
GroupBox not in toolbox

How to access Dynamically Generated Controls not knowing their names

My page generates a list of check boxes at run time. I set the Id of each of these boxes manually by assigning to each the string.Format("checkbox_{0}, n} where n is a running number.
How to I find out the list of checkbox controls from codebehind? I couldn't find them in Page.Form.Controls. Where are they placed?
(I can see the checked checkboxes in Request.Form but that contains client side names of each control.)
Thanks.
-
---More info----
My iterative loop Adds CheckBoxes to a Panel control (created in the mark-up page) as follows:
CheckBox checkbox = new CheckBox();
checkbox.Text = "Add to list";
checkbox.ID = string.Format("checkbox_{0}", n.ToString("0"));
Panel1.Controls.Add(checkbox);
The checkboxes show up nicely on the page. I can tick. When I submit the Http POST stream contains all those ticked ones.
On post back, Panel1.Controls.Count is 0. When the page was first generated, Panel1.Controls.Count was 200, as it contained lots of LieteralControls for layout.
You can use the findcontrol method on page object or parent control object to find that checkbox object. like:
CheckBox chx=(CheckBox)Page.FindControl("Checkbox1");
if(chx!=null)
chx.Checked = false;
you can put this insode the loop also like:
for(int i=0;i<n;i++)
{
CheckBox chx=(CheckBox)Page.FindControl("Checkbox"+n);
if(chx!=null)
chx.Checked = false;
}
On post back, Panel1.Controls.Count is 0. When the page was first generated, Panel1.Controls.Count was 200, as it contained lots of LieteralControls for layout.
If you add controls dynamically in this way, you need to add them again (same ids, same order) on each postback.
Which means you need to persist whatever info you need to add them again. For example, you could store the number of checkboxes in ViewState, then regenerate that number of checkboxes on each post back.

How navigate to next page using AJAX in MVC4?

I don't have so much experience using AJAX in a MVC application, in fact is my first facing. Please check the below image and note the rectangles.
The image is just an example that I took from internet.
The biggest rectangle is a partial view in my application and I have to render it when the user press Continue or Continuar button. The application should replace the current view for another without refresh the page.
This is the code which I'm testing, note first that I'm passing the first element of a list, but when the user press the button, render the view with the next element index = 2.
public ActionResult DoTest()
{
if (!Request.IsAjaxRequest())
{ }
List<Worksheet> worksheets = new List<Worksheet>()
{
new Worksheet("Hoja 1", ...),
new Worksheet("Hoja 2", ...)
};
return View(worksheets[0]);
}
Can orient me a little bit to know how to implement this feature? I just know that I need to use Ajax.
Have a look through the tutorials and examples here. There's plenty of other material around on the web with information on this subject.
There are many different ways you can achieve this. One way would be to write a custom paging Helper (HtmlHelper) that accepts new content upon the post event. You can view all about Helpers here : Custom HTML Helpers
Another way could be to use partial page rendering to achieve the partial page update upon post event.
If I was you I would combine a partial view with a jquery function to update the content. You can view some help on that here: Change dive content with Jquery

C#: How to Re-RegisterClientScripts?

I have a problem which I can't find a solution. I have a Parent page calling dynamically a default user control ( 6 different UC based on a selected item Combo X on the parent page). Within the UC, i inject (registerClientscript) a javascript which controls visibility within the UC based on a combo box. All UC has the same combo but the controls within this UC can vary.
The problem i am having is that on first load, the JS is generated correctly... but on change of Combo X from the parent, i trigger a partial refresh of the UC, which in turn re-register a new JS.
function DefineView(sender, eventArgs) {
var comboSearch = $find('%%cmbSearchType%%');
//cmbSearch Section
switch (comboSearch.get_selectedItem().get_value()) {
[[MY CODE HERE]
}
}
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "SearchVisibilityPPSA",jsFunction.Replace("%%cmbSearchType%%", cmbSearchType.ClientID),true);
As you can see, I replace the ClientID, and on first load of the page, this is resolved correctly like ctl00_PrincipalPlaceHolder_ctl00_cmbSearchType but when I change Combo X, it reloads the user control, which in turn reload the script above. In the rendered HTML, The COmbo ID is renamed to ctl00_PrincipalPlaceHolder_ctl01_cmbSearchType (Note the subtle change in name from ct00 to ct01 ) In my debug, I saw this ClientID contain the new ID but somehow it is not replaced regenrated on the rendered html.
I guess my question is how do i force the JS to be re-rendered every time this UC is called? For some reason, it is always using the original rendered JS ( which is why it is working the first time)
I think this is related to my dynamic control i was generating without assigining any id ... by forcing the id attribute, it kept it the same...

Resources