Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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>
 
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>
 

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!
Posted 2-Nov-10 12:25pm
sujanir749
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 .
  Permalink  
Comments
sujanir at 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'); } csText.Append("script>");
 
cs.RegisterClientScriptBlock(typeof(Page), "script", csText.ToString());
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
 
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();");
    }
  Permalink  
Comments
sujanir at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2
Comments
sujanir at 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') csText.Append("script>");
 
ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(typeof(Page), "script", csText.ToString());
 
button1.Attributes.Add("onclick", "javascript:DoClick();");
aidin Tajadod at 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 at 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 at 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>";
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.......
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 539
1 Maciej Los 300
2 DamithSL 233
3 Sergey Alexandrovich Kryukov 209
4 BillWoodruff 200
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 3 Nov 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100