Solved

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now