Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

XML
<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>


Note - My ascx page also uses AJAX.

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

XML
<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>



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?

C#
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!
Posted

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 .
 
Share this answer
 
Comments
sujanir 2-Nov-10 18:14pm    
I tried below code on Page_Load of web user control. This ran without errors but did not do anything.
I think AJAX interferes in running client side javascripts. May be i should try an AJAX popup control. i wonder how much functionality can be embedded in such a control?

ClientScriptManager cs = Page.ClientScript;

StringBuilder csText = new StringBuilder();
csText.Append("<script type=\"text/javascript\"> function DoClick() {");
csText.Append("alert('test'); } ");

cs.RegisterClientScriptBlock(typeof(Page), "script", csText.ToString());
hi,

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.

C#
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();");
    }
 
Share this answer
 
Comments
sujanir 2-Nov-10 23:33pm    
This works! Could you explain as to why it does not work in ItemCommand event but works in Databound event.
I can confirm by debuggging that the Item Command does fire when I click on the button, and it also finds the button control inside the formview.
Bryian Tan 4-Nov-10 3:03am    
The ItemCommand event occurs when a button within a FormView control is clicked. With that said, the JavaScript will not be added to the Button on page-load until someone click on the Button in the FormView.
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.
 
Share this answer
 
v2
Comments
sujanir 2-Nov-10 18:33pm    
Thanks aidin. I tried that but nothing happened. I tried below code on form load, I deliberately introduced an error in the javascript code. While .net was quick to spot errors in my javascript code, when I corrected the same and ran without errors, it ran fine but without executing the same.

StringBuilder csText = new StringBuilder();
csText.Append("<script type=\"text/javascript\"> function DoClick() {");
csText.Append("alert('test') ");

ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(typeof(Page), "script", csText.ToString());

button1.Attributes.Add("onclick", "javascript:DoClick();");
aidin Tajadod 2-Nov-10 19:53pm    
first, you forgot ";" after csText.Append("alert('test') ");
second, Could you please use literal instead of register. please let me know what happend.
sujanir 2-Nov-10 20:08pm    
1. I purposely omitted a ";" to find out if ASP.NET even processes the javascript. ASP.NET showed a javascript error & so it does process javascript. I have put back the semicolon and the result is the same - nothing happens.
2. I dont understand what you mean by using literal instead of register.

Thanks Aidin.
aidin Tajadod 2-Nov-10 23:47pm    
There is a control name literal. you can put in in your page like this
<literal runat="server" id="ltrMain"></literal>
then in your code:

ltrMain.text="<Script language=\"javascript\">";
ltrMain.text+="alert('ok');";
ltrMain.text+="</Script>";
Pls write the Javascript Function on the continer page of the ascx,thts means aspx page....(the back page of ascx)then run it will fire.......
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900