Results 1 to 3 of 3

Thread: [RESOLVED] handle fire event when row clicked in a datagrid

  1. #1

    Thread Starter
    Fanatic Member VBKNIGHT's Avatar
    Join Date
    Oct 2000
    Location
    Port25
    Posts
    619

    Resolved [RESOLVED] handle fire event when row clicked in a datagrid

    i want to edit the selected value in another page by click the row in the datagrid.

    how can i pass the value? how can i call the other page?

    i have this dgrid:
    <asp:datagrid id="clientGrid" runat="server" cellPadding="5" PageSize="20" AutoGenerateColumns="False"
    AllowSorting="True" Width="576px" AllowPaging="True" Height="144px">
    <SelectedItemStyle Font-Size="XX-Small" Font-Names="Tahoma" Font-Bold="True" BackColor="WhiteSmoke"></SelectedItemStyle>
    <AlternatingItemStyle Font-Size="XX-Small" Font-Names="Tahoma" BackColor="WhiteSmoke"></AlternatingItemStyle>
    <ItemStyle Font-Size="XX-Small" Font-Names="Tahoma"></ItemStyle>
    <HeaderStyle Font-Size="XX-Small" Font-Names="Arial" HorizontalAlign="Center"></HeaderStyle>
    <Columns>
    <asp:TemplateColumn>
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    <ItemTemplate>
    <asp:linkbutton id="cmdView" runat="server">View</asp:linkbutton>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    <ItemTemplate>
    <asp:linkbutton id="cmdEdit" runat="server">Edit</asp:linkbutton>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="CLIE_CODE" ReadOnly="True" HeaderText="CODE"></asp:BoundColumn>
    <asp:BoundColumn DataField="CLIE_NAME" HeaderText="CLIENT NAME"></asp:BoundColumn>
    <asp:BoundColumn DataField="CLIE_SENTBY" HeaderText="SENTBY"></asp:BoundColumn>
    <asp:BoundColumn DataField="CLIE_ADDEDDATE" ReadOnly="True" HeaderText="DATE" DataFormatString="{0:d}"></asp:BoundColumn>
    <asp:TemplateColumn>
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    <ItemTemplate>
    <asp:linkbutton id="cmdDelete" runat="server">Delete</asp:linkbutton>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    <PagerStyle Font-Size="0.7em" Font-Names="Verdana" HorizontalAlign="Right" ForeColor="#FF0066"
    Position="TopAndBottom" PageButtonCount="11" Mode="NumericPages"></PagerStyle>
    </asp:datagrid>
    what i want is if i clicked the edit linkbutton i will get the row informatin and open the edit page for that id.

    TIA

    If a post has helped you then Please Rate it!

  2. #2
    Addicted Member
    Join Date
    Aug 2004
    Location
    Cape Town, South Africa
    Posts
    149

    Re: handle fire event when row clicked in a datagrid

    Rather than using item templates, use a buttoncolumn like this:

    Code:
    <asp:ButtonColumn ButtonType="LinkButton" Text="Edit"  CommandName="Select"></asp:ButtonColumn>
    When the user clicks on this you need to get the client that they clicked on. So what you need to do is to put this code in your <asp:datagrid> tag

    Code:
    OnItemCommand="viewRow" DataKeyField="CLIE_CODE"
    You must then define this 'viewRow' event in your page behind. Something like this...

    VB Code:
    1. Public Sub viewRow(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
    2.  
    3.     If Not e.Item.ItemIndex = -1 Then
    4.          'put code here
    5.          Dim ID as String = ctype(dgrGrid.DataKeys(e.Item.ItemIndex), string)
    6.    
    7.          'etc....
    8.     End If
    9.  
    10. End Sub

    dgrGrid.DataKeys(e.Item.ItemIndex) gives you the keyfield (CLIE_CODE) for the selected row that you specified in your <asp:datagrid> tag

    I think thats about right

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: handle fire event when row clicked in a datagrid

    Architecturally, you wouldn't want the server to have to post-back the form to take you to another page. The ID of the row (record what have you), should be in the link to the other page.

    Your goal would be to have the html output look like this...
    <a href="ItemEdit.aspx?id=123">Edit</a>

    An anchor link will make the client Get a request for ItemEdit.aspx with the id parameter filled in.

    A linkbutton has to post-back to the page that rendered the current response, wire everything up, then ascertain that the user clicked a button, fire your event handler, which would then initiate a response.redirect to ItemEdit.aspx. In a high-volume site, that would be just simple overkill.

    As you can easily see, the former method is much faster.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width