Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My javscript are not working.i am using a javascript for open a new pop window for edit a records,but window are open but not contains ID values.my question is how use javascript for get imagebutton commandname value?
ASP.NET
<asp:gridview><asp:ImageButton ID="Edit" ImageUrl="~/Pages/Images/Edit_new.jpg" runat="server" CommandName='<%# Container.DataItem("ID") %>'  text='<%# Container.DataItem("ID") %>' OnClientClick ="return openph(this)"  /></asp:gridview>
VB
If Not IsPostBack Then
           Session.Add("CnString", ConfigurationManager.AppSettings("CnString"))

           If Not Request.QueryString("ID") = "" Then
               ViewState.Add("ID", Request.QueryString("ID"))
               'LoadRecord(Request.QueryString("ID"))
               ViewState.Add("RoleID", Session.Item("RoleID"))
               'LoadstateInfo(Request.QueryString("ID"))

           End If
       End If
JavaScript
<script type="text/javascript" language="JavaScript">
function openph(str)
{
var img=document .getElementById ("Edit")


var url1 = "New Employee.aspx?ResponseType=Edit&Id=" + img ;
window.open(url1, "test","top=0,left=0,menubar=no,toolbar=no,location=no, resizable=no,height=550,width=440,status=no,scrollbars=no,minimizable=no,maxmizable=no,resizable=0,titlebar=no;");
if ( window .focus ())
{
url1 .focus()
}
}
</script>

advance thanks
Posted
Updated 10-Jan-12 22:36pm
v2

I think u need this:
----// means the code is updated

XML
<script type="text/javascript" language="JavaScript">
function openph(str)
{
----//var img=document .getElementById ("Edit")
var imgCommand=document.getElementById ("Edit").getAttribute("CommandName")


---//var url1 = "New Employee.aspx?ResponseType=Edit&Id=" + img ;
var url1 = "New Employee.aspx?ResponseType=Edit&Id=" + imgCommand 

var newwin = window.open(url1, 
-------//window.open(url1, "test","top=0,left=0,menubar=no,toolbar=no,location=no, resizable=no,height=550,width=440,status=no,scrollbars=no,minimizable=no,maxmizable=no,resizable=0,titlebar=no;");

if ( newwin.focus )
------//if ( window .focus ())
{
---//url1 .focus()
newwin.focus()
}
}
</script>
 
Share this answer
 
Comments
Tejas Vaishnav 11-Jan-12 4:08am    
before you provide solution please check it on your local system that it will work of not....

your solution seems not working....
Jamal Seyedi 11-Jan-12 4:36am    
do u think helping to u is my duty?
of course not instead saying thanks u expect me to waste my time for u.
where are u from?
Jamal Seyedi 11-Jan-12 4:38am    
I will remember no to help to Indian people.
Vasim889 11-Jan-12 4:51am    
cool.now i am workout all of codings, i take lot of time,so i forget tosay thanks(R)comment,k and then u solution is not working. kindly thanks.
You could try something like this:


ASP.NET
<asp:gridview xmlns:asp="#unknown"><asp:imagebutton id="Edit" imageurl="~/Pages/Images/Edit_new.jpg" runat="server" commandname="<%# Container.DataItem("ID") %>" onclientclick="return openph(<%# Container.DataItem("ID") %>)" /></asp:gridview>


JavaScript
<script type="text/javascript" language="JavaScript">
function openph(commandName) {
    var url1 = "New Employee.aspx?ResponseType=Edit&Id=" + commandName;
    window.open(url1, "test","top=0,left=0,menubar=no,toolbar=no,location=no, resizable=no,height=550,width=440,status=no,scrollbars=no,minimizable=no,maxmizable=no,resizable=0,titlebar=no;");
    if (window .focus()) {
        url1.focus()
    }
}
</script>
 
Share this answer
 
You Need to Change your Javascript Like This...

one more thing in url1 you write "new Employee.aspx....."
here if you have that page placed in side new folder then wirte like this

new/Employee.aspx.... or if you write it for new window then no need to write it just use my provided js function to call your edit method...

JavaScript
 <script type="text/javascript" language="JavaScript">
function openph(img) {
    var url1 = "Employee.aspx?ResponseType=Edit&Id=" + img.text;
    newWindow = window.open(url1, "test", this.target, "top=0,left=0,menubar=no,toolbar=no,location=no, resizable=no,height=550,width=440,status=no,scrollbars=no,minimizable=no,maxmizable=no,resizable=0,titlebar=no;");
    if (newWindow.focus()) {
       newWindow.focus()
    }
}


and you need to change your design code like this...

ASP.NET
<asp:imagebutton id="Edit" imageurl="~/edit.png" width="10px" height="10px" runat="server" commandname="<%# Eval("ID") %>" text="<%# Eval("ID") %>" xmlns:asp="#unknown">
OnClientClick="return openph(this)" />
</asp:imagebutton>


if you pass your image button using this then no need to do document.getElementByID and also you do it in wrong way, because when you need to get the element decalred as a item template inside grid or any other control at that time you can not get that element directly...

here you pass this keywork while calling your edit method so you can get that control directly to your caller function so use that...
 
Share this answer
 
Comments
Vasim889 11-Jan-12 4:29am    
thanks.It's not working?how to get gridview ->item template-> imagebutton-> commandname-> value in javascript,pls give me solution

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