ASP. NET practical code (3) (1)

Source: Internet
Author: User

1. Read the TextBox value of the DataGrid Control.

CODE:foreach(DataGrid dgi in yourDataGrid.Items){TextBox tb = (TextBox)dgi.FindControl("yourTextBoxId");tb.Text....}

2. there are three template columns in the DataGrid: DG_ShuLiang (Quantity), DG_DanJian (unit price), and DG_JinE (amount) in columns 5, 6, and 7, respectively, it is required that the amount be calculated automatically when the quantity and unit price are entered: quantity * unit price = amount. The input time limit is also required to be numeric. How can I use a client script to implement this function?

CODE: <asp: TemplateColumn HeaderText = "quantity"> <ItemTemplate> <asp: TextBox id = "ShuLiang" runat = 'server' Text = '<% # DataBinder. eval (Container. dataItem, "DG_ShuLiang") %> '/> <asp: regularExpressionValidator id = "revS" runat = "server" C ErrorMessage = "must be integer" Validati/> </ItemTemplate> </asp: TemplateColumn> <asp: templateColumn HeaderText = "unit price"> <ItemTemplate> <asp: TextBox id = "DanJian" runat = 'server' Text = '<% # DataBinder. eval (Container. dataItem, "DG_DanJian") %> '/> <asp: regularExpressionValidator id = "revS2" runat = "server" C ErrorMessage = "must be numeric" Validati/> </ItemTemplate> </asp: TemplateColumn> <asp: templateColumn HeaderText = "amount"> <ItemTemplate> <asp: TextBox id = "JinE" runat = 'server' Text = '<% # DataBinder. eval (Container. dataItem, "DG_JinE") %> '/> </ItemTemplate> </asp: TemplateColumn> <script language = "jav Ascript "> function DoCal () {var e = event. srcElement; var row = e. parentNode. parentNode; var txts = row. all. tags ("INPUT"); if (! Txts. length | txts. length <3) return; var q = txts [txts. length-3]. value; var p = txts [txts. length-2]. value; if (isNaN (q) | isNaN (p) return; q = parseInt (q); p = parseFloat (p); txts [txts. length-1]. value = (q * p ). toFixed (2) ;}</script>

3. Why do I always refresh the row selected at the bottom of the datagrid, and then scroll to the top of the page? The selected row cannot be seen due to the relationship between the screen.

Page_load

Page. smartNavigation = true

4. modify data in the Datagrid. When you click the edit key, the data appears in the text box. How to control the size of the text box?

CODE:private void DataGrid1_ItemDataBound(obj sender,DataGridItemEventArgs e){for(int i=0;i<e.Item.Cells.Count-1;i++)if(e.Item.ItemType==ListItemType.EditType){e.Item.Cells.Attributes.Add("Width", "80px")} }

5. Dialog Box

CODE:private static string ScriptBegin = "<script language=\"JavaScript\">";private static string ScriptEnd = "</script>";public static void ConfirmMessageBox(string PageTarget,string Content){string C+Content+"’);"+"if(retValue){window.location=’"+PageTarget+"’;}";ConfirmContent=ScriptBegin + ConfirmContent + ScriptEnd;Page ParameterPage = (Page)System.Web.HttpContext.Current.Handler;ParameterPage.RegisterStartupScript("confirm",ConfirmContent);//Response.Write(strScript);}

6. format the time: string aa = DateTime. Now. ToString ("MM dd, yyyy ");

Take the current year, month, day, hour, minute, second: currentTime = System. DateTime. Now;

Take the current Year: int Year = DateTime. Now. Year;

Take the current Month: int Month = DateTime. Now. Month;

Take the current Day: int Day = DateTime. Now. Day;

Current Time: int = DateTime. Now. Hour;

Take the current score: int score = DateTime. Now. Minute;

Take the current Second: int Second = DateTime. Now. Second;

Take the current Millisecond: int Millisecond = DateTime. Now. Millisecond;

7. Custom paging code:

First define the variable:

CODE: public static int pageCount; // total number of pages public static int curPageIndex = 1; // next page of the current page: if (DataGrid1.CurrentPageIndex <(DataGrid1.PageCount-1 )) {DataGrid1.CurrentPageIndex + = 1; curPageIndex + = 1;} bind (); // Previous Page of the Data Binding function of DataGrid1: if (DataGrid1.CurrentPageIndex> 0) {DataGrid1.CurrentPageIndex + = 1; curPageIndex-= 1;} bind (); // page jump of the Data Binding function of DataGrid1: int a = int. parse (JumpPage. value. trim (); // JumpPage. value. trim () is the jump value if (a <DataGrid1.PageCount) {this. dataGrid1.CurrentPageIndex = a;} bind ();

8. Use the DataGrid:

Confirm deletion:

Private void maid (object sender, System. web. UI. webControls. dataGridItemEventArgs e) {foreach (maid di in this. dataGrid1.Items) {if (di. itemType = ListItemType. item | di. itemType = ListItemType. alternatingItem) {(LinkButton) di. cells [8]. controls [0]). attributes. add ("onclick", "return confirm ('Are you sure you want to delete this item? ');");}}}

Style alternation:

CODE:ListItemType itemType = e.Item.ItemType; if (itemType == ListItemType.Item ) { e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor=’#FFFFFF’;"; e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor=’#d9ece1’;cursor=’hand’;" ; } else if( itemType == ListItemType.AlternatingItem) { e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor=’#a0d7c4’;"; e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor=’#d9ece1’;cursor=’hand’;" ; }

Add an ID column:

CODE: DataTable dt = c. executeRtnTableForAccess (sqltxt); // The able DataColumn dc = dt. columns. add ("number", System. type. getType ("System. string "); for (int I = 0; I <dt. rows. count; I ++) {dt. rows ["number"] = (I + 1 ). toString ();} DataGrid1.DataSource = dt; DataGrid1.DataBind (); add a CheckBox in the DataGrid1, and add a full selection box private void CheckBox2_CheckedChanged (object sender, System. eventArgs e) {foreach (maid) {(CheckBox) thisitem. cells [0]. controls [1]). checked = CheckBox2.Checked ;}}

Delete all data displayed on the current page.

CODE: foreach (maid in maid) {if (CheckBox) thisitem. cells [0]. controls [1]). checked) {string strloginid = maid [thisitem. itemIndex]. toString (); Del (strloginid); // delete function }}


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.