?
Solved

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

Posted on 2006-06-23
2
Medium Priority
?
339 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

752 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