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
Solved

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

807 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