views:

934

answers:

2

I'm missing something here...

I need to access the values of the controls on the item that is leaving editmode, thus firing ItemUpdating.

I found how to get the key value so I know which record in the database I have to update. The problem is that I can't seem to access the values of the controls on the editing row.

the EditTemplate contains a few checkboxes, a dropdown and textbox. I need to access these values so I can update the record.

When looking at the eventargs, nothing shows.

I think I'm overlooking something crucial here, any help would be handy.

A: 

There are two ways I can think to do this.

The first way would be to use a datasource that supports an update command and using two way binding to up date the values. The following snipped uses two way binding to populate the name and student fields and it will also update them for you.

<EditItemTemplate>
            <tr style="background-color: #FFCC66;color: #000080;">
                <td>
                    <asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
                        Text="Update" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                        Text="Cancel" />

                </td>
                <td>
                    <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' />
                </td>
                <td>
                    <asp:TextBox ID="studentTextBox" runat="server" Text='<%# Bind("student") %>' />
                </td>
            </tr>
        </EditItemTemplate>

If you are not using a datasource that supports this you can do one other thing. Notice how the update button has a command called "Update". You can use this to fetch the control values you want by handling the list views ItemCommand Event.

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "Update")
        {
            TextBox box = (TextBox)e.Item.FindControl("nameTextBox");

            string name = box.Text;
        }
    }

You can find any control in the editied item by simply calling find control and passing it the control's ID, and don't forget the cast.

Hope this helps

cptScarlet
going through the ItemCommand instead of ItemUpdating is indeed the solution for me as I don't use a DataSource :)thanks !
Jan W.
reopening ...I still have a problem, since using the Update Command in the aspx it expects an ItemUpdating event, which I now catch in ItemCommand. This throws an error. when I use another commandname I seem to have trouble with the viewstate when I reload my list ... any ideas?
Jan W.
I think I need to see some code before I can help, the command name should not cause ItemCommand to throw any errors. Also are you still handling the ItemUpdating event? If you are I'm not sure you really need to. I think you can do everything you were doing in the ItemUpdating event in the ItemCommand Event.Also, can you be more informative about your viewstate issue when reloading the lis?
cptScarlet
A: 

You can also use "ListView.EditItem.FindControl("X")" to access control directly.

Protected Sub ListView_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewUpdateEventArgs) Handles ListView.ItemUpdating

        Dim DropDownListddl As DropDownList = ListView.EditItem.FindControl("DropDownListddl")

        Lblwarning.Text = DropDownListddl.SelectedValue
End Sub
Xtradi