tags:

views:

33

answers:

3

I have repeater which shows a list of users and there roles. There is also a dropdown list to chnage there role see below

<td><asp:DropDownList ID="ddlChangeType" class="controlwidth100" runat="server" AutoPostBack="true" OnSelectedIndexChanged="change"  /></td>

the change event works but what I need is the ID of the row so the user can be updated.

A: 

One option is to do this in a GridView, using its inbuilt editing capabilities. I wouldn't normally suggest the GridView control, but I do here because from the markup you have entered it is clear that you are rendering a table anyway.

Another option would be to do it as an AJAX call, picking up the row value via Javascript. This might perform a little better than a full postback anyway.

Daniel Dyson
A: 
<td>
    <asp:DropDownList ID="ddlChangeType" class="controlwidth100" 
        runat="server" AutoPostBack="true" OnSelectedIndexChanged="change"  />
    <asp:HiddenField ID="RoleIdValue" runat="server" 
        Value='<$# Databinder.Eval(Container.DataItem, "UserId") %>' />
        //Set the userId here so you can access it from code
</td>

protected void change(object sender, EventArgs e) {
    DropDownList ddlChangeType = (DropDownList)sender;
    HiddenField RoleIdValue = 
        (HiddenField)ddlChangeType.Parent.FindControl("RoleIdValue");

    string? userId = RoleIdValue.Value;
    string? roleId = ddlChangeType.SelectedValue;
    SomeDS.Update(userId, roleId);
}
Marko
+1  A: 

It still exposes the UserID on the client but this should get you there:

<td><asp:DropDownList ID="ddlChangeType" class="controlwidth100" runat="server" AutoPostBack="true" OnSelectedIndexChanged="change" UserID=<%#Eval("UserID") %> /></td>

And for the server event:

Protected Sub change(ByVal sender As Object, ByVal e As EventArgs)
    Response.Write(DirectCast(sender, DropDownList).Attributes("UserID"))
End Sub
Hugh Jeffner