I know how to add controls to a datagrid using the asp:templatecolumn/itemtemplate, but I am unsure on how to do anything with it from there. I don’t have OnCommand, CommandName, or CommandArgument available to the drop down list so I don’t know how to identify which row was selected or what action to take.
For each row in the datagrid, I’d like a drop down list with options to Remove, Archive, Print, etc… Normally, I use buttons and set the CommandName to what the button does and the CommandArgument to the record id for that row. And then in the OnCommand event handler I place a switch statement based on the command name.
Nevermind, I think I may have just gotten it. I can use the OnSelectedIndexChanged event handler in place of the OnCommand, the item value property in place of CommandArgument, and the item text property in place of the CommandName.
The only downside is I don’t know how I would reset the selected property back to the orginal without re-populating the datagrid.
I thought I’d reply in case it wasn’t realized that there was still a question here: how can I reset the dropdownlist in a datagrid without re-populating the list?
Also Marcie Robillard (AKA Datagrid Girl!) has gathered together a huge list of articles/links that are dedicated to the DataGrid. http://www.datagridgirl.com/default.aspx
I will look at the above links, but here some code from what I am trying to do. I am not working in the edit item template, though.
Sub ddlAction_OnChange(sender As Object, e As System.EventArgs)
'action based on drop down choice and record id
End Sub
'inside datagrid
<asp:templatecolumn>
<itemtemplate>
<div align="right">
<asp:dropdownlist ID="ddlAction" runat="server" CssClass="dropdownlist" OnSelectedIndexChanged="ddlAction_OnChange" AutoPostBack="true">
<asp:listitem Text="select an option" Value="0" />
<asp:listitem Text="[modify]" />
<asp:listitem Text="[delete]" />
<asp:listitem Text="[view]" />
<asp:listitem Text="[add problem/issue]" />
<asp:listitem Text="[add outreach/referral]" />
<asp:listitem Text="[add treatment note]" />
</asp:dropdownlist>
</div>
</itemtemplate>
</asp:templatecolumn>
Protected Sub ddlAction_OnChange(s As Object, e As EventArgs)
Dim ddl As DropDownList = CType(s, DropDownList)
Dim sSelectedValue As String = ddl.SelectedValue
Dim sDataKey As String = oDG.DataKeys(e.Item.ItemIndex)
System.Diagnostics.Debug.WriteLine("Selected Value = " & sSelectedValue.ToString())
System.Diagnostics.Debug.WriteLine("Selected ID = " & sDataKey.ToString())
End Sub
Has not been tested however so you may have to do some debugging.
e doesn’t contain a property for Item, Zak didn’t get the translation right, although he did say it was untested.
If you look at my code and his, I used the NamingContainer approach and it has a ItemIndex property. e is just System Args not DataGrid args.
Protected Sub ddlAction_OnChange(ByVal s As Object, ByVal e As EventArgs)
Dim ddl As DropDownList = CType(s, System.Web.UI.WebControls.DropDownList)
Dim sSelectedValue As String = ddl.SelectedValue
Dim dgi As DataGridItem = CType(ddl.NamingContainer, System.Web.UI.WebControls.DataGridItem)
Dim sDataKey As String = oDG.DataKeys(dgi.ItemIndex)
System.Diagnostics.Debug.WriteLine("Selected Value = " & sSelectedValue.ToString())
System.Diagnostics.Debug.WriteLine("Selected ID = " & sDataKey.ToString())
End Sub
Again, thanks for all the help - I really appreciate it.
I am running into a couple other problems, however, it is with my logic in using this approach, not the actual script.
[list][]The screen flickers three times. It’s not only annoying, but I have javascript written within that sub to open a new window for certain options so it loses focus (it actually posts back after writing the script so window.focus() isn’t doing me any good).
[]I don’t have a way to confirm the delete option.
[/list]I think my best bet is to add a button to ‘submit’ the drop down list option, however, I don’t know how I would access the drop down list from another control.
I attached the full source if it helps - the drop down sub is towards the bottom of the script block.
Man, I pulled dawn patrol on Friday at Tamarack’s in Carlsbad. Sets were coming in 4-5 feet consistantly. Unfortunately, I was freezing my *** off when I got out of the car so I decided not to paddle out. There’s another SW swell supposed to come in this Thursday, I might paddle out then. Did you see any action from the last swell?
Sorry man, that script is way to long for me to weed through on my time budget but as far as your Confirm question is concerned, have you looked at this link?