Link to home
Start Free TrialLog in
Avatar of monachan
monachan

asked on

C#.net--Ajax-- how do i get the value of a bound filed which is inside an update panel ---from a javscript fuction call.

i am having an AJax update panel control in which a gridview is placed.when the form loaded the gridview is binded with values from database.

the first column of the grid view is a <asp:LinkButton for delete.

other columns are  <asp:BoundField with values such as EMailAddress,GroupId etc.

when user click on 'delete' link a java script popup should come for delete confirmation.

i have added a 'OnClientClick="confirmSubmit()"  in the <asp:LinkButton which brings the javascript popup.........cool!!!!!!!!!

now the problem is i need to get the EMailId from the form  inside the JavaScript function so that i can say

"are sure you want to delete this" + EmailId

but i don't know how can i get the value of the EId from form .
pls give me some leads.
<asp:UpdatePanel ID="updPnlDisplayLocalAddresses" runat="server" UpdateMode="Conditional">
                                                            <ContentTemplate>
                                                                <asp:GridView ID="gvLocalAddresses" runat="server" Width="403px" AutoGenerateColumns="False"
 
                                                                            <asp:TemplateField ControlStyle-Width="150px" HeaderStyle-Width="60px" HeaderText="Address">
 
                                                                            <asp:TemplateField ControlStyle-Width="50px" HeaderStyle-Width="60px" HeaderText="">
                                                                                <ItemTemplate>
                                                                                    <asp:LinkButton ID="btnConfirmDeleteDetails" SkinID="BlueMagentaButtonSkinSmall2" runat="server"
                                                                                        Text="Delete"  OnClientClick="confirmSubmit()" />
                                                                                </ItemTemplate>
                                                                            </asp:TemplateField>
 
                                                                                <ItemTemplate>
                                                                                    <asp:LinkButton ID="btnViewDetails" SkinID="BlueMagentaButtonSkinSmall2" runat="server"
                                                                                        Text='<%#Eval("emailaddress")%>' OnClick="btnSelect_Click" />
                                                                                </ItemTemplate>
                                                                            </asp:TemplateField>
                                                                       
 
                                                                            <asp:BoundField HeaderText="Group Id" DataField="groupid" ItemStyle-HorizontalAlign="left"  />
                                                       </asp:UpdatePanel>
 
 
<script language="javascript" type="text/javascript">
     function confirmSubmit()
     {
 
''need to get the EMailAddress from fom here . 
      var agree=confirm("Are you sure you wish to delete this LocalAddress "+EMail Address from form...);
 
      if (agree)
 
       return true ;
 
      else
 
       return false ;
   }
</script>

Open in new window

Avatar of David Robitaille
David Robitaille
Flag of Canada image

here how i did it in a previous project.
1 replace your BoundField with template field that have id
2 link the function in the LinkButton  using the new feilds is client id (in my case, i use
onfocus instead of onclient click):

<asp:LinkButton ID="btnConfirmDeleteDetails" SkinID="BlueMagentaButtonSkinSmall2" runat="server"
                                                                                        Text="Delete"  OnClientClick='<%# "confirmSubmit(""" & EMailAddress.clientid & """)%>'
/>
3 add a parameter to your jascript function tu retrive the control :

