manipulating server controls in the Codebehind .cs file 
Author Message
 manipulating server controls in the Codebehind .cs file

Hi, there,

I hava this ASP.NET page and on the page I have a listbox, now the problem
is I want to change the contents of the listbox on the Codebehind C# code.
The HTML code:

AutoEventWireup="true" Inherits="WebApplication1.SimpleControls" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>SimpleControls</title>
  <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content=" http://www.*-*-*.com/ ;
name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="SimpleControls" method="post" runat="server">
   <h1>Listserver Registration</h1>
   <asp:label id="LableName" Runat="server">Enter your name</asp:label><br>
   <asp:textbox id="TextName"
Runat="server"></asp:textbox><asp:requiredfieldvalidator
id="RequiredFieldValidator1" Runat="server" ControlToValidate="TextName">
   You must supply your name
   </asp:requiredfieldvalidator>
   <p>Select your preferred email delivery format</p>
   <br>
   <asp:listbox id="ListDeliveryTypes"
Runat="server"></asp:listbox><asp:requiredfieldvalidator
id="RequiredFieldValidator2" Runat="server"
ControlToValidate="ListDeliveryTypes" InitialValue="<--Select a Format-->">
   You must select a delivery format
   </asp:requiredfieldvalidator>
   <P></P>
   <asp:button id="Button1" Runat="server"
Text="Submit"></asp:button></form>
 </body>
</HTML>

The C# code in the Codebehind .cs file for LoadEvent:
private void Page_Load(object sender, System.EventArgs e)

{

if(ListDeliveryTypes.Items.Count==0)

{

ListDeliveryTypes.Items.Add("<--Select a Format-->");

ListDeliveryTypes.Items[0].Selected = true;

ListDeliveryTypes.Items.Add("Individual Emails");

ListDeliveryTypes.Items.Add("Digest");

ListDeliveryTypes.Items.Add("No Emails");

Quote:
}
}

When I run the debug, the listbox contents didn't change at all, so
obviously the code in the Codebehind C# code didn't run at all.

What could be the problem for the Codebehind C# code not working?

Thanks a lot.

-May



Wed, 05 Jan 2005 01:31:13 GMT  
 manipulating server controls in the Codebehind .cs file
Xiao,

    There is a problem in VS.NET that deletes the delegate that is used to
load the page (I've had it happen to me a few times).  What you will have to
do is open the region in the code behind page that ways "generated code" or
something like that.  In it, you should see the InitializeComponent method.
In it, there should be a statement like this:

this.Load += new EventHandler(this.Page_Load);

    If it is not there, add it, recompile, and you should be ok.

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> Hi, there,

> I hava this ASP.NET page and on the page I have a listbox, now the problem
> is I want to change the contents of the listbox on the Codebehind C# code.
> The HTML code:

> AutoEventWireup="true" Inherits="WebApplication1.SimpleControls" %>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
> <HTML>
>  <HEAD>
>   <title>SimpleControls</title>
>   <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
>   <meta content="C#" name="CODE_LANGUAGE">
>   <meta content="JavaScript" name="vs_defaultClientScript">
>   <meta content="http://schemas.microsoft.com/intellisense/ie5"
> name="vs_targetSchema">
>  </HEAD>
>  <body MS_POSITIONING="GridLayout">
>   <form id="SimpleControls" method="post" runat="server">
>    <h1>Listserver Registration</h1>
>    <asp:label id="LableName" Runat="server">Enter your

name</asp:label><br>

- Show quoted text -

Quote:
>    <asp:textbox id="TextName"
> Runat="server"></asp:textbox><asp:requiredfieldvalidator
> id="RequiredFieldValidator1" Runat="server" ControlToValidate="TextName">
>    You must supply your name
>    </asp:requiredfieldvalidator>
>    <p>Select your preferred email delivery format</p>
>    <br>
>    <asp:listbox id="ListDeliveryTypes"
> Runat="server"></asp:listbox><asp:requiredfieldvalidator
> id="RequiredFieldValidator2" Runat="server"
> ControlToValidate="ListDeliveryTypes" InitialValue="<--Select a
Format-->">
>    You must select a delivery format
>    </asp:requiredfieldvalidator>
>    <P></P>
>    <asp:button id="Button1" Runat="server"
> Text="Submit"></asp:button></form>
>  </body>
> </HTML>

> The C# code in the Codebehind .cs file for LoadEvent:
> private void Page_Load(object sender, System.EventArgs e)

> {

> if(ListDeliveryTypes.Items.Count==0)

> {

> ListDeliveryTypes.Items.Add("<--Select a Format-->");

> ListDeliveryTypes.Items[0].Selected = true;

> ListDeliveryTypes.Items.Add("Individual Emails");

> ListDeliveryTypes.Items.Add("Digest");

> ListDeliveryTypes.Items.Add("No Emails");

> }

> }

> When I run the debug, the listbox contents didn't change at all, so
> obviously the code in the Codebehind C# code didn't run at all.

> What could be the problem for the Codebehind C# code not working?

> Thanks a lot.

> -May



Tue, 04 Jan 2005 22:39:42 GMT  
 manipulating server controls in the Codebehind .cs file
Well,

  Two things.  First, make sure the Initialize method created by Visual
Studio still assisgns the event handler called Page_Load() to your Page
class.  Without this, the code will never run.

  Second, in the de{*filter*}, stop on the line:

    if(ListDeliveryTypes.Items.Count==0)

and make sure the number if items is really zero.

Erik

============
Erik Brown
Author of "Windows Forms Programming with C#"
http://www.*-*-*.com/


Quote:
> Hi, there,

> I hava this ASP.NET page and on the page I have a listbox, now the problem
> is I want to change the contents of the listbox on the Codebehind C# code.
> The HTML code:

> AutoEventWireup="true" Inherits="WebApplication1.SimpleControls" %>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
> <HTML>
>  <HEAD>
>   <title>SimpleControls</title>
>   <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
>   <meta content="C#" name="CODE_LANGUAGE">
>   <meta content="JavaScript" name="vs_defaultClientScript">
>   <meta content=" http://www.*-*-*.com/ ;
> name="vs_targetSchema">
>  </HEAD>
>  <body MS_POSITIONING="GridLayout">
>   <form id="SimpleControls" method="post" runat="server">
>    <h1>Listserver Registration</h1>
>    <asp:label id="LableName" Runat="server">Enter your

name</asp:label><br>

- Show quoted text -

Quote:
>    <asp:textbox id="TextName"
> Runat="server"></asp:textbox><asp:requiredfieldvalidator
> id="RequiredFieldValidator1" Runat="server" ControlToValidate="TextName">
>    You must supply your name
>    </asp:requiredfieldvalidator>
>    <p>Select your preferred email delivery format</p>
>    <br>
>    <asp:listbox id="ListDeliveryTypes"
> Runat="server"></asp:listbox><asp:requiredfieldvalidator
> id="RequiredFieldValidator2" Runat="server"
> ControlToValidate="ListDeliveryTypes" InitialValue="<--Select a
Format-->">
>    You must select a delivery format
>    </asp:requiredfieldvalidator>
>    <P></P>
>    <asp:button id="Button1" Runat="server"
> Text="Submit"></asp:button></form>
>  </body>
> </HTML>

> The C# code in the Codebehind .cs file for LoadEvent:
> private void Page_Load(object sender, System.EventArgs e)

> {

> if(ListDeliveryTypes.Items.Count==0)

> {

> ListDeliveryTypes.Items.Add("<--Select a Format-->");

> ListDeliveryTypes.Items[0].Selected = true;

> ListDeliveryTypes.Items.Add("Individual Emails");

> ListDeliveryTypes.Items.Add("Digest");

> ListDeliveryTypes.Items.Add("No Emails");

> }

> }

> When I run the debug, the listbox contents didn't change at all, so
> obviously the code in the Codebehind C# code didn't run at all.

> What could be the problem for the Codebehind C# code not working?

> Thanks a lot.

> -May



Tue, 04 Jan 2005 22:46:47 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. ATL Server vs. CodeBehind / C# WebService

3. Manipulating .Wav files or Voice Recorder Control With Playback

4. resx files needed for cs - files ??

5. controls in tablecells & c# codebehinds conflict

6. CS files display in VS6 like CPP files ?

7. Include code in other Cs files

8. Reuse of cs files, namespace, arch advice pls

9. How to Generate .cs file at Runtime

10. Two CS files (using namespaces)

11. My .cs files were deleted!?

12. *.~cs files

 

 
Powered by phpBB® Forum Software