Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select row in datagrid and pass a value

Status
Not open for further replies.

Ankor

Programmer
Mar 21, 2002
144
US
I would like to be able to select a row in the datagrid without having a select button. Then, when I click "Edit" button on the form, one of the values from this row should be pulled into the querystring on another page created for editing.

I understand I should somehow mimic javascript:__doPostBack for onclick event, but I am not sure how.

Thank you.
 
Hi Ankor.
I don't think I can answer your question, but I might be able to get you looking in the right direction. I think what you need to do is implement the OnEditCommand (or something like that) event handler. It will take the place of the default edit button, or should, and you should then be able to pass whatever you're looking for in a query string. This is how the old datagrid would have to handle events. You can do the same for custom sorting routines, delete routines, cancel routines, just about anything. Like I said, I'm not sure how to implement this exactly (I just use the default stuff) but maybe this will help a little. Good luck!
 
Hi Alan6895,
That's my problem. I don't know what I am doing wrong in the routine. I was trying to put code for ItemCreated:

Private Sub dgList_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgList.ItemCreated
If (e.Item.ItemType = ListItemType.AlternatingItem) Then
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#FFC080';this.style.cursor='hand'")
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#C0C0FF';")
e.Item.Attributes.Add("onclick", "javascript:__doPostBack('dgList$_ctl" & ((Convert.ToInt32(e.Item.ItemIndex)) + 3) & "$_ctl0','')")
End If
If (e.Item.ItemType = ListItemType.Item) Then
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#FFC080';this.style.cursor='pointer'")
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF';")
e.Item.Attributes.Add("onclick", "javascript:__doPostBack('dgList$_ctl" & ((Convert.ToInt32(e.Item.ItemIndex)) + 3) & "$_ctl0','')")
End If
End Sub

Onmouseover and onmouseout work, but not onclick.
 
Why are you using the doPostBack? OnItemCrated why don't you create a Hyperlink that has the information you need in the navigate url?

 
Because I want to be able to select a row without moving to the edit page. I might have multiple functions: edit or delete selected row.
 
So make a link for deleting the row, which wont move a user off the page, then create a link for Edit, which will move the user to the edit page? You can make as many hyperlink columns as you want or need.
 
Ok I misread what you're saying, the doPostBack code you have hurt my eyes.

You can apply a CSS style to a row that is selected.

Here is a decent link, you need to dig into the article a little ...
But add an onclick attribute, leave the doPostback alone, and write a little js function to mess with your CSS for the row :) (sample link
HTH better.
 
i just have to put in my 2 cents

as long as you dont set the Text="" attribute, the linkbutton wont display, so put it anywhere in an item template.

all you have to do is extend the CommandName to the datagrid. Edit, Delete, Select are reserved for the datagrid. You can use your own custom event with the CommandArgument and OnCommand, but not needed here.

then do what you need to do with the OnEditCommand and OnDeleteCommand events as usual.

Code:
    <asp:DataGrid ID="dgGrid" runat="server" OnEditCommand="dgEdit_Action" OnDeleteCommand="dgDelete_Action"
        OnItemDataBound="dgIDB_Event" DataKeyField="KeyID">
        <Columns>
            <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:LinkButton ID="lbEdit" runat="server" CommandName="Edit" />
                    <asp:LinkButton ID="lbDelete" runat="server" CommandName="Delete" OnClientClick="return confirm('DELETING RECORD: Are You Sure?');" />
                </ItemTemplate>
            </asp:TemplateColumn>
        </Columns>
    </asp:DataGrid>


Code:
    Sub dgIDB_Event(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            e.Item.Attributes.Add("onmouseover", "this.className='dgItemHover';")
            e.Item.Attributes("onclick") = Page.GetPostBackClientHyperlink(CType(e.Item.FindControl("lbEdit"), LinkButton), "")
            e.Item.Attributes("oncontextmenu") = Page.GetPostBackClientHyperlink(CType(e.Item.FindControl("lbDelete"), LinkButton), "")
        End If
        If e.Item.ItemType = ListItemType.Item Then
            e.Item.Attributes.Add("onmouseout", "this.className='dgItem';")
        ElseIf e.Item.ItemType = ListItemType.AlternatingItem Then
            e.Item.Attributes.Add("onmouseout", "this.className='dgAltItem';")
        End If
    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top