New Year
As shown in the following days
The common JavaScript code used in C # is collected here.
To remember
------------------------------
At the same time, I hope you can contribute your common JS resources. Thank you!
-----------------------------
1. Button front and back-end events
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "button"
Onclientclick = "alert ('room-side verification, blocking server-side submissions '); Return false;"/>
2. Register related events: onblur, onclick, onchange
This. textbox1.attributes. Add ("onchange ",
"Alert ('data changed, check whether the input meets the rule ');");
3. Registration attributes:
This. textbox1.attributes. Add ("readonly", "true ");
4. introduce JS files
front-end HTML page:
backend CS page:
This. registerclientscriptblock ("jsfile",
" ");
5. When you click the button, the related column is not empty.
Function checkempty (txtobj, msgshow)
{
If (txtobj. value = "")
{
Alert (msgshow );
Return false;
}
}
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "button"
Onclientclick = "Return checkempty (textbox1, 'textbox1 cannot be blank ')"/>
6.
// Select whether to use chcekbox
// Control whether the textbox is input.
Function chktextbox (chkobj, txtobj)
{
If (chkobj. Checked = true)
{
Txtobj. value = "";
Txtobj. readonly = false;
Txtobj. Focus ();
}
If (chkobj. Checked = false)
{
Txtobj. value = "";
Txtobj. readonly = true;
}
}
<Input id = "checkbox1" type = "checkbox" onclick = "chktextbox (checkbox1, textbox1)"/>
7
// pass the value to the modal window and obtain the returned value
var encodequeryname = escape (name);
var strpara = "'dialogwidth: 400px; dialogheight: 400px; dialogleft: 300px; dialogtop: 200px; toolbar: No; menubar: No; resizable: Yes; Location: No; Status: No; scrollbars = no '";
var returninfo = Window. showmodaldialog ("qryname. aspx? & Name = "+ encodequeryname +" & queryid = "+ querytype +" ",'', strpara);
If (returninfo! = NULL)
{< br> var arrayreturninfo = returninfo. split ("@");
document. all. drpid. value = arrayreturninfo [1];
document.all.txt name. value = arrayreturninfo [2];
}
8
// The confirmation dialog box for JS appears.
// Trigger background operations based on the confirmation results
If (confirm ('Are you sure you want? '))
{
Document. All. hidbtn_submit.click ();
}
Else
{
Document. All. hidbtn_cancel.click ();
}
HTML pageCode:
<Input id = "hidbtn_submit" type = "button" value = "Confirm modification"
Style = "display: none ;"
Onserverclick = "hidbtn_submit_serverclick"
Runat = "server"/>
9
// Add a page response to the shortcut key
// For example, when you press F2, add a button.
# Region add page response to shortcut keys
String strjs_javaskey = "<script language = 'javascript 'Type = 'text/JavaScript '> ";
Strjs_foreign key + = "document. onkeydown = foreign keydown ;";
Strjs_primary key + = "function compute keydown ()";
Strjs_1_key + = "{";
// Add
If (this. buttonctl1.imgbtn _ addfamily. Visible)
{
String btninsertcid = This. buttonctl1.imgbtn _ insert. clientid. Trim ();
// F2-113
Strjs_1_key + = "If (event. keycode = '000000 ')";
Strjs_1_key + = "{";
Strjs_1_key + = "document. All ('" + btninsertcid + "'). Click ();";
Strjs_1_key + = "event. keycode = 0 ;";
Strjs_1_key + = "event. returnvalue = false ;";
Strjs_1_key + = "Return false ;";
Strjs_1_key + = "}";
}
// Modify
If (this. buttonctl1.imgbtn _ edit. Visible)
{
String btneditcid = This. buttonctl1.imgbtn _ edit. clientid. Trim ();
// F3-114
Strjs_1_key + = "If (event. keycode = '000000 ')";
Strjs_1_key + = "{";
Strjs_inclukey + = "document. All ('" + btneditcid + "'). Click ();";
Strjs_1_key + = "event. keycode = 0 ;";
Strjs_1_key + = "event. returnvalue = false ;";
Strjs_1_key + = "Return false ;";
Strjs_1_key + = "}";
}
Strjs_1_key + = "}";
// Register an event
Page. registerstartupscript ("primary key", strjs_primary key );
# Endregion
10
// The pop-up prompt is displayed on the branch
Alert ('aaa \ r \ n BBB \ r \ n CCC ');
If it is registered in the background. CS File
You need
String stralertcontent = "AAA" + "\ r \ n ";
Stralertcontent + = "BBB" + "\ r \ n ";
11
// Implementation result
// When you click a row in the gridview
// Radiobutton at the beginning of the row is selected
// Save the relevant values in the hidden column at the same time
// bind the queried dataset.
If (DT. rows. count> 0)
{< br> // bind
This. gv_infofromsendmodule.datasource = DT;
This. gv_infofromsendmodule.databind ();
// click the OK button to display
This. btn_ OK .visible = true;
this.txt hid_rowcount.text = DT. rows. count. tostring ();
}
// Rowdatabound of the gridview
Protected void gv_infofromsendmodule_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowindex <0)
Return;
E. row. attributes. add ("onclick", "radbutton ('" + E. row. rowindex. tostring () + "','" + E. row. cells [1]. text. trim () + "');");
// Radiobutton rad = (radiobutton) E. Row. cells [0]. findcontrol ("rad_select ");
// Rad. attributes. add ("onclick", "radbutton ('" + E. row. rowindex. tostring () + "','" + E. row. cells [1]. text. trim () + "');");
}
// The JS bound to the row
Function radbutton (rowindex, rowguid)
{
// Gv_infofromsendmodule $ ctl02 $ rad_select
VaR rowcount = parseint(document.all.txt hid_rowcount.value) + 2;
For (VAR I = 2; I <rowcount; I ++)
{
VaR tmpname;
If (I <10)
{
Tmpname = "gv_infofromsendmodule $ ctl0" + I + "$ rad_select ";
}
Else
{
Tmpname = "gv_infofromsendmodule $ CTL" + I + "$ rad_select ";
}
// Obtain the corresponding radio object
VaR tmpradio = Document. getelementbyid (tmpname );
// Select other items to cancel the selection
If (I-2) = rowindex)
{
Tmpradio. Checked = true;
}
Else
{
Tmpradio. Checked = false;
}
}
Document.all.txt hid_guid.value = rowguid;
}
12
// Remove leading and trailing Spaces
Function fn_trim (OBJ)
{
If (OBJ = NULL)
{
Return;
}
Else
{
VaR oldstr = obj. value;
VaR newstr = oldstr. Replace (/^ \ s + | \ s + $/g ,"");
OBJ. value = newstr;
}
}
13
// determines whether the textbox text content length is exceeded. True is returned.
function fn_istoolong (OBJ, varlength)
{< br> If (OBJ = NULL)
{< br> return false;
}< br> else
{< br> var valuestr = obj. value;
var Len = valuestr. match (/[^-~] /G) = NULL? Valuestr. Length: valuestr. Length + valuestr. Match (/[^-~] /G). length;
If (LEN> parseint (varlength ))
{
Return true;
}
Else
{
Return false;
}
}
}