In the C # development process, it is unavoidable to write some JS, in fact, to do the backend development, itself is not good at writing JS, simply summed up, to facilitate their own and convenient for others to share. Oh ~ ~
1. Front Button Background Event
Copy Code code as follows:
<asp:button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "button"
onclientclick= "alert (' room-side verification, blocking submission to server Side"); return false; "/>
2. Registration Related events: Onblur,onclick,onchange
Copy Code code as follows:
This. TEXTBOX1.ATTRIBUTES.ADD ("onchange",
"Alert (' data is changed, check if input conforms to the rules ');"
3. Register related attributes:
Copy Code code as follows:
This. TEXTBOX1.ATTRIBUTES.ADD ("ReadOnly", "true");
4. Introduction of JS File
Foreground HTML page:
Copy Code code as follows:
<script type= "Text/javascript" src= "Jscript.js" language= "JavaScript" ></script>
<script type= "Text/javascript" language= "JavaScript" >
function Fn_name ()
{
Alert ("JS");
}
</script>
Background CS Page:
Copy Code code as follows:
This. RegisterClientScriptBlock ("Jsfile",
"<script type= ' text/javascript ' src= ' jscript.js ' language= ' JavaScript ' ></script>");
[Code]
5. Click on the button when the relevant field is not NULL judgment
[Code]
function Checkempty (txtobj,msgshow)
{
if (Txtobj.value = "")
{
alert (msgshow);
return false;
}
}
<asp:button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "button"
onclientclick= "Return Checkempty (TextBox1, ' TextBox1 cannot be empty ')"/>
6. Whether or not the corresponding textbox can be entered by chcekbox the point of choice
Copy Code code as follows:
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 value to modal window and get returned value
Copy Code code as follows:
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)
{
var arrayreturninfo = returninfo. Split ("@");
Document.all.drpID.value = arrayreturninfo[1];
Document.all.txtname.value= arrayreturninfo[2];
}
8. Popup JS Confirmation dialog box, and according to the confirmation result triggers the background related operation
Copy Code code as follows:
if (Confirm (' Confirm how? '))
{
Document.all.hidbtn_Submit.click ();
}
Else
{
Document.all.hidbtn_Cancel.click ();
}
HTML page related code:
Copy Code code as follows:
<input id= "Hidbtn_submit" type= "button" value= "Confirm modification"
Style= "Display:none;"
onserverclick= "Hidbtn_submit_serverclick"
runat= "Server"/>
9. Add the page to the shortcut key response, such as press F2 when the new button operation, etc.
Copy Code code as follows:
#region Add a page response to a shortcut key
String Strjs_shortkey = "<script language= ' javascript ' type= ' text/javascript ' >";
Strjs_shortkey + = "document.onkeydown=shortkeydown;";
Strjs_shortkey + + "function shortkeydown ()";
Strjs_shortkey + = "{";
New
if (this. ButtonCtl1.ImgBtn_AddFamily.Visible)
{
String btninsertcid = this. ButtonCtl1.ImgBtn_Insert.ClientID.Trim ();
f2-113
Strjs_shortkey + = "if (event.keycode== ' 113 ')";
Strjs_shortkey + = "{";
Strjs_shortkey + + document.all (' + Btninsertcid + '). Click (); ";
Strjs_shortkey + = "event.keycode= 0;";
Strjs_shortkey + = "Event.returnvalue = false;";
Strjs_shortkey + = "return false;";
Strjs_shortkey + = "}";
}
Modify
if (this. ButtonCtl1.ImgBtn_Edit.Visible)
{
String btneditcid = this. ButtonCtl1.ImgBtn_Edit.ClientID.Trim ();
f3-114
Strjs_shortkey + = "if (event.keycode== ' 114 ')";
Strjs_shortkey + = "{";
Strjs_shortkey + + document.all (' + Btneditcid + '). Click (); ";
Strjs_shortkey + = "event.keycode= 0;";
Strjs_shortkey + = "Event.returnvalue = false;";
Strjs_shortkey + = "return false;";
Strjs_shortkey + = "}";
}
Strjs_shortkey + = "}";
Registering events
Page.registerstartupscript ("Shortkey", Strjs_shortkey);
#endregion
10. Pop-up prompts for branch display
Copy Code code as follows:
Alert (' AAA \ r \ n bbb \ r \ n] CCC ');
If you are registering in the background. cs file
You need
Copy Code code as follows:
String stralertcontent = "AAA" + "\\r\\n";
Stralertcontent + + "BBB" + "\\r\\n";
11. When you click a row on the GridView, the RadioButton is selected at the beginning of the line, while the related values are saved in the hidden field
Copy Code code as follows:
Binding with a queried dataset
if (dt. Rows.Count > 0)
{
Binding
This.gv_InfoFromSendModule.DataSource = DT;
This.gv_InfoFromSendModule.DataBind ();
OK button display
This.btn_ok. Visible = true;
This.txthid_RowCount.Text = dt. Rows.Count.ToString ();
}
The 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 () + "');
}
JS that is bound on the line
function Radbutton (ROWINDEX,ROWGUID)
{
Gv_infofromsendmodule$ctl02$rad_select
var rowcount = parseint (document.all.txthid_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";
}
Get the corresponding Radio object
var Tmpradio = document.getElementById (tmpname);
Currently select another unchecked
if ((i-2) = = RowIndex)
{
Tmpradio.checked = true;
}
Else
{
tmpradio.checked = false;
}
}
Document.all.txthid_GUID.value = RowGUID;
}
12. Remove space before and after
Copy Code code as follows:
function Fn_trim (obj)
{
if (obj==null)
{
Return
}
Else
{
var oldstr = Obj.value;
var newstr = Oldstr.replace (/^\s+|\s+$/g, "");
Obj.value = Newstr;
}
}
13.TextBox text content length to see if the length exceeds the return true
Copy Code code as follows:
function Fn_istoolong (obj,varlength)
{
if (obj==null)
{
return false;
}
Else
{
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;
}
}
}