根据ASP.Net中gridview(编辑模式)内的下拉列表的选定值,为gridview中的其他列设置值

时间:2011-10-03 06:17:01

标签: c# asp.net gridview

我有员工姓名,Emp Code,Designation的gridview。我正在使用rowdatabound事件中的值填充Employee Names下拉列表。在编辑模式中,在选择“员工姓名”下拉列表中的值时,EmpCode和Designation应相应更改。 empCode和Designation标签控件位于templatefield中。我为Employee Names drodownlist提供了writtern selectionchanged事件,

ddlEmp.SelectedIndexChanged += new EventHandler(grd_ddlEmp_SelectedIndexChanged);

但我不知道如何更改gridview中特定行内的Emp Code和Designation值。

4 个答案:

答案 0 :(得分:1)

您需要将gridview放在 UpdatePanel 中并在 SelectedIndexChanged 事件的代码后面执行,就像我在下面的datalist中所做的那样:

<asp:DataList ID="dlstPassengers" runat="server" OnItemDataBound="dlstPassengers_ItemDataBound"
 RepeatDirection="Horizontal" RepeatColumns="2" Width="100%">
 <ItemTemplate>
       <div class="form-linebg" style="line-height: 32px;">
            <asp:DropDownList ID="ddlCountry" AutoPostBack="true"  OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged"
CssClass="select-3" Style="width: 145px; margin-bottom: 9px;" runat="server">
</asp:DropDownList>
<br />
<asp:DropDownList ID="ddlCity" CssClass="select-3" Style="width: 145px; margin-bottom: 10px;"
runat="server">
</asp:DropDownList>
</div>
</ItemTemplate>
</asp:DataList>

在代码背后:

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList ddlCountry = (DropDownList)sender;
                DropDownList ddlCity = (DropDownList)((DropDownList)sender).Parent.FindControl("ddlCity");
                BindCity(ddlCity, ddlCountry.SelectedValue);
            }

    private void BindCity(DropDownList ddlCity, string countryCode)
            {
    // Binding code of city based selected country
    }

您需要通过在后面的代码中查找控件,在下拉列表的SelectedIndexChanged事件上设置EmpCode和Designation列。

答案 1 :(得分:1)

在SelectedIndexChanged函数中,找到下拉框的选定值

  DropDownList ddl = (GridView1.Rows[GridView1.SelectedIndex].FindControl("DropDownList1") AS DropDownList);
  var selectedVal = ddl.SelectedValue;

执行一些代码以确定Emp Code和Desgination,然后填充相关标签(或其他控件):

Label lbl = GridView1.Rows[GridView1.SelectedIndex].FindControl("Label1") as Label;

为标签指定值。

答案 2 :(得分:1)

SelectedIndexChanged

DropDownList事件中是否包含这些内容
  1. 首先找到行。
  2. 然后访问控件并相应更改。
  3. 伪代码

    protected void grd_ddlEmp_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView row = ((DropDownList)sender).NamingContainer as GridViewRow;
        Label Designation = row.FindControl("id of the designation label") as Label;
        Designation.Text = "new Designation Name";
    }
    

答案 3 :(得分:0)

在您的下拉列表中使用 AutoPostBack =“true”