function OnClientClick(EMailAddressID){
var EMailAddressIDObject = document.getElementById(EMailAddressID);
...

 

 
Avatar of monachan
monachan

ASKER

can pls explain this with code details.

i am unable to follow the first 2 points.

thanks you
the point 1 is about replacing the BoundField with Templatefield using labels and textbox. that way, those will have "controlID" that will be retrievable from asp.net.
the point 2 is about filling the parameter of the onclientclick function with a string. that string is build using the clientid (the name of the control on the client side)  of the new controls created in 1. Since the controls are within a GridView, they will have different names from a row to an other. That way the java script will receive the name of the control to find as a string.
sir,

infact this is the code i am using to display a delete link in the first column & the EmailId as a link in the second column.i think i should get the value in javascript using
      var myTextField = document.getElementById("btnViewDetails");

but i am not .can pls look into this code.


                                                                            <asp:TemplateField ControlStyle-Width="50px" HeaderStyle-Width="60px" HeaderText="">
                                                                                <ItemTemplate>
                                                                                    <asp:LinkButton ID="btnConfirmDelete" SkinID="BlueMagentaButtonSkinSmall2" runat="server"
                                                                                        Text="Delete"  OnClientClick="return confirmSubmit()" OnClick="btnConfirmDelete_Click" />
                                                                                </ItemTemplate>
                                                                            </asp:TemplateField>


                     <asp:TemplateField ControlStyle-Width="150px" HeaderStyle-Width="60px" HeaderText="Address">
                                                                                <ItemTemplate>
                                                                                    <asp:LinkButton ID="btnViewDetails" SkinID="BlueMagentaButtonSkinSmall2" runat="server"
                                                                                        Text='<%#Eval("emailaddress")%>' OnClick="btnSelect_Click" />
                                                                                </ItemTemplate>
                                                                            </asp:TemplateField>


document.getElementById("btnViewDetails"); wont work because the id of the btnViewDetails will change form a row to an other (check the source). that why you shoul retrive it using clientID.
 

<asp:LinkButton ID="btnConfirmDeleteDetails" SkinID="BlueMagentaButtonSkinSmall2" runat="server"
             Text="Delete"  OnClientClick='<%# "confirmSubmit(""" & btnViewDetails.clientid & """)%>' 
/>

Open in new window

thanks for the code sample.looks like am clos to the sloution.


when  i try to use this line of code for the link button i am not able to complile .a wierd error 'NewLine in constant' is dilplayed.

i tryed different things adding and removing.the compile erroe occurs only whein i add the code

'<%# "confirmSubmit(""" & btnViewDetails.clientid & """)%>'  in the Linl.

if i pass a simple valued that is bening acceptd by Javascript

pls see/help
i dont know...i dont remember saw this...
but i just googled it and i found this.
http://support.microsoft.com/kb/827420
also check on experts-excange. i saw other question with this error.
You could do this:

Have a template column for the delete link:
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="lbdelete" runat="server" Text="Delete" CommandName="Delete" />
                    </ItemTemplate>
                </asp:TemplateField>

then put the following event handlers on the grid declaration:

<asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting" OnRowDataBound="GridView1_RowDataBound">

Then in the code-behind,

protected void GridView1_RowDeleting(object Sender, GridViewDeleteEventArgs e)
        {
            //code to delete row
        }
        protected void GridView1_RowDataBound(object Sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ((LinkButton)e.Row.FindControl("lbdelete")).OnClientClick = "return confirm('Are you sure you want to delete " + PUT_EMAILID_HERE + "?');";
            }
        }
i just think of it and, instead of passing the id (btnViewDetails.clientid ), you could pass the binded value direcly Eval("emailaddress")
 

<asp:LinkButton ID="btnConfirmDeleteDetails" SkinID="BlueMagentaButtonSkinSmall2" runat="server"
             Text="Delete"  OnClientClick='<%# "confirmSubmit(""" & Eval("emailaddress").tostring() & """)%>' 
/>

Open in new window

hi.,


the code compiles.but keep on throwing runtime error.......
failed to load the popup also.

in this code

protected void GridView1_RowDataBound(object Sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ((LinkButton)e.Row.FindControl("lbdelete")).OnClientClick = "return confirm('Are you sure you want to delete " + PUT_EMAILID_HERE + "?');";
            }
        }

i cannot comlie because 'emailaddress' is not existing in the current context.


ASKER CERTIFIED SOLUTION
Avatar of colonel720
colonel720
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for who gave the input .i was able to get it done,
this code worked.

                        ((LinkButton)e.Row.FindControl("LinkButton1")).OnClientClick = "return confirm('Are you sure you wish to delete the LocalAddress " + ((LinkButton)e.Row.FindControl("btnViewDetails")).Text + "?');";
thank you guys

colonel720:  &  davrob60:

thankyou very much

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Glad to help.

Why did we get a "B"
https://www.experts-exchange.com/help.jsp#hi403 
"an answer is worth an A, unless it doesn't resolve your issue"
sir,

infact i never  completely read the pointing system.so i was not aware about this;i am glad to give 'A' because it was a difficult qn.
but how do i change the grade........

the document says

'If you feel you have graded a solution incorrectly, click the Request Attention button and the Moderators will change it for you.'
i don't see any 'Request Attension' button.
it`s at the top of the page, just unter the question Code Snippet text and before the "view solution" button.
Thanks!