Solved

how to get value from gridview when user clicks a link within the gridview

Posted on 2006-06-23
2
337 Views
Last Modified: 2008-01-09
My GridView:
      name      office
delete      me      local
delete      you      remote


aspx:
<asp:GridView ID="GridViewUsers" runat="server" BorderColor="Silver" BorderStyle="Solid" BorderWidth="1px"
        DataKeyNames="email" OnRowDeleting="GridViewUsers_RowDeleting" >
            <HeaderStyle BackColor="#E0E0E0" Font-Bold="True" />
            <AlternatingRowStyle BackColor="#F8F8F8" BorderStyle="Solid" />
            <Columns>
                <asp:TemplateField HeaderText="Remove">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("Email") %>' CommandName="Delete"
                        runat="server">Delete</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
</asp:GridView>


code behind:
protected void GridViewUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
       string UserEmail = GridViewUsers.DataKeys[e.RowIndex].Value.ToString();
       string UserOffice = ""; //*** how do I get the office from the gridview for the selected row? ***
       DeleteRecordByID(UserEmail, UserOffice);
    }
protected void DeleteRecordByID(string EMail)
    {
        //delete from list
        String strConnection = ConfigurationManager.ConnectionStrings["MyConnection"].ToString();
        String strSQL = "Delete from table where email ='" + EMail + "'";
        SqlConnection objConnection = new SqlConnection(strConnection);
        SqlCommand cmd = new SqlCommand(strSQL, objConnection);
        objConnection.Open();
        cmd.ExecuteNonQuery();
        objConnection.Close();
        //reload page;
    }


How do I get the office value from a given row when the user clicks the delete link?
0
Comment
Question by:fwsteal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Expert Comment

by:spfeedback
ID: 16971231
add a hidden label to the itemtemplate and fill it with the office value. Like so
<asp:Label id="lblOffice" visible="False"
                    text=<%# DataBinder.Eval(Container.DataItem,"office")%>
                    runat="server" />
then in the delete function you would use something like:
   Dim office As String = CType(e.Item.FindControl("lblOffice"), Label).Text

0
 
LVL 4

Accepted Solution

by:
gregg1ep00 earned 500 total points
ID: 16971318
Change your grid definition to:

<asp:GridView ID="GridViewUsers" runat="server" BorderColor="Silver" BorderStyle="Solid" BorderWidth="1px"
        DataKeyNames="email,office" OnRowDeleting="GridViewUsers_RowDeleting" >

Then, in your RowDeleting handler:
       string UserEmail = GridViewUsers.DataKeys[e.RowIndex].Values[0].ToString();
       string UserOffice = GridViewUsers.DataKeys[e.RowIndex].Values[1].ToString();

Hope that helps!
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net mvc5 6 30
hbo knew my windows software 4 76
ASP.NET (VB) return a record 2 52
vb.net, web page, SQL holding the selected data in an accessible variable 7 34
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question