Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

asp.net - Can I call a code behind function from a div onclick event?

I have a bunch of divs on my page that are added at run time dynamically. When any of the dynamically added divs are clicked - All need to call the same function in my code behind. Each of the divs must pass it's own ID to the function. I can't use web methods as the function needs to identify which div was clicked and then show/hide/populate other controls on the page.

Cheers guys

Header Controls and stuff go here
    <div id="div_Footer" class="HoverEdit" title="Click To Edit" runat="server" onclick="EditDiv(div_Footer)">
        Footer Controls and stuff go here
    </div>

and then in the code behind :

Sub EditDiv(ID_ofDiv As String)

    'Do some stuff to the controls on the page
    'Swapping tabs, showing /hiding controls etc.

End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm not used to write VB code so my example is in C# but maybe it can help you get started. It's probably not the cleanest way to implement this, but i'll give it a try:

HTML

 <div id="div_Footer" class="HoverEdit" title="Click To Edit" runat="server" onclick="EditDiv(this)">
        Footer Controls and stuff go here
    </div>

Client

<script type="text/javascript">
function EditDiv(s,e){
var id = $(s).attr("id");
__doPostBack(id,id);
}
</script>

Server

private void Page_Load(object sender, EventArgs e)
{
   var arg = Request.Form["__EVENTTARGET"]; 'this will be empty on your first page request, but if the user click a div it will cause a postback to server, so this event will be fired again and will contain the div ID.

   if(arg != null)
   {
      string divID = (string)arg;
      'call your method with the argument.
   }
}

More information about this can be found here:

http://wiki.asp.net/page.aspx/1082/dopostback-function/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...