Asp.net development-Detail Summary 3 [dynamic control creation in the background], asp.net --

Source: Internet
Author: User

Asp.net development-Detail Summary 3 [dynamic control creation in the background], asp.net --

First, click Add to add a data record. Click Delete to delete the record;

 

The Code is as follows:

Protected void Page_Load (object sender, EventArgs e)

{

CreateTable ();

}

/// <Summary>

/// Add a new receipt

/// </Summary>

/// <Param name = "sender"> </param>

/// <Param name = "e"> </param>

Protected void btn_shouju_add_Click (object sender, EventArgs e)

{

If (string. IsNullOrEmpty (txt_shouju_number.Text ))

{

Lb_msg_shouju.Text = "the receipt number cannot be blank! ";

Return;

}

If (ViewState ["shoujuCount"] = null | ViewState ["shoujuCount"]. ToString () = "0 ")

{

ViewState ["shoujuCount"] = 1;

ViewState ["shoujuNumbers"] = txt_shouju_number.Text;

ViewState ["shoujuStates"] = ddl_shouju_state.SelectedItem.Text;

}

Else

{

String shoujuNumber = ViewState ["shoujuNumbers"]. ToString () + "| ";

If (shoujuNumber. Contains (txt_shouju_number.Text + "| "))

{

Lb_msg_shouju.Text = "this receipt already exists. Please add it again! ";

Return;

}

ViewState ["shoujuCount"] = int. Parse (ViewState ["shoujuCount"]. ToString () + 1;

ViewState ["shoujuNumbers"] + = "|" + txt_shouju_number.Text;

ViewState ["shoujuStates"] + = "|" + ddl_shouju_state.SelectedItem.Text;

}

CreateTable ();

}

/// <Summary>

/// Create a receipt list

/// </Summary>

Void CreateTable ()

{

Int count = ViewState ["shoujuCount"] = null? 0: Convert. ToInt32 (ViewState ["shoujuCount"]);

List <string> numbers = ViewState ["shoujuNumbers"]! = Null? StrsToList (ViewState ["shoujuNumbers"]. ToString (): null;

List <string> states = ViewState ["shoujuStates"]! = Null? StrsToList (ViewState ["shoujuStates"]. ToString (): null;

Tb_shouju_list.Controls.Clear ();

If (count> 0)

{

# Region create a header

TableHeaderRow head_row = new TableHeaderRow ();

 

TableHeaderCell head_cell_number = new TableHeaderCell ();

TableHeaderCell head_cell_state = new TableHeaderCell ();

TableHeaderCell head_cell_delete = new TableHeaderCell ();

 

Literal head_lateral_number = new Literal ();

Head_lateral_number.Text = "receipt number ";

Literal head_lateral_state = new Literal ();

Head_lateral_state.Text = "receipt status ";

Literal head_lateral_delete = new Literal ();

Head_lateral_delete.Text = "operation ";

 

Head_cell_number.Controls.Add (head_lateral_number );

Head_cell_state.Controls.Add (head_lateral_state );

Head_cell_delete.Controls.Add (head_lateral_delete );

 

Head_row.Controls.Add (head_cell_number );

Head_row.Controls.Add (head_cell_state );

Head_row.Controls.Add (head_cell_delete );

Tb_shouju_list.Rows.Add (head_row );

# Endregion

 

For (int I = 0; I <count; I ++)

{

# Region looping creates a table subject

TableRow row = new TableRow ();

Row. ID = "tr _" + I;

TableCell cell_number = new TableCell ();

TableCell cell_state = new TableCell ();

TableCell cell_delete = new TableCell ();

 

 

Literal literal_number = new Literal ();

Literal_number.Text = numbers [I];

 

Literal literal_state = new Literal ();

Literal_state.Text = states [I];

 

Button btn_temp = new Button ();

Btn_temp.Text = "delete ";

Btn_temp.Attributes ["delID"] = I. ToString ();

Btn_temp.Click + = new EventHandler (Delete_shoujuRow );

 

Cell_number.Controls.Add (literal_number );

Cell_state.Controls.Add (literal_state );

Cell_delete.Controls.Add (btn_temp );

 

Row. Controls. Add (cell_number );

Row. Controls. Add (cell_state );

Row. Controls. Add (cell_delete );

 

Tb_shouju_list.Rows.Add (row );

# Endregion

}

}

 

}

 

List <string> strsToList (string str)

{

If (! String. IsNullOrEmpty (str. Trim ()))

{

String [] strs = str. Split ('| ');

List <string> list = new List <string> ();

List. AddRange (strs );

Return list;

}

Return null;

}

/// <Summary>

/// Delete -- added row

/// </Summary>

/// <Param name = "sender"> </param>

/// <Param name = "e"> </param>

Void Delete_shoujuRow (object sender, EventArgs e)

{

Button btn = (Button) sender;

Int id = Convert. ToInt32 (btn. Attributes ["delID"]);

Int count = ViewState ["shoujuCount"] = null? 0: Convert. ToInt32 (ViewState ["shoujuCount"]);

List <string> numbers = ViewState ["shoujuNumbers"]! = Null? StrsToList (ViewState ["shoujuNumbers"]. ToString (): null;

List <string> states = ViewState ["shoujuStates"]! = Null? StrsToList (ViewState ["shoujuStates"]. ToString (): null;

If (count> 0)

{

Numbers. RemoveAt (id );

States. RemoveAt (id );

ViewState ["shoujuNumbers"] = string. Join ("|", numbers );

ViewState ["shoujuStates"] = string. Join ("|", states );

ViewState ["shoujuCount"] = count-1;

}

CreateTable ();

}

 


Several controls dynamically created during page loading in aspnet 35 (c #) call the same method but pass different parameters

It seems that this function is not available ~

Aspnet dynamically adds a control. Each time you click "Add button", a Textbox is created (values can be accessed in the background)

A major problem I have simply read is that the status of the dynamic control is saved after the return. That is, after you dynamically create a control, you can find that the control is missing or overwritten. now!
To solve this problem, we only need to save the view State of the Control. We can use viewstate to save it or static variables to save it. Because the aspx page cycle restores the View data in each load event therefore, the controls added in the background will be cleared and need to be manually encoded. The following is a simple example. Of course, you can use the static variables I used in viewstate.
Aspx front-end code:
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: Button ID = "button1" runat = "server" Text = "CREATE" onclick = "button#click"/>
<Asp: Panel ID = "Panel1" runat = "server">
</Asp: Panel>
<Asp: Button ID = "id2" runat = "server" Text = "get all values" onclick = "id2_Click"/>
</Div>
</Form>
</Body>
Background code:
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// Restore the control
RestoreTextBox ();
}
// Static variable storage control list
Static List <TextBox> txtlist = new List <TextBox> ();
// Control id
Static int id = 1;

Protected void button#click (object sender, EventArgs e)
{
Var txt = new TextBox
{
ID = id. ToString (),
Text = "Sample textbox" + id. ToString ()
};

This. Panel1.Controls. Add (txt );
Txtlist. Add (txt );
// Id auto-increment 1
Id ++;
}

// & Amp; #4 ...... remaining full text & gt;>

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.