Dynamic add control first-time experience-gridview

Source: Internet
Author: User

Recently, the impression on the gridview is getting worse and worse, so recently I often want to have some controls to replace it.Article, It seems that repeater is a good choice.

---------- A small source of bad impressions ------------

Some time ago, when I checked the slow access speed of the system, I accidentally noticed that the HTML generated by my own page was too large. After a rough look at the generated HTML source file, I only saw a slap in the eye and a table with hundreds of rows. Why? First, the viewstate of the gridview is not disabled. I want to disable it. However, if a template is listed, and a button is used for sorting in the template column, the disabled button becomes invalid. This makes me very depressed, a bloody case caused by a template column. However, I was lucky to see an article in the garden: I found that viewstate can be disabled for a column in a row.

For example:

 
Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs e) {If (E. Row. dataitem = NULL) return; E. Row. enableviewstate = false ;}

Then what about the hundreds of rows of table? Actually, there are only a dozen rows of data. After careful consideration, we found that this was a bloody case caused by the template column.

For example, a default template column is generated:
<Asp: templatefield> <edititemtemplate> <asp: textbox id = "textbox1" runat = "server" text = '<% # BIND ("orderid ") %> '> </ASP: textbox> </edititemtemplate> <itemtemplate> <asp: label id = "label1" runat = "server" text = '<% # BIND ("orderid") %>'> </ASP: label> </itemtemplate> </ASP: templatefield>

After running:

 
</Tr> <TD> <span id = "gridview1_ctl02_label1"> 10258 </span> </TD> <span id = "gridview1_ctl02_label2"> ernsh </span> </TD> </tr>

The label is parsed into span, with countless spaces and carriage returns...

It seems much better to try it several times.

1: pin it to the top: a space and a byte

2: discard the label.

3: write in the same line

 
<Asp: templatefield> <itemtemplate> <% # eval ("orderid") %> </itemtemplate> </ASP: templatefield>

 

---------- Big reasons for bad impressions ------------

A few days ago, I came up with a requirement to dynamically Add a form.

I have never tried to add controls dynamically in ASP. NET before. So what I can think:

1. Add a form using JavaScript and submit it to the server at one time.

2. implement it on the server side, return once, add a batch, return once, and add a batch...

I have never done either, but I do not like the latter very much: return a page refresh once, and an essential viewstate... However, considering the time constraints, I chose the latter (I think the latter is relatively simple ).

 

The effect to be achieved is as follows:

After some Google searches, I began to try again after learning the general practice. However, I immediately realized a headache: the dynamic addition of rows in the gridview seems to need to be done from the data source. This means that the user needs to query the database and re-bind the data by clicking the Add row button once.

I can't help it. I bit my teeth and continue. However, the tragedy is that three hours later, I still did not come up and gave up.

 

Finally, I tried to implement it using scripts, so the gridview became an obstacle: I needed to customize the Control ID in the cell to group the control when the value is set (as shown in, multiple rows of the same inventory information are in the same group ). I don't know whether the gridview can be implemented. In the end, I thought I 'd just splice the table on the ASPX page.

Below isCode: You can use it for reference.

