admin管理员组文章数量:1301512
I'm not able to understand what is the use of it.RegisterClientScriptBlock/RegisterStartupScript.
When we can directly write the JavaScript code in.js file then call it on the button
ex: 2
<script>
function ReqField1Validator() {
if (document.forms[0].txtField1.value == '') {
alert('TextBox cannot be empty')
return false
}
return true
}
</script>
btnPostback.Attributes.Add("onclick","return ReqField1Validator()");
What will be the use of RegisterClientScriptBlock/RegisterStartupScript?
protected void btnPostback_Click(object sender, EventArgs e) {
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(@"<script language='javascript'>");
sb.Append(@"var lbl = document.getElementById('lblDisplayDate');");
sb.Append(@"lbl.style.color='red';");
sb.Append(@"</script>");
if (!ClientScript.IsStartupScriptRegistered("JSScript")){
ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb.ToString());
}
}
Read few articles but they were not clear.
Any thoughts on this would be great.
I'm not able to understand what is the use of it.RegisterClientScriptBlock/RegisterStartupScript.
When we can directly write the JavaScript code in.js file then call it on the button
ex: 2
<script>
function ReqField1Validator() {
if (document.forms[0].txtField1.value == '') {
alert('TextBox cannot be empty')
return false
}
return true
}
</script>
btnPostback.Attributes.Add("onclick","return ReqField1Validator()");
What will be the use of RegisterClientScriptBlock/RegisterStartupScript?
protected void btnPostback_Click(object sender, EventArgs e) {
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(@"<script language='javascript'>");
sb.Append(@"var lbl = document.getElementById('lblDisplayDate');");
sb.Append(@"lbl.style.color='red';");
sb.Append(@"</script>");
if (!ClientScript.IsStartupScriptRegistered("JSScript")){
ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb.ToString());
}
}
Read few articles but they were not clear.
Any thoughts on this would be great.
Share edited Apr 7, 2020 at 12:56 Renan Araújo 3,64111 gold badges41 silver badges52 bronze badges asked Aug 5, 2011 at 4:29 KumeeKumee 2111 gold badge4 silver badges12 bronze badges2 Answers
Reset to default 4RegisterStartupScript
is used to register and execute javascript functions once the page finished loading, this javascript code executes before the onLoad event of page is raised. You can use it for multiple reasons, for example, you want to execute any particular javascript function depending on some variable value in your code behind.
On the other hand, RegisterClientScriptBlock
is used to add a script block on the top of your page, similarly it provides you a way to work with both client side code and server code in your code behind.
In time, you might e across situations in which you will need to call a function in JavaScript depending on some variants in your code behind.
A very mon example would be to handle exception messages and then display them in a fancy way using JavaScript.
You might want to catch an exception like this:
public void TrySomethingAwesome(){
try
{
//try to do something awesome
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(Page, typeof(Page), "showError",
string.Format("ShowError({0});", "Oops! Something went wrong :("), true);
}
}
And that way your JavaScript can take over and display the message in a fancy way :D
本文标签: cUse of RegisterClientScriptBlockRegisterStartupScript in aspnet 35Stack Overflow
版权声明:本文标题:c# - Use of RegisterClientScriptBlockRegisterStartupScript in asp.net 3.5 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741677313a2391959.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论