如何将值传递给Grid中的DropDownList [英] How to pass a value to DropDownList in a Grid

查看:69
本文介绍了如何将值传递给Grid中的DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的网格,它有DropDownlist1来填充数据,如下所示。

点击按钮时,下拉列表中的更新值存储到数据库中,这很好。

我想在页面上加载仅从下拉列表中存储的值,以在加载下拉列表中选择该特定值。我该怎么做?



所以它必须加载初始数据但是对于那些存储到DB的记录,它应该加载用户选择的内容吗?



你怎么能以任何方式将值从代码隐藏传递到网格中的下拉列表?



 <   pre     lang   =  xml >  <   asp:GridView     ID   =  GridView1   宽度  =  100%    runat   =  server    AutoGenerateColumns   =     CellPadding   =  4    DataKeyNames   =  client_id    DataSourceID   =  SqlDataSource1   字体大小  =     Fore颜色  = #333333    GridLines   =  < span class =code-attribute>   AllowPaging   =  True    AllowSorting   =   True >  
< AlternatingRowStyle BackColor = 白色 ForeColor = #284775 / >
< < span class =code-keyword>>
< asp:BoundField DataField = client_id HeaderText = 客户端# InsertVisible = 错误 ReadOnly < span class =code-keyword> = True SortExpression = client_id / >
< asp:BoundField DataField = name_first < span class =code-attribute> HeaderText = 名称 SortExpression = name_first / >
< asp:BoundField DataField = name_last HeaderText = SortExpression = name_last / >
< asp:BoundField 数据字段 = phone1 HeaderText = 电话号码 SortExpression = phone1 / >
< asp:BoundField DataField = adress_line1 HeaderText = 地址 SortExpression = adress_line1 < span class =code-attribute> / >
< asp:TemplateField HeaderText = 辅导员选择 >
< ItemTemplate >
< asp:DropDownList ID = DropDownList1 宽度 = 100% runat = server DataSourceID = SqlDataSource2 DataTextField = user_name DataValueField = user_name AppendDataBoundItems < span class =code-keyword> = true AutoPostBack = True >
< asp:ListItem Text = - 选择 - = > < / 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 名称 = agency_id SessionField = agency_id 类型 = Int32 / >
< / SelectPar ameter >
< / asp:SqlDataSource >
< / ItemTemplate >
< / asp:TemplateField >
< ASP:HYPERLINKFIELD text = & nbsp; UPDATE datanavigateurlfields = client_id datanavigateurlformatstring = main_page_process.aspx?client_id = {0} > < / ASP:HYPERLINKFIELD >
< /列 >
< EditRowStyle BackColor = #999999 / >
& lt; FooterStyle BackColor = #5D7B9D Font-Bold = True ForeColor = 白色 / >
< HeaderStyle BackColor = #5D7B9D < span class =code-attribute> Font-Bold = ForeColor = 白色 / >
< PagerStyle BackColor = #284775 ForeColor = 白色 Horizo​​ntalAlign = 中心 / >
< RowStyle BackColor = #F7F6F3 ForeColor = #333333 < span class =code-keyword> / >
< SelectedRowStyle BackColor = #E2DED6 字体粗体 = 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 [客户] WHERE([agency_id] = @agency_id) >
< SelectParameters >
< asp:SessionParameter 名称 = agency_id SessionField = agency_id 类型 = 字符串 / >
< / SelectParameters >
< / asp:SqlDataSource >

解决方案

ConnectionStrings:ocean4d_devConnectionString%> SelectCommand = SELECT [user_name] FROM [AgencyUser] WHERE([agency_id] = @agency_id) >
< SelectParameters >
< asp:SessionParameter 名称 = agency_id SessionField = < span class =code-keyword> agency_id 类型 = Int32 / >
< / SelectParameters >
< / asp:SqlDataSource >
< / ItemTemplate >
< < span class =code-leadattribute> / asp:TemplateField >
< ASP:HYPERLINKFIELD text < span class =code-keyword> = & nbsp; UPDATE < span class =code-attribute> datanavigateurlfields = client_id datanavigateurlformatstring = main_page_process.aspx?client_id = {0} > < / ASP:HYPERLINKFIELD >
< /列 >
< ; EditRowStyle BackColor < span class =code-keyword> = #999999 / >
< FooterStyle BackColor = #5D7B9D 字体 - 粗体 = ForeColor = 白色 / >
< HeaderStyle BackColor = #5D7B9D Font-Bold = True ForeColor = 白色 / < span class =code-keyword>>
< PagerStyle BackColor = #284775 ForeColor = 白色 Horizo​​ntalAlign = 中心 / >
< < span class =code-leadattribute> RowStyle BackColor = #F7F6F3 ForeColor = #333333 / >
< SelectedRowStyle BackColor = #E2DED6 字体粗体 = True ForeColor = #333333 / >
< SortedAscendingCellStyle < span class =code-attribute> BackColor = #E9E7E2 / >
< SortedAscendingHeaderStyle BackColor = #506C8C / >
< SortedDescendingCellStyle Ba ckColor = #FFFDF8 / >
< SortedDescendingHeaderStyle BackColor = #6F8DAE / >
< / asp:GridView >

< asp:SqlDataSource ID < span class =code-keyword> = SqlDataSource1 runat = 服务器 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>

< /blockquote>

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.


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?

<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>

解决方案

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>


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.


这篇关于如何将值传递给Grid中的DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