Link to home
Get AccessLog in
Avatar of awilderbeast
awilderbeastFlag for United Kingdom of Great Britain and Northern Ireland

asked on

c# running a javascript and a codebehind function on same button?

Hi all,
not sure if this is possible becuase a .net button on click has a value of javascript:dopostback_ etc


but basically, i want a code behind function to run then a javascript one i can make it do one or the other and at the moment the code is only doing the javascript one and ignoring the c# one

any ideas on how i can do this?
############################## aspx page
<script>
        $(document).ready(function () {
            $(".dialog-modal-history").dialog({
                height: 450,
                width: 700,
                autoOpen: false,
                modal: true,
                dialogClass: 'Dialog',
                title: 'Work History'
            });

            $('.butHistoryDialog').click(function () {
                $('.dialog-modal-history').dialog('open');
                // prevent the default action, e.g., following a link
                return false;
            });

            $('.dialogclose').click(function () {
                $('.dialog-modal-history').dialog('close');
                // prevent the default action, e.g., following a link
                return false;
            });
        });
        function HistoryClick() {
            dialog = document.getElementById("butHistoryDialog");
            dialog.click();
        }
    </script>
<asp:Repeater runat="server" ID="rptJobHistory" OnItemDataBound="rptJobHistory_OnItemDataBound">
                <ItemTemplate>
                    <tr class="<%# Container.ItemIndex % 2 == 0 ? "Odd" : "Even" %>">
                        <td>
                            <%# Eval("DateStart", "{0:MMM yyyy}")%>
                        </td>
                        <td>
                            <%# ifdatenotend(Eval("DateEnd", "{0:MMM yyyy}"))%>
                        </td>
                        <td>
                            <%# Eval("Title") %>
                        </td>
                        <td>
                            <%# Eval("Company") %>
                        </td>
                        <td>
                            
                            <asp:LinkButton ID="View" runat="server" CommandArgument=<%# Eval("ID") %>>Edit</asp:LinkButton>
                        </td>
                        <td>
                            <asp:LinkButton runat="server" OnClick="Delete" CommandArgument=<%# Eval("ID") %>>Delete</asp:LinkButton>
                        </td>

                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </table></td>
                <td><asp:Button ID="butAddJobHistory" runat="server" Text="Add History" SkinID="MessageButton" CssClass="opener" />
                <input runat="server" ID="butHistoryDialog" type="button" class="butHistoryDialog" clientidmode="Static" /></td>
            </tr>
        </table>

####################### code behind

    protected void rptJobHistory_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    { 
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            LinkButton ViewHistory = e.Item.FindControl("View") as LinkButton;

            ViewHistory.Click += new EventHandler(History);
            smUpdate.RegisterAsyncPostBackControl(ViewHistory);
            ViewHistory.Attributes.Add("onclick", "javascript:HistoryClick();");
        }
    }

Open in new window

Avatar of dj_alik
dj_alik

OnClientClick event handler we can write your javascript right in the line and when the user thinks
the POST action will be canceled by returning false.
Avatar of awilderbeast

ASKER

i did use onclientclick and it executed the wrong way round for my scenario, it loaded the dialog then iflled it (or atleast seemed to)

im not sure my updatepanel is functionign correctly

can i use onclick and onclientclick together then?
Avatar of Tom Beck
You could register a client script block that either calls a function on the page or you could write out the javascript function in code behind and paste the whole thing into csText1 in the following example. You could place it at the end of OnItemDataBound, however, you will want to check if the script is already registered because OnItemDataBound runs repeatedly, once for each item in the repeater.    

ClientScriptManager cs = Page.ClientScript;

    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
    {
      String cstext1 = "alert('Hello World');";
      cs.RegisterStartupScript(cstype, csname1, cstext1);
    }
the below just runs the javascript function, the c# function isnt run at all

not sure i follow you tommyboy never seen this method before can you show me an example?

Thanks
protected void rptJobHistory_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            LinkButton ViewHistory = e.Item.FindControl("View") as LinkButton;
            ViewHistory.Click += new EventHandler(LoadHistory);
            ViewHistory.OnClientClick = "javascript:HistoryClick();";
            smUpdate.RegisterAsyncPostBackControl(ViewHistory);

        }
    }

Open in new window

check the rendered HTML for existing of "onclick="javascript:HistoryClick();"
What event triggers the code behind function to run, the butAddJobHistory button click? Then in the click handler for that button, do what you need to do in C# then use the RegisterStartUpScript function to print a javascript call to the response. When the page reloads on the client end the javascript will be called. What javascript function are you wanting to call, the HistoryClick()? Then in the button click handler end with:

    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
    {
      String cstext1 = "HistoryClick();";
      cs.RegisterStartupScript(cstype, csname1, cstext1);
    }
Working sample of RegisterClientScriptBlock.
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace testApp1
{
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientScriptManager cs = Page.ClientScript;

            // Check to see if the startup script is already registered.
            if (!cs.IsStartupScriptRegistered(this.GetType(), "test"))
            {
                String cstext1 = "HistoryClick();";
                cs.RegisterStartupScript(this.GetType(), "test", cstext1, true);
            }

        }
    }
}

Open in new window

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default2.aspx.cs" Inherits="testApp1.Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript">
    function HistoryClick() {
            alert("HistoryClick is running");
            dialog = document.getElementById("butHistoryDialog");
            dialog.click();
        }
    </script>
</head>
<body>
<form id="form1" runat="server">
<div id="container"></div>
</form> 
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of awilderbeast
awilderbeast
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
had to use alternate method