Solved

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

Posted on 2006-06-23
2
338 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

690 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