C#从gridview asp net的下拉列表中获取选定的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11176829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:53:09  来源:igfitidea点击:

C# getting selected value from dropdownlist in gridview asp net

c#gridviewdrop-down-menuselectedvalue

提问by gogogo

How can I change the value of a textbox whenever a dropdownlist within a gridview has its value changed?

每当 gridview 中的下拉列表的值发生变化时,如何更改文本框的值?

On page load, the textbox shows the selected value, but when I change the selection of the dropdownlist, the textbox value doesn't change.

在页面加载时,文本框显示所选值,但是当我更改下拉列表的选择时,文本框值不会更改。

The code is below.

代码如下。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false">
    <Columns>
        <asp:TemplateField HeaderText="Entry">
            <ItemTemplate>
                <%# Container.DataItemIndex + 1 %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Duty">
            <ItemTemplate>
                <asp:DropDownList ID="duty" runat="server" OnLoad = "ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged" autopostback="true" EnableViewState="true"></asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

The code behind is below.

后面的代码如下。

protected void ddl1_load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable dt = new DataTable();
        Duty dy = new Duty();
        dt = dy.getdutyid(Convert.ToInt32(dropcontractid.SelectedValue));
        DropDownList ddl = (DropDownList)sender;
        ddl.DataSource = dt;
        ddl.DataTextField = "dutyid";
        ddl.DataValueField = "dutyid";
        ddl.DataBind();
        TextBox1.Text = ddl.SelectedValue;
    }
}

回答by Coder

You need to use SelectedIndexChangedhandler to show selected value:

您需要使用SelectedIndexChanged处理程序来显示选定的值:

Markup:

标记:

<asp:DropDownList ID="duty" runat="server" OnLoad="ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged"></asp:DropDownList>

Code-behind:

代码隐藏:

protected void duty_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);   
    DropDownList duty= (DropDownList) gvr.FindControl("duty");
    TextBox1.Text = duty.SelectedItem.Value;
}

回答by Simon Ejsing

You should look into using data binding instead. You can bind the textbox.Text to the selecteditem.value, this will ensure that proper updating takes place

您应该考虑使用数据绑定。您可以将 textbox.Text 绑定到 selecteditem.value,这将确保正确更新发生

回答by user1666231

I had a similar problem using the DropDownListsin GridView. My solution was to adjust the onLoadfor the dropdown so that it wouldn't re-write the DropDownListon every post back. This way if there's something there then it won't re-populate it.

我在使用DropDownListsin 时遇到了类似的问题GridView。我的解决方案是调整onLoad下拉菜单,这样它就不会DropDownList在每次回帖时重写。这样,如果那里有东西,那么它就不会重新填充它。

protected void dropDownLoad(object sender, EventArgs e)
{
    DropDownList dropDown = sender as DropDownList;
    if (dropDown.SelectedValue == null || dropDown.SelectedValue == "")
    { 
        // Your Code to populate table
    }
}

回答by user1851384

this happens to me once then i code like this... but i didnt use the onLoad attribute, tell me if this works,

这发生在我身上,然后我像这样编码...但我没有使用 onLoad 属性,告诉我这是否有效,

 <asp:TemplateField HeaderText="duty" SortExpression="duty">
                                       <EditItemTemplate>
                                      <asp:TextBox ID="duty" runat="server" Text='<%# Bind("duty_Name") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                      <ItemTemplate>
                                           <asp:Label ID="lblduty" runat="server" Text='<%# Eval("duty_Name") %>' />
                                        <asp:DropDownList ID="ddlduty" runat="server" CssClass="dropdownlist" 
                                          OnLoad = "ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged" Visible = "false" 
                                           >
                                        </asp:DropDownList>
                                    </ItemTemplate>


                                        <HeaderStyle Width="5%" />
                                        <ItemStyle HorizontalAlign="Center" />
                                    </asp:TemplateField>