在Gridview中更改下拉列表选择的索引,从第二个DropDownlist中获取(从中获取值)

时间:2017-09-14 05:29:59

标签: c# asp.net gridview drop-down-menu

我要执行此操作的页面: enter image description here

我想将外部下拉列表连接到gridview下拉列表中。当从外部下拉列表中选择项目时,必须自动更改内部下拉列表项。我尝试过很多改变的事情。你能帮我个忙吗?感谢。

我的ASPX页面。(不要回答连接此代码)

<asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="False"   
CellPadding="4" DataKeyNames="Kimlik" GridLines="Horizontal"   
onrowdatabound="GridView4_RowDataBound" ForeColor="#333333" Visible="False">  
<AlternatingRowStyle BackColor="White" />  
<Columns>  
    <asp:TemplateField HeaderText="Öd.Nu.">  
        <EditItemTemplate>  
            <asp:TextBox ID="TextBox1x" runat="server" Text='<%# Bind("Kimlik") %>'></asp:TextBox>  
        </EditItemTemplate>  
        <ItemTemplate>  
            <asp:Label ID="Label1x" runat="server" Text='<%# Bind("Kimlik") %>'></asp:Label>  
        </ItemTemplate>  
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Öğrenci Adı Soyadı">  
        <EditItemTemplate>  
            <asp:TextBox ID="TextBox2x" runat="server" Text='<%# Bind("OgrenciAdiSoyadi") %>'></asp:TextBox>  
        </EditItemTemplate>  
        <ItemTemplate>  
            <asp:Label ID="Label2x" runat="server" Text='<%# Bind("OgrenciAdiSoyadi") %>'></asp:Label>  
        </ItemTemplate>  
    </asp:TemplateField>  

    <asp:TemplateField HeaderText="Odev Sonucu">  
        <EditItemTemplate>  
            <asp:TextBox ID="TextBox3x" runat="server"></asp:TextBox>  
        </EditItemTemplate>  
        <ItemTemplate>  
            <asp:DropDownList ID="DropDownList1x" runat="server">  
            </asp:DropDownList>  
        </ItemTemplate>  
    </asp:TemplateField> 

    <asp:TemplateField HeaderText="Ödev Sonuç Açıklaması">  
        <EditItemTemplate>  
            <asp:TextBox ID="TextBox4x" runat="server" Text='<%# Bind("SonucAciklama") %>'></asp:TextBox>  
        </EditItemTemplate>  
        <ItemTemplate>  
            <asp:Label ID="Label4x" runat="server" Text='<%# Bind("SonucAciklama") %>'></asp:Label>  
        </ItemTemplate>  
    </asp:TemplateField>  
</Columns>  

<EditRowStyle BackColor="#7C6F57" />

<FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True" />  
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />  
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />  
<RowStyle BackColor="#E3EAEB" />  
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />  
<SortedAscendingCellStyle BackColor="#F8FAFA" />  
<SortedAscendingHeaderStyle BackColor="#246B61" />  
<SortedDescendingCellStyle BackColor="#D4DFE1" />  
<SortedDescendingHeaderStyle BackColor="#15524A" />  
</asp:GridView>

2 个答案:

答案 0 :(得分:0)

关于外部下拉列表的选定索引更改事件添加以下代码

protected void OurterDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView.Rows)
            {
                DropDownList innerDropdown = (DropDownList)row.FindControl("innedDropedDOwnID");

                 innerDropdown .ClearSelection(); //making sure the previous selection has been cleared
                 innerDropdown .Items.FindByValue(OurterDropDown.SelectedValue).Selected = true;               

            }
}

答案 1 :(得分:0)

确保在DropDownList外部设置了AutoPostBack="true",然后在RowDataBound事件中尝试此代码:

protected void GridView4_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // check if your row is not a Header/Footer row
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // get Dropdownlist from gridview
        DropDownList DropDownList1x = e.Row.FindControl("DropDownList1x") as DropDownList;

        // ddlouter is your outside DropDownList
        DropDownList1x.Items.FindByValue(ddlOuter.SelectedValue).Selected = true; 
    }
}