将Javascript添加到ascx页面 [英] Adding Javascript to ascx page

查看:66
本文介绍了将Javascript添加到ascx页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我单击Web用户控件上的formview内的按钮时,我试图打开一个新窗口.

我的ascx页面中有此代码.

Hi,

Iam trying to open a new window when a button present inside a formview on my web user control is clicked.

I have this code in my ascx page.

<script language="javascript">
                function OpenFiles()
                {
                  alert('open files');
                    var WinSettings = "center:yes;resizable:no;dialogHeight:300px"
                    // supply correct URL for Child Form
                    var MyArgs = window.showModalDialog("http://localhost/WebSite1/UI/ATSMain.aspx", MyArgs, WinSettings);


                }
</script>



注意-我的ascx页面也使用AJAX.

在formview的项目模板中,我有一个按钮.



Note - My ascx page also uses AJAX.

Inside my formview''s item template I have a button.

<asp:FormView runat="server" ID="fvATSDetails" OnPreRender="fvATSDetails_PreRender" DataKeyNames="ApplicationID,ApplicantId"

                            DataSourceID="sqldataSource_ATSDetails"

                            OnItemCommand="fvATSDetails_ItemCommand">

        <ItemTemplate>

            <table style="width: 697px; font-family: Arial, Helvetica, sans-serif; font-size: x-small;">

            <tr>
                <td>
                    <asp:Button ID="btnEdit" runat="server" CommandName="Edit"

                         Text="Edit" Width="138px" />
                    &nbsp;<asp:Button ID="btnDecline" runat="server" CommandName="Decline"

                        Text="Decline Application" />
                    &nbsp;<asp:Button ID="btnDoc" runat="server" CommandName="Documents"

                        Text="Manage Applicant's Documents" Width="194px" />
                </td>
            </tr>
            </table>
        </ItemTemplate>

    </asp:FormView>




在FormView的项目命令中,单击按钮时添加了javascript属性.添加此代码时,我希望打开一个新窗口或至少使用javascript函数显示警报消息.但是什么也没发生.谁能看到我做错了什么?




In the FormView''s Item Command, I add javascript attributes when the button is clicked. When I add this code, I expect a new window to open or atleast the javascript function to show the alert message. But nothing happens. Can anyone see what Iam doing wrong?

protected void fvATSDetails_ItemCommand(object sender, FormViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "Documents":
                {
                    Button btnDoc = (Button) fvATSDetails.FindControl("btnDoc");
                    btnDoc.Attributes.Add("onclick", "javascript:openFiles();");
                }
                break;
        }

    }




谢谢!




Thanks!

推荐答案

尝试注册脚本而不是attribute.add,

您可以在页面状态中看到错误吗


例子

page.registerscriptblock(",",",");

由于您使用的是ajax脚本管理器,因此无法处理javascript,因此,如果您注册该脚本,则该脚本应运行.
try to register the script instead of attributes.add ,

can you see error on your page status


example

page.registerscriptblock("","","","");

since you are using ajax script manager the javascript is not being handled so if you register the script it should run .


hi,

如果您单击两次按钮,JavaScript将被调用.如果您对其进行调试,则会注意到fvATSDetails_ItemCommand方法不会在页面加载时执行.方法触发的唯一时间是单击按钮时.如果要在页面加载/呈现时将JavaScript添加到Button.尝试通过DataBound事件将其绑定.



The JavaScript will get call if you click on the button twice. If you debug it, you will notice that the fvATSDetails_ItemCommand method will not be executed on page load. The only time the method get trigger is when you click on the button. If you want the to add the JavaScript to the Button on page-load/render. Try bind it through the DataBound event.

protected void Page_Load(object sender, EventArgs e)
    {
        fvATSDetails.DataBound += new EventHandler(fvATSDetails_DataBound);
    }
    void fvATSDetails_DataBound(object sender, EventArgs e)
    {
        Button btnDoc = (Button)fvATSDetails.FindControl("btnDoc");
        btnDoc.Attributes.Add("onclick", "javascript:OpenFiles();");
    }




可能是因为错误.
你在做:

btnDoc.Attributes.Add("onclick","javascript:openFiles();");

但是您函数的名称是OpenFiles!因此将您的代码更改为

btnDoc.Attributes.Add("onclick","javascript:OpenFiles();");

(大写o)

希望对您有所帮助.
Hi,

It might be because of error.
you are doing :

btnDoc.Attributes.Add("onclick", "javascript:openFiles();");

but the name of your function is OpenFiles !! so change your code to

btnDoc.Attributes.Add("onclick", "javascript:OpenFiles();");

(upper case o)

hope this can help.


这篇关于将Javascript添加到ascx页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