Click here to Skip to main content
15,921,840 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a grid that looks like this and it has DropDownlist1 that populates data as seen here.
When clicked on a button Update value from dropdownlist is stored to DB and thats fine.
I want on a page load only for those values stored from dropdown list to select that particular value in dropdown list on load. How can I do that?

So it has to load initial data but for those records stored to DB it should load what user selected?

How can you in any way pass the value from codebehind to dropdownlist in the grid?

ASP.NET
<pre lang="xml"><asp:GridView ID="GridView1" Width="100%" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="client_id" DataSourceID="SqlDataSource1" Font-Size="Medium" ForeColor="#333333" GridLines="None" AllowPaging="True" AllowSorting="True">
                      <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                      <Columns>
                          <asp:BoundField DataField="client_id" HeaderText="Client #" InsertVisible="False" ReadOnly="True" SortExpression="client_id" />
                          <asp:BoundField DataField="name_first" HeaderText="Name" SortExpression="name_first" />
                          <asp:BoundField DataField="name_last" HeaderText="Surname" SortExpression="name_last" />
                          <asp:BoundField DataField="phone1" HeaderText="Phone #" SortExpression="phone1" />
                          <asp:BoundField DataField="adress_line1" HeaderText="Address" SortExpression="adress_line1" />
                          <asp:TemplateField HeaderText="Counselors Selection">
                              <ItemTemplate>
                                  <asp:DropDownList ID="DropDownList1" Width="100%" runat="server" DataSourceID="SqlDataSource2" DataTextField="user_name" DataValueField="user_name" AppendDataBoundItems="true" AutoPostBack="True">
                                  <asp:ListItem Text="--Select--" Value=""></asp:ListItem>
                                  </asp:DropDownList>
                                  <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ocean4d_devConnectionString %>" SelectCommand="SELECT [user_name] FROM [AgencyUser] WHERE ([agency_id] = @agency_id)">
                                      <SelectParameters>
                                          <asp:SessionParameter Name="agency_id" SessionField="agency_id" Type="Int32" />
                                      </SelectParameters>
                                  </asp:SqlDataSource>
                              </ItemTemplate>
                          </asp:TemplateField>
                           <ASP:HYPERLINKFIELD text="&nbsp;UPDATE" datanavigateurlfields="client_id" datanavigateurlformatstring="main_page_process.aspx?client_id={0}"></ASP:HYPERLINKFIELD>
                      </Columns>
                      <EditRowStyle BackColor="#999999" />
                      <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                      <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                      <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                      <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                      <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                      <SortedAscendingCellStyle BackColor="#E9E7E2" />
                      <SortedAscendingHeaderStyle BackColor="#506C8C" />
                      <SortedDescendingCellStyle BackColor="#FFFDF8" />
                      <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                  </asp:GridView>

                  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ocean4d_devConnectionString %>" SelectCommand="SELECT [client_id], [name_first], [name_last], [phone1], [adress_line1] FROM [Client] WHERE ([agency_id] = @agency_id)">
                      <SelectParameters>
                          <asp:SessionParameter Name="agency_id" SessionField="agency_id" Type="String" />
                      </SelectParameters>
                  </asp:SqlDataSource>
Posted

If you want to populate the dropdown from database, then you can use the dataset, populate the data from table into dataset and
use a method to populate the dropdown using the dataset.
 
Share this answer
 
Comments
_Lovro_ 5-Dec-13 6:21am    
Problem is how to populate the dropdown in the grid

This is what I usually do when it is outside of the grid

Try
DropDownList3.Items.Clear()
Dim cmdgetpodaci As New SqlCommand("select * from filijale order by filijala", conn)
If conn.State = ConnectionState.Closed Then conn.Open()

Dim citaj As SqlDataReader = cmdgetpodaci.ExecuteReader

Me.DropDownList3.Items.Add(New ListItem("------------------------------------------", "0"))

Do While citaj.Read()
Dim newitem As New ListItem
newitem.Text = citaj.GetString(3)
newitem.Value = citaj.GetValue(3)
DropDownList3.Items.Add(newitem)
Loop

Catch ex As Exception
labelica.Visible = True
labelica.Text = "Problem prilikom spajanja na bazu" + ex.Message

Finally
conn.Close()
End Try

Can you send me the code example?
All the code you can write in the RowDataBound Event , here you can select particular index of deopdownlist, according to the value come from the database.
 
Share this answer
 
Comments
_Lovro_ 5-Dec-13 6:22am    
Are you able to send me code example how to do it since I am talking about dropdown in the grid?
Use the findControl() to find the dropdown
and then bind that dropdown to the DS

eg. dropdownlist ddl= gridview1.findcontrol("DropDownList1") as DropDownList;

now Bind ddl to your dataSource
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900