Using a databound dropdown within datagrid

ninel

Active member
Joined
Mar 23, 2005
Messages
32
Location
Land O Lakes, Florida
Programming Experience
3-5
I have a dropdownlist box within a datagrid that is populated with values that come from a database.

What I need is....
When a user selects a different value in the dropdown , on the autopostback I need to update the database with that value and then re-display the datagrid with the dropdown having the new value from the database.

Here's my code so far:
HTML:
<asp:datagrid id="grdResults" runat="server" Height="50px" Width="1200px" 
Font-Names="Tahoma"Font-Size="XX-Small" ForeColor="Black" HorizontalAlign="Center" 
CellSpacing="1" BorderStyle="Solid" AutoGenerateColumns="False"CellPadding="1" 
PageSize="1" BorderColor="Silver">
<AlternatingItemStyle CssClass="GridAltItem">
</AlternatingItemStyle>
<HeaderStyle Font-Size="XX-small" Font-Bold="True"></HeaderStyle><Columns>        
<asp:BoundColumn DataField="iID" visible="False" HeaderText="ID"></asp:BoundColumn>    
<asp:BoundColumn DataField="iExceptionId" visible="False" HeaderText="ExceptionId"></asp:BoundColumn>
    <asp:BoundColumn DataField="sType" HeaderText="Type"></asp:BoundColumn> 
       <asp:TemplateColumn HeaderText="Status">  
          <ItemTemplate>      
  <asp:DropDownList AutoPostback="true" 
 id="ddlStatus" DataSource="<%#BindState()%>" 
TextField="sStatusName" DataValueField="iCmOtdExceptionStatusId" runat="server" Font-Name="Tahoma" 
Font-Size ="xx-small" runat="server" />   
     </ItemTemplate>   
 </asp:TemplateColumn></Columns>
</asp:datagrid>


VB.NET:This populates the dropdown within the datagrid.
VB.NET:
Public Function BindState()
        Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("connString"))
        Dim myCommand As SqlCommand = New SqlCommand("uspGetOTDExceptionStatus", myConnection)
        myCommand.CommandType = CommandType.StoredProcedure
        myConnection.Open()
        Return myCommand.ExecuteReader(CommandBehavior.CloseConnection)
End Function

I can't figure out in which event I need to capture the new dropdown value and then call the stored proc that updates the value in the database.

Can someone help me please?

Thanks,
Ninel
 
Basically what you need to do is provide a handler for the selected index change of the dropdownlist. There is more than one way of doing this, for example in the itemdatabound event of the datagrid you can register a handler and set the autopostback to true. Another, I find easier way is:

Add this to your dropdown designer code:

OnSelectedIndexChanged="HandlePostback" AutoPostBack="True"

From the vb side

VB.NET:
[COLOR=blue]Protected[/COLOR] [SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE]HandlePostback([SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2]System.EventArgs)[/SIZE]

 [INDENT]' do events here[/INDENT]
[COLOR=blue]End Sub[/COLOR]
 
Back
Top