Asp.net dynamic management (add, delete, read) Controls

Source: Internet
Author: User

Oh, you just need to talk about it.Code.

I. Page code

 

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "test. aspx. cs" inherits = "test" %>

<! 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 id = "head1" runat = "server">
<Title> </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: button id = "button3" runat = "server" text = "assign value" onclick = "button3_click"/>
<Asp: button id = "button2" runat = "server" onclick = "button2_click" text = "value"/>
<Table width = "280px">
<Tr>
<TD>
<Asp: Panel id = "Panel1" runat = "server">
</ASP: Panel>
<Asp: Label id = "label1" runat = "server" text = "label"> </ASP: Label>
<Asp: button id = "button1" runat = "server" text = "add" onclick = "button#click"/>
</TD>
</Tr>
</Table>
</Div>
</Form>
</Body>
</Html>

 

 

Ii. Background code

 

Using system;
Using system. configuration;
Using system. Data;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;

Public partial class test: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Viewstate ["num"] = 3;
}

Loadctrl (Int. parse (viewstate ["num"]. tostring ()));
}

/// <Summary>
/// Set the value
/// </Summary>
Private void settxtvalue ()
{
Viewstate ["num"] = 5;
Loadctrl (Int. parse (viewstate ["num"]. tostring ()));

For (INT I = 0; I <5; I ++)
{
Textbox TXT = panel1.findcontrol ("TXT" + I. tostring () as textbox;
TXT. Text = I. tostring ();
}
}

/// <Summary>
/// Load controls
/// </Summary>
/// <Param name = "num"> </param>
Private void loadctrl (INT num)
{
Panel1.controls. Clear ();

For (INT I = 0; I <num; I ++)
{
Textbox TXT = new Textbox ();
TXT. ID = "TXT" + I. tostring ();
TXT. Text = request. Form [TXT. clientid];

Linkbutton lbtn = new linkbutton ();
Lbtn. ID = "lbtn" + I. tostring ();
Lbtn. commandargument = I. tostring ();
Lbtn. Text = "delete ";
Lbtn. Click + = new eventhandler (btndel_click );

Literal LTL = new literal ();
LTL. ID = "LTL" + I. tostring ();
LTL. Text = "<br/> ";

panel1.controls. add (txt);
panel1.controls. add (lbtn);
panel1.controls. add (LTL);
}< BR >}

///


/// Delete
///
///
//
protected void btndel_click (Object sender, eventargs e)
{< br> linkbutton lbtn = sender as linkbutton;
string id = lbtn. commandargument;
textbox TXT = panel1.findcontrol ("TXT" + id. tostring () as textbox;
literal LTL = panel1.findcontrol ("LTL" + id. tostring () as literal;

Panel1.controls. Remove (txt );
Panel1.controls. Remove (lbtn );
Panel1.controls. Remove (LTL );
Viewstate ["num"] = int. parse (viewstate ["num"]. tostring ()-1;

Setcontrolsid ();
}

///


/// add
///
///
//
protected void button#click (Object sender, eventargs e)
{< br> viewstate ["num"] = int. parse (viewstate ["num"]. tostring () + 1;
loadctrl (Int. parse (viewstate ["num"]. tostring ();
}

/// <Summary>
/// Value
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void button2_click (Object sender, eventargs E)
{
Label1.text = string. empty;

For (INT I = 0; I <(INT) viewstate ["num"]; I ++)
{
Label1.text + = (textbox) (panel1.findcontrol ("TXT" + I. tostring (). text;
}
}

/// <Summary>
/// Reset the Control ID
/// </Summary>
Private void setcontrolsid ()
{
Int I = 0;

Foreach (control CTL in panel1.controls)
{
If (CTL. GetType (). Name. tolower () = "textbox ")
{
CTL. ID = "TXT" + I. tostring ();
}
If (CTL. GetType (). Name. tolower () = "linkbutton ")
{
CTL. ID = "lbtn" + I. tostring ();
}
If (CTL. GetType (). Name. tolower () = "literal ")
{
CTL. ID = "LTL" + I. tostring ();

I ++;
}
}
}

/// <Summary>
/// Assign a value
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void button3_click (Object sender, eventargs E)
{
Settxtvalue ();

}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.