Radio Button List Selection and postback of the page - c#

In my program I have used upadate panel nad ScriptManager. My problem is,I am using radio button named RbFresh, It have two values named Fresher and Experienced when I select Experienced I will show a panel on this panel I have put text box control .But after selection any value in radionbutton total page will be full postbacks.How will avoid full postback of my page.
<asp:UpdatePanel ID="UpdatePanel4" runat="server">
<ContentTemplate>
<asp:RadioButtonList ID="RbFresh" runat="server" onselectedindexchanged="RbFresh_SelectedIndexChanged"
RepeatDirection="Horizontal" Width="297px" AutoPostBack="True">
<asp:ListItem Text="Fresher" Value="1"></asp:ListItem>
<asp:ListItem Text="Experienced" Value="2"></asp:ListItem>
</asp:RadioButtonList>
</ContentTemplate>
</asp:UpdatePanel>
<Triggers>
<asp:PostBackTrigger ControlID="Rbfresh"/>
</Triggers>

Using this code page will not be post back and you can easily get the value of radio buttons
Paste on HTML Page
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel4" runat="server">
<ContentTemplate>
<asp:RadioButtonList ID="RbFresh" runat="server" onselectedindexchanged="RbFresh_SelectedIndexChanged"
RepeatDirection="Horizontal" Width="297px" AutoPostBack="True">
<asp:ListItem Text="Fresher" Value="1"></asp:ListItem>
<asp:ListItem Text="Experienced" Value="2"></asp:ListItem>
</asp:RadioButtonList>
</ContentTemplate>
</asp:UpdatePanel>
Paste on .cs Page
protected void RbFresh_SelectedIndexChanged(object sender, EventArgs e)
{
int Value = 0; int.TryParse( RbFresh.SelectedValue,out Value);
int ID = Value;
}

Related

The labels don't update when the selected item in DropDownList changes

I've tried to use the DropDownList to update the content of a label.
The first one is the Asynchronous Transfer Mode. The page doesn't refresh when a new item is selected.
The second one is the Synchronous Transfer Mode. The entire page is refreshed when a new item is selected.
However, neither of these two labels refreshed as I like when I tried to selected a new item in my DropDownList.
Could anyone help me?
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ex8_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
</asp:ScriptManager>
<div>
Password:<asp:TextBox ID="TextBox1" runat="server" TextMode="Password"></asp:TextBox>
<br />
<br />
Asynchronous Transfer Mode</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Value="1">Item 1</asp:ListItem>
<asp:ListItem Value="2">Item 2</asp:ListItem>
<asp:ListItem Value="3">Item 3</asp:ListItem>
<asp:ListItem Value="4">Item 4</asp:ListItem>
<asp:ListItem Value="5">Item 5</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList1"
EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<p>
Show selected item <asp:Label ID="Label1" runat="server"></asp:Label>
</p>
<p>
Synchronous Transfer Mode</p>
<p>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
<asp:ListItem Value="1">Item 1</asp:ListItem>
<asp:ListItem Value="2">Item 2</asp:ListItem>
<asp:ListItem Value="3">Item 3</asp:ListItem>
<asp:ListItem Value="4">Item 4</asp:ListItem>
<asp:ListItem Value="5">Item 5</asp:ListItem>
</asp:DropDownList>
</p>
<p>
Show selected item <asp:Label ID="Label2" runat="server"></asp:Label>
</p>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ex8_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedValue;
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
Label2.Text = DropDownList2.SelectedValue;
}
}
DropDownList1 is inside UpdatePanel, while Label1 is outside of it. UpdatePanel enables rendering part of a page which is inside of it. Therefore Label1 keeps the old value. If you place Label1 inside UpdatePanel, more precise inside ContentTemplate, you will see the change of the label.
UpdatePanel renders part of the page without postback. You can see that when you select an item from DropDownList1 there is no new go back arrow in your browser. The best way to check this is when you open the .aspx page and first select an item from DropDownList1. Go back arrow will still be disabled.
DropDownList2 is not inside an UpdatePanel and it uses postback to render entire page. This is the reason why Label1, if it is outside UpdatePanel, sets its value to selected item from DropDownList1 when you select an item from DropDownList2. Label1 got the selected item from DropDownList1, it just waits for the postback, which is fired by DropDownList1.
To update Label2 you need to add event handler OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" to DropDownList2.
When removed UdpatePanel control, the DropDownList after SelectedIndexChanged, Label is updated with selectedvalue.
The ScriptManager and UpdatePanel controls remove the requirement to refresh the whole page with each postback, which improves the user experience.
More Information Here!
You can have one global UpdatePanel and then use UpdatePanels as per requirements. And then controls may not be restricted to particular UpdatePanel. It still gets refreshed.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form2" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList
ID="DropDownList1"
runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem Value="1">Item 1</asp:ListItem>
<asp:ListItem Value="2">Item 2</asp:ListItem>
<asp:ListItem Value="3">Item 3</asp:ListItem>
<asp:ListItem Value="4">Item 4</asp:ListItem>
<asp:ListItem Value="5">Item 5</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<p>
Show selected item <asp:Label ID="Label1" runat="server"></asp:Label>
</p>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>

`SelectedIndexChanged` it is not firing

I have a DropDownList and I have a <DIV> element, When I change the value of dropdown my div is not going to show up I have the following ascx code
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true">
<asp:ListItem Value="Zgjidh Kompaninë">-- Zgjidh Kompaninë--</asp:ListItem>
<asp:ListItem Value="Meridian Corporation">Meridian Corporation</asp:ListItem>
<asp:ListItem Value="Meridian Express">Meridian Express</asp:ListItem>
<asp:ListItem Value="Buka">Buka</asp:ListItem>
</asp:DropDownList>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="chart_div"></div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList2" EventName="SelectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>
If I place this <div id="chart_div"></div> outside of UpdatePanel it works or If I just delete the <Triggers> tag it works but it refreshes the page both times. So Why My Div Is Not Showing UP I need hep please
I am working on a sharepoint WebPart!

How to prevent UpdatePanel from resetting values outside the panel?

Here is my problem :
I select a type using ddlType
I click on btnAddChoice to add a few choices
I click on btnSubmit to save the form
But when I try to get ddlType.SelectedValue in btnSubmit_Click(), it's always back to "Select a type".
If I don't do step #2 then it works fine.
From what I understand, the problem is related the ajax call (postback) and could be solved by saving the SelectedValue in the Session or ViewState.
But I have many many other controls (such as ddlType) on this page so it does not seem like an elegant solution. I was hoping for the framework to keep the selected values in the ViewState automatically ... Any idea ? After all, is it not the goal of UpdatePanel to update just the panel ?
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:DropDownList ID="ddlType" runat="server">
<asp:ListItem Text="Select a type" Value=""></asp:ListItem>
<asp:ListItem Text="One" Value="1"></asp:ListItem>
<asp:ListItem Text="Two" Value="2"></asp:ListItem>
<asp:ListItem Text="Three" Value="3"></asp:ListItem>
</asp:DropDownList>
<div>
<strong>Choices</strong>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAddChoice" EventName="Click" />
</Triggers>
<ContentTemplate>
<ul class="list-unstyled">
<asp:PlaceHolder runat="server" ID="phChoices"></asp:PlaceHolder>
</ul>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="btnAddChoice" runat="server" Text="Add Choice" CausesValidation="false"
OnClick="btnAddChoice_Click" />
</div>
<asp:Button ID="btnSubmit" runat="server" Text="Add question"
OnClick="btnSubmit_Click" />
UPDATE
Part of the problem was caused by :
$('#<%= MainPanel.FindControl("btnAddChoice").ClientID %>').click(function () {
var ddlType = $('#<%= MainPanel.FindControl("ddlType").ClientID %>');
ddlType.attr('disabled', 'disabled');
});
Please see this question for details.
Regarding other controls also empty, the reason is that "dynamically created controls have to be created each and every post-back"
Try adding something like this to your ScriptManager
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
Then all the code which you want to get refreshed will come under Update Panel
<asp:Updatepanel id="UpdatePanel1" runat="server">
<ContentTemplate>
...All your HTML content here
</ContentTemplate>
</asp:Updatepanel>
Hope that helps
More information on EnablePartialRendering
UPDATE
Also if your dropdownlist value is not getting saved because of Updatepanel
try Request.Form and check
Request.Form.Get("dropdownlistID").ToString());

UpdatePanel not refreshing on my radiobuttonlist

I have some radio buttons like this:
<li class="list-group-item">
<p>YOUR AGE IS AN IMPORTANT FACTOR IN YOUR ABILITY TO TAKE ON INVESTMENT RISK. YOUR AGE IS:</p>
<asp:RadioButtonList ID="radioBtnFirst" CssClass="radioBtnAge" runat="server" RepeatDirection="Horizontal"
TextAlign="Right" AutoPostBack="True" OnSelectedIndexChanged="radioBtnFirst_SelectedIndexChanged">
<asp:ListItem Value="1">60 and over</asp:ListItem>
<asp:ListItem Value="2">50 - 59</asp:ListItem>
<asp:ListItem Value="3">40 - 49</asp:ListItem>
<asp:ListItem Value="4">30 - 39</asp:ListItem>
<asp:ListItem Value="5">Under 30</asp:ListItem>
</asp:RadioButtonList>
</li>
Now somewhere donw I have a textbox where I want to get the value of the selected radiobutton. This is the code that is doing that:
protected void radioBtnFirst_SelectedIndexChanged(object sender, EventArgs e)
{
totalScore.Text = radioBtnFirst.SelectedValue;
}
I wanted to add UpdatePanel so it won't refresh the whole page but it's not working, when I press first one of the radio buttons it doesn't happen anything, but when I press the second time, it refreshes again the whole page.
This is the code that I used for UpdatePanel:
<h3>Total Score:</h3>
<asp:UpdatePanel ID="upScore" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox runat="server" ID="totalScore" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="radioBtnFirst" EventName="SelectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>
Now it's ok, it's working. I figured out that the problem was behind the code in Page_Load method.

asp.net drop down list selectedindexchanged firing slowly compared to button click within update panel

I have a few buttons within an update panel. I have now been asked to replace the buttons with
a drop down list . I see that the selectedindexchanged event on the drop down list is much slower
than the button click event. I have the code below. Can anyone point me as to why it is happenning ?
or what I can do to make the selectedindexchanged event response faster .
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<ContentTemplate>
<div class="ui-widget-header" style="display: inline;">
<asp:Label ID="lblRefresh" runat="server" Text="Refresh Interval:" CssClass="label"
ForeColor="Black"></asp:Label>&nbsp&nbsp&nbsp
<asp:LinkButton ID="btnOFF" runat="server" OnClick="btnOFF_Click" Text="Off">
</asp:LinkButton>&nbsp
<asp:LinkButton ID="btn60SEC" runat="server" OnClick="btn60SEC_Click" Text="1Min">
</asp:LinkButton>&nbsp
<asp:LinkButton ID="btn5MIN" runat="server" OnClick="btn5MIN_Click" Text="5Min">
</asp:LinkButton>&nbsp
<asp:LinkButton ID="btn10MIN" runat="server" OnClick="btn10MIN_Click" Text="10Min">
</asp:LinkButton>&nbsp
<asp:LinkButton ID="btn15MIN" runat="server" OnClick="btn15MIN_Click" Text="15Min">
</asp:LinkButton>
<asp:DropDownList ID="ddlRefresh" runat="server" onselectedindexchanged="ddlRefresh_SelectedIndexChanged">
<asp:ListItem Text="OFF" Value="0"></asp:ListItem>
<asp:ListItem Text="5MIN" Value="5"></asp:ListItem>
<asp:ListItem Text="10MIN" Value="10"></asp:ListItem>
</asp:DropDownList>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlRefresh" EventName="SelectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>
Well what seems to work for me right now is to set AutoPostback = "true" for the dropdownlist .
<asp:DropDownList ID="ddlRefresh" runat="server" onselectedindexchanged="ddlRefresh_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="OFF" Value="0"></asp:ListItem>
<asp:ListItem Text="5MIN" Value="5"></asp:ListItem>
<asp:ListItem Text="10MIN" Value="10"></asp:ListItem>
</asp:DropDownList>
I had the same problem and I found that the problem is with _destroyTree. Check it here (https://siderite.dev/blog/very-slow-updatepanel-refresh-when.html).

Categories

Resources