Click here to Skip to main content
15,887,135 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: How to Compare two dates [modified] Pin
mnaveed21-Jul-06 2:27
mnaveed21-Jul-06 2:27 
AnswerRe: How to Compare two dates Pin
VenkataRamana.Gali21-Jul-06 2:50
VenkataRamana.Gali21-Jul-06 2:50 
QuestionSelectedIndexChanged event only fired the second time Pin
spoa21-Jul-06 0:22
spoa21-Jul-06 0:22 
AnswerRe: SelectedIndexChanged event only fired the second time Pin
minhpc_bk21-Jul-06 16:59
minhpc_bk21-Jul-06 16:59 
AnswerRe: SelectedIndexChanged event only fired the second time Pin
spoa24-Jul-06 0:47
spoa24-Jul-06 0:47 
GeneralRe: SelectedIndexChanged event only fired the second time Pin
minhpc_bk24-Jul-06 6:06
minhpc_bk24-Jul-06 6:06 
AnswerRe: SelectedIndexChanged event only fired the second time Pin
spoa26-Jul-06 7:25
spoa26-Jul-06 7:25 
GeneralRe: SelectedIndexChanged event only fired the second time [modified] Pin
minhpc_bk26-Jul-06 19:17
minhpc_bk26-Jul-06 19:17 
Do you set a value for the ID of the dynamic user control? If you don't, you'll need to do because in the first time you load and add the dynamic control in the event handler of the radio buttons, and the control is readded on postback in another event which should occurs early in the control life cycle in order for the postback event (SelectedIndexChanged) work. So if you don't specify an id for the control, the ASP.NET will automatically generate the id for the control. And as a result of that, the control has different id values, and the ASP.NET cannot process the posted data. From the second time on, the control is always added in an event until you select a new action from the radio buttons, so the auto-generated id of the user control is the same, the ASP.NET can process the postback data and the SelectedIndexChanged event gets fired. Below is a simple example to demonstrate what you might want to do, the sample code consists of the web user control and the testing web page.

--------Web User Control sample code -----------
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="SampleWeb1.WebUserControl1" %>
<asp:TextBox ID="TextBox1" runat="server" Visible="true"></asp:TextBox>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" Visible="false" AutoPostBack="True" On_SelectedIndexChanged="DropDownList1_SelectedIndexChanged">
    <asp:ListItem Text="Item1" Value="Item1" Selected="True"></asp:ListItem>
    <asp:ListItem Text="Item2" Value="Item2" ></asp:ListItem>
    <asp:ListItem Text="Item3" Value="Item3" ></asp:ListItem>
    <asp:ListItem Text="Item4" Value="Item4" ></asp:ListItem>
</asp:DropDownList>
                
namespace SampleWeb1
{
    public partial class WebUserControl1 : UserControl
    {
        public string Action
        {
            get
            {
                if (ViewState["Action"] == null)
                    ViewState["Action"] = "Create";

                return (string)ViewState["Action"];
            }
            set
            {
                ViewState["Action"] = value;

                SetControlVisibility();
            }
        }
              
        protected void Page_Load(object sender, EventArgs e)
        {
        }
             
        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
                 
            SetControlVisibility();
        }
                
        private void SetControlVisibility()
        {
            TextBox1.Visible = (Action == "Create");
            DropDownList1.Visible = !TextBox1.Visible;
        }
                
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Write(string.Format("New selected value: {0}<br/>", DropDownList1.SelectedValue));
        }
    }
}
        
       
--------Testing web page -------------
           
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SampleWeb1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:RadioButtonList runat="server" ID="rdoActionList" AutoPostBack="True" On_SelectedIndexChanged="rdoActionList_SelectedIndexChanged">
            <asp:ListItem Text="Create" Value="Create"></asp:ListItem>
            <asp:ListItem Text="Edit" Value="Edit"></asp:ListItem>
            <asp:ListItem Text="Delete" Value="Delete"></asp:ListItem>
        </asp:RadioButtonList>
        <asp:PlaceHolder runat="server" ID="PlaceHolder1">
        </asp:PlaceHolder>
    </form>
</body>
</html>
           
namespace SampleWeb1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
            
        private string CurrentControlUrl
        {
            get
            {
                if (ViewState["CurrentControlUrl"] == null)
                    return string.Empty;

                return (string)ViewState["CurrentControlUrl"];
            }
            set 
            {
                ViewState["CurrentControlUrl"] = value;
            }
        }
             
        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);

            if (!string.IsNullOrEmpty(CurrentControlUrl))
            {
                WebUserControl1 control = (WebUserControl1)LoadControl(CurrentControlUrl);
                control.ID = "WebUserControl1";
                PlaceHolder1.Controls.Add(control);
            }
        }
              
        protected void rdoActionList_SelectedIndexChanged(object sender, EventArgs e)
        {
			//Persist the dynamic user control to reload it on postback.
            CurrentControlUrl = "WebUserControl1.ascx";
            WebUserControl1 control = (WebUserControl1)LoadControl(CurrentControlUrl);
                     
			//Explicitly specify the id of the control.
            control.ID = "WebUserControl1";
            PlaceHolder1.Controls.Clear();
            PlaceHolder1.Controls.Add(control);
            control.Action = rdoActionList.SelectedValue;
        }
    }
}
Last modified: Thursday, July 27, 2006 12:16:05 AM --

AnswerRe: SelectedIndexChanged event only fired the second time Pin
spoa27-Jul-06 0:32
spoa27-Jul-06 0:32 
Questionon server no validator control is working,How? Pin
Amit Agarrwal21-Jul-06 0:02
Amit Agarrwal21-Jul-06 0:02 
AnswerRe: on server no validator control is working,How? Pin
mnaveed21-Jul-06 2:38
mnaveed21-Jul-06 2:38 
QuestionSort in gridview? Pin
blurMember20-Jul-06 23:41
blurMember20-Jul-06 23:41 
AnswerRe: Sort in gridview? Pin
Paddy Boyd21-Jul-06 0:02
Paddy Boyd21-Jul-06 0:02 
AnswerRe: Sort in gridview? Pin
mnaveed21-Jul-06 2:44
mnaveed21-Jul-06 2:44 
QuestionHow to pass data from one page to another page.. Pin
ypsyong20-Jul-06 23:35
ypsyong20-Jul-06 23:35 
AnswerRe: How to pass data from one page to another page.. Pin
_AK_20-Jul-06 23:42
_AK_20-Jul-06 23:42 
GeneralRe: How to pass data from one page to another page.. Pin
ypsyong21-Jul-06 0:00
ypsyong21-Jul-06 0:00 
GeneralRe: How to pass data from one page to another page.. Pin
_AK_21-Jul-06 0:04
_AK_21-Jul-06 0:04 
GeneralRe: How to pass data from one page to another page.. Pin
ypsyong21-Jul-06 0:16
ypsyong21-Jul-06 0:16 
GeneralRe: How to pass data from one page to another page.. Pin
_AK_21-Jul-06 0:45
_AK_21-Jul-06 0:45 
GeneralRe: How to pass data from one page to another page.. Pin
Amit Agarrwal21-Jul-06 0:20
Amit Agarrwal21-Jul-06 0:20 
GeneralRe: How to pass data from one page to another page.. Pin
_AK_21-Jul-06 0:32
_AK_21-Jul-06 0:32 
GeneralRe: How to pass data from one page to another page.. Pin
Amit Agarrwal21-Jul-06 0:40
Amit Agarrwal21-Jul-06 0:40 
GeneralRe: How to pass data from one page to another page.. Pin
_AK_21-Jul-06 0:48
_AK_21-Jul-06 0:48 
AnswerRe: How to pass data from one page to another page.. Pin
blurMember20-Jul-06 23:46
blurMember20-Jul-06 23:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.