简体   繁体   English

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

[英]C# getting selected value from dropdownlist in gridview asp net

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

You need to use SelectedIndexChanged handler 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;
}

I had a similar problem using the DropDownLists in GridView . 我在GridView使用DropDownLists遇到了类似的问题。 My solution was to adjust the onLoad for the dropdown so that it wouldn't re-write the DropDownList on every post back. 我的解决方案是为下拉列表调整onLoad ,以便它不会在每个帖子上重写DropDownList This way if there's something there then it won't re-populate it. 这样,如果有什么东西那么它就不会重新填充它。

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

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,这将确保进行正确的更新

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GridView中的DropDownList绑定到ASP.NET C#中GridView内TextBox中DropDown的选定值 - DropDownList in a GridView to bind the selected value of DropDown in TextBox inside a GridView in asp.net c# 在 C# 的 asp.net 下拉列表中的 select2 上设置多个选定的值 - set multiple selected value on select2 multiple in asp.net dropdownlist from C# Gridview带有来自数据库的下拉列表,并在gridview外部添加,编辑删除按钮? ASP.NET C# - Gridview with dropdownlist from the database and outside add,edit delete buttons outside the gridview ? ASP.NET C# 如何将DropDownList选择的值插入到ASP.NET的GridView中? - How to insert DropDownList selected value into GridView in asp.net? 如何将DropDownList选择的值插入到ASP.NET中的GridView? - How to insert DropDownList selected value to GridView in asp.net? 在 ASP.Net 中的 GridView EditItemTemplate 中设置 DropDownList 选定值 - Set DropDownList Selected Value in GridView EditItemTemplate in ASP.Net 从ASP.NET C#中的gridview中选择时,检索并存储数据库中的复选框值? - Retrieve and store checkbox value in database when selected from gridview in asp.net c#? 从 DropDownList 值 C# ASP.NET 加载 TreeView - Load a TreeView from a DropDownList value C# ASP.NET 如何在ASP.NET C#WebForm中的文本框和下拉列表的GridView页脚行中获取值 - How Can I get the value in the gridview footer row of textbox and dropdownlist in asp.net c# webform asp:dropdownlist不保存用户选择的值c#asp.net aspx - asp:dropdownlist doesn't save users selected value c# asp.net aspx
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM