根据gridview asp net

时间:2015-06-04 09:30:51

标签: asp.net gridview

我的gridview中有一个Dropdown和一个textBox。 gridview包含一些主题名称,在数据库中有特定的费用 学科。我想在选择下拉列表的每个值上显示费用进入文本框。

我已经完成了相同的功能,它可以提供我想要的精确输出,但不会在选择主题后立即输出,它会在按钮点击时给出值。

可能是我做错了但找不到确切的解决方案请帮忙。

ASPX代码:

<asp:GridView ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false" OnRowDataBound="Gridview1_RowDataBound" CssClass="EU_DataTable">
    <Columns>
        <asp:TemplateField HeaderText="Program Name">
            <ItemTemplate>
                <asp:DropDownList ID="ddl_ProgList" runat="server" AppendDataBoundItems="true" OnSelectedIndexChanged="ddl_ProgList_SelectedIndexChanged" ControlStyle-Width="160px">
                    <asp:ListItem Value="-1">Select</asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>

       <asp:TemplateField HeaderText="Fees">
            <ItemTemplate>
                <asp:TextBox ID="txt_fees" runat="server" ControlStyle-Width="75px"></asp:TextBox>
            </ItemTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <FooterTemplate>
                <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码背后:

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Control ctrl = e.Row.FindControl("ddl_ProgList");
        if (ctrl != null)
        {
            DropDownList dd = ctrl as DropDownList;

            //SqlConnection conn = new SqlConnection(connStr);

            clsProg_TB objprg = new clsProg_TB();
            objprg.Center_Code = ddl_centerCode.SelectedItem.ToString();
            DataSet ds = clsUserLogicLayer.LoadPrograms(objprg);
            dd.DataTextField = "prg_Name";
            dd.DataValueField = "prg_Name";
            dd.DataSource = ds.Tables[0];
            dd.DataBind();
        }
    }
}
protected void ddl_ProgList_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl = sender as DropDownList;

    foreach (GridViewRow row in Gridview1.Rows)
    {

        Control ctrl = row.FindControl("ddl_ProgList") as DropDownList;
        if (ctrl != null)
        {
            DropDownList ddl1 = (DropDownList)ctrl;

            if (ddl.ClientID == ddl1.ClientID)
            {
                TextBox txt2 = row.FindControl("txt_fees") as TextBox;
                clsProg_TB objprg = new clsProg_TB();
                objprg.Center_Code = ddl_centerCode.SelectedItem.ToString();
                objprg.Prg_Name = ddl1.SelectedItem.ToString();
                DataSet ds = clsUserLogicLayer.getProgFees(objprg);
                if (ds.Tables[0].Rows.Count != 0)
                {
                    txt2.Text = ds.Tables[0].Rows[0][0].ToString(); 
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以尝试更新Row并再次绑定gridview。您还需要在开始编辑之前设置GridView.SetEditRow()。另一种方法是更新dataTable并再次绑定它。

   protected void ddl_ProgList_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl = sender as DropDownList;

    foreach (GridViewRow row in Gridview1.Rows)
    {
GridView.SetEditRow(row.RowIndex); 


        Control ctrl = row.FindControl("ddl_ProgList") as DropDownList;
        if (ctrl != null)
        {
            DropDownList ddl1 = (DropDownList)ctrl;

            if (ddl.ClientID == ddl1.ClientID)
            {
                TextBox txt2 = row.FindControl("txt_fees") as TextBox;
                clsProg_TB objprg = new clsProg_TB();
                objprg.Center_Code = ddl_centerCode.SelectedItem.ToString();
                objprg.Prg_Name = ddl1.SelectedItem.ToString();
                DataSet ds = clsUserLogicLayer.getProgFees(objprg);
                if (ds.Tables[0].Rows.Count != 0)
                {
                    txt2.Text = ds.Tables[0].Rows[0][0].ToString(); 
                }
            }
        }
         Gridview1.UpdateRow(row.RowIndex, false);
    }
Gridview1.DataBind();
}