C#从GridView ASP网络的下拉列表中选择值 [英] C# getting selected value from dropdownlist in gridview asp net

查看:108
本文介绍了C#从GridView ASP网络的下拉列表中选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当更改网格视图中的下拉列表的值时,如何更改文本框的值?

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

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

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

代码如下。

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

下面的代码如下。

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


推荐答案

I在 GridView 中使用 DropDownLists 有类似的问题。我的解决方案是调整下拉菜单的 onLoad ,以免在每个帖子上都重新编写 DropDownList 背部。这样,如果那里有东西,它将不会重新填充它。

I had a similar problem using the DropDownLists in GridView. My solution was to adjust the onLoad for the dropdown so that it wouldn't re-write the DropDownList on every post back. 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
    }
}

这篇关于C#从GridView ASP网络的下拉列表中选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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