Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

  • Hello, I want to insert HTML Code inside a <div id="div1"
  • I want to search for that div, by its ID
  • It was something like element.innerhtml ...... forgot
  • better if u give me a solution w\ Javascript

Development & Research Department @ Elven Soft
Posted

Try this:

<html>
   <head>
   <title>some title</title>
   <script type="text/jscript">
   function setDivCode(myCode)
   {
    
     var myDiv = document.getElementById('div1');
     myDiv.innerHTML += myCode;
   }
   </script>

   </head>
   <body>
       <input type="text" id="txt" value="<b>here is  my code</b>"/>
       <input type="button" id="btn" onclick="setDivCode(txt.value);" />
       <div id="div1">
           <a href="http://www.google.com" id="lnk">google</a>
       </div>
   </body>
</html>

Posted

Ok, This is a start.

But I'm kinda nubish with JavaScript.

 

What I need is something to work with from asp.net, in the .vb code behinde file. That's because I work with sql and I have to insert in that div data from SQL.

Development & Research Department @ Elven Soft
Posted

OK I found out how and I'll post it here for others searching for the same thing:

 

The trick is to put in the html code runat="server" for the div element:

<div id="div1" runat="server">

</div>

 

then in ASP.NET use

div1.InnerHtml

to insert Html.

 

Now I wonder if there's a way to delete a div from the html. (not only to set its InnerHtml to nothing)

Development & Research Department @ Elven Soft
Posted

So, if you add that div from Codebehind you'll have no problem to remove it.

 

This example will show you how to add DIV to TableCell (You can add it to whatever you want ) and how to remove it.

 

aspx code:

 

....
<table>
<tr>
<td id="myTD" runat="server">
</td>
</tr>
</table>
....

aspx.cs code:

 

   HtmlGenericControl div2 = new HtmlGenericControl("div");//DIV declaration
   protected void Page_Load(object sender, EventArgs e)
   {
       div2.ID = "div2";
       HtmlButton btn = new HtmlButton(); //put some DIV controls just to see if it's there
       btn.ID = "btn1";
       div2.Controls.Add(btn);
       myTD.Controls.Add(div2);// Add it to the TableCell
   }
   protected void Button1_Click(object sender, EventArgs e)
   {
       //Here is, remove the DIV.  
       myTD.Controls.Remove(div2);
   }
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...