<Table id = "tsell" class = "list_tbl"> <tr> <TH style = "width: 120px; "rowspan =" 1 "> vaccine name </Th> <TH rowspan =" 1 "style =" width: 70px; "> specification </Th> <TH rowspan =" 1 "> system batch number </Th> <TH rowspan =" 1 "style =" width: 70px; "> available stock </Th> <TH rowspan =" 1 "style =" width: 70px; "> effective time </Th> <TH> receipt Unit </Th> <TH> quantity </Th> <TH> unit price </Th> <TH> downstream time </ th> </tr> <% IF (! Ispostback) {%> <% ilist <iview_stock> stocks = getsource (); %> <% for (INT I = 0; I <stocks. count; I ++) %> <% {iview_stock stock = stocks [I]; %> <tr id = "Tr <% = I %>"> <TD> <% = stock. name %> <input type = "hidden" id = "hdstock <% = I %> 0" name = "hdstock <% = I %> 0" value = "<% = stock. id %> "/> </TD> <% = stock. model + "" + stock. unit + "/" + stock. modelname %> </TD> <% = stock. lot %> </TD> <% = stock. amount1 + stock. modelname %> </TD> <% = stock. required tdate. tow.datestring () %> </TD> <TD id = "DDL" style = 'text-align: Left; '> <% = getdepart (I) %> <a id = "lbadd <% = I %>" class = "button" href = "javascript: addnewdepart (<% = I %>)" style = "width: 15px; "> + </a> <a id =" A1 "class =" button "href =" javascript: reducenewdepart (<% = I %>) "Style =" width: 15px; ">-</a> </TD> <input type =" text "id =" tbamount <% = I %> 0 "name =" tbamount <% = I %> 0 "style =" width: 30px "/> <% = stock. modelname %> </TD> <input type = "text" id = "tbprice <% = I %> 0" name = "tbprice <% = I %> 0 "value =" <% = stock. price %> "Disabled =" disabled "style =" width: 40px "/> <% =" Yuan/"+ stock. modelname %> </TD> <input type = "text" id = "tb1_< % = I %> 0" name = "tb1_< % = I %> 0 "Class =" date_input "style =" width: 70px "value =" <% = datetime. now. tow.datestring () %> "/> </TD> </tr> <% }%> </table>
And some jquery scripts:
 
// Add the row function addnewdepart (INDEX) {var tr =$ ("# TR" + index); var newtr = "";
 
// Piece together the HTML of the new line .... Newtr + = "<TD style = 'text-align: Left; '>" + Tr. children (). eq (5 ). children (). eq (0 ). outerhtml () + "</TD>"; newtr + = tr. children (). eq (6 ). outerhtml (); newtr + = tr. children (). eq (7 ). outerhtml (); newtr + = tr. children (). eq (8 ). outerhtml (); var rowspan = tr. children (). eq (0 ). ATTR ("rowspan"); rowspan = Number (rowspan) + 1; tr. children (). eq (0 ). ATTR ("rowspan", rowspan); tr. children (). eq (1 ). ATTR ("rowspan", rowspan); tr. children (). eq (2 ). ATTR ("rowspan", rowspan); tr. children (). eq (3 ). ATTR ("rowspan", rowspan); tr. children (). eq (4 ). ATTR ("rowspan", rowspan );
 // Replace the Control ID var ddlid = new Regexp ("ddldepart" + index + "0", "G "); vaR tbamountid = new Regexp ("tbamount" + index + "0", "G"); var tbpriceid = new Regexp ("tbprice" + index + "0 ", "G"); var tbeffectid = new Regexp ("tbbench" + index + "0", "G"); var datacss = new Regexp ("date_input "); newtr = newtr. replace (ddlid, "ddldepart" + index + dic. get (INDEX); newtr = newtr. replace (tbamountid, "tbamount" + index + dic. get (INDEX); newtr = newtr. replace (tbpriceid, "tbprice" + index + dic. get (INDEX); newtr = newtr. replace (tbeffectid, "tb1_" + index + dic. get (INDEX); // newtr = newtr. replace (datacss, "date_inputc" + index + dic. get (INDEX); // var table =$ ("# tsell"); tr. after ("" + newtr + ""); var old = dic. get (INDEX) + 1; dic. put (index, old) ;}
// The following is the submission function sellsubmit () {var sellist = new array (); for (I = 0; I <dic. getsize (); I ++) {var TR = $ ("# TR" + I); var stockamount = parseint (TR. children (). eq (3 ). text (); var amounts = $ ("input [name ^ = tbamount" + I + "]"); var sum = 0; For (j = 0; j <amounts. length; j ++) {var stockid = $ ("# hdstock" + I + "0 "). val (); var DDL =$ ("# ddldepart" + I + J );
 
// N lines of data verification code are omitted below
VaR buyrangeid = $ ("# ddldepart" + I + J ). val (); var amount = $ ("# tbamount" + I + J ). val (); var price = $ ("# tbprice" + I + J ). val (); var selldate = $ ("# tb1_" + I + J ). val (); var sellinfo = new object (); sellinfo. amount = amount; sellinfo. buyrangeid = buyrangeid; sellinfo. sotckid = stockid; sellinfo. raport = NULL; sellinfo. selldate = selldate; sellinfo. sellprice = price; sellist. push (sellinfo) ;}} pig. bacter. bacterservice. bacterinclu( sellist, user, bactersellsucceed, onajaxfailded); Return false ;}

 

 

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.