2

In button click event I am writing this code.(code behind).I know button click event

protected void button_click()
{         
    string s1 = "Computer"; 
    StringBuilder sb = new StringBuilder(); 
    sb.Append(@"<script language='javascript'>"); 
    sb.Append("document.write('<table><tbody>')";); 
   sb.Append("document.write('<tr><td>Information</td></tr>')";); 
    if(s1 == "Computer" ) 
   {
     sb.Append("<tr><td>s1</td></tr>"); 
   }
   sb.Append("document.write('</tbody></table>')";);
   sb.Append(@"</script>"); 

}

It is working but I want value of s1 not s1.I know javascript is a client side programing.I wrote this function in button click event.How can pass value of s1 that is computer to table's cell

4 Answers 4

4

simply

sb.Append("document.write('<tr><td>" + s1 + "</td></tr>');"); 
Sign up to request clarification or add additional context in comments.

Comments

3

Have you tried

    if(s1 == "Computer" )  
   { 
     sb.Append("<tr><td>"+s1+"</td></tr>"); 
   }

2 Comments

When I click on button,table go to top position.I want it at bottom below buttom.I know when we use Table control in cs file like Table tb = new Table(); ,placeholder.control.Add(tb) is used.But how add dynamic table at perticular position,in this case?
Thats different to your original question.. You would probably best use ajax to add it without submitting the page, or, using a webform, submit the page and use the new value.
3

You could change this line to:

sb.Append("<tr><td>" + s1 + "</td></tr>");

Comments

1

Your code is a bit irrelevant to the question you're asking. Your error has already been fixed by previous answers. But I will try to expand the asnwer further. If you wish to pass some value from C# to JavaScript, there're different methods.

1) You could make a protected method in a codebehind file and call it from your .aspx file, like

my.aspx

<script type="text/javascript">
var x = '<%= GetMyValue() %>';
DoFooWithX(x);
</script>

my.aspx.cs

protected string GetMyValue()
{
    return "example";
}

2) If you wish to execute some JavaScript once page finishes loading, you could also do it from your codebehind like this

my.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    string myScript = string.Format("myVar = '{0}';", GetMyVarValue());
    ScriptManager.RegisterStartupScript(this, this.GetType(), "MyVarSetter", myScript, true);
}
private string GetMyVarValue()
{
    return "example";
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.