网格视图RowDataBound中的输入字符串格式不正确?

时间:2017-06-22 15:58:04

标签: c# asp.net

这是HTML下拉列表。

在编辑按钮上没有绑定项目模板字段

中标签的确切值
<asp:TemplateField HeaderText="Country">
    <EditItemTemplate>
        <asp:DropDownList ID="ddlcountry" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlcountry_SelectedIndexChanged"></asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="lblcountry" runat="server" Text='<%#Eval("cnname") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State">
    <EditItemTemplate>
        <asp:DropDownList ID="ddlstate" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlstate_SelectedIndexChanged"></asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="lblstate" runat="server" Text='<%#Eval("sname") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
    <EditItemTemplate>
        <asp:DropDownList ID="ddlcity" runat="server"></asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="lblcity" runat="server" Text='<%#Eval("cname") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

这是控制器代码:

    protected void gvcustomgrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                //Find the blood group DropDownList in the Row
                DropDownList ddlbloodgroup = (e.Row.FindControl("ddlbg") as DropDownList);
                ddlbloodgroup.DataSource = ttdal.getbloodgroup();
                ddlbloodgroup.DataTextField = "bg_name";
                ddlbloodgroup.DataValueField = "bg_id";
                ddlbloodgroup.DataBind();
                ddlbloodgroup.SelectedValue = gvcustomgrid.DataKeys[e.Row.RowIndex].Value.ToString();

                //Find the country DropDownList in the Row
                DropDownList ddlcountry = (e.Row.FindControl("ddlcountry") as DropDownList);
                ddlcountry.DataSource = ttdal.getcountry();
                ddlcountry.DataTextField = "cnname";
                ddlcountry.DataValueField = "cnid";
                ddlcountry.DataBind();
                ddlcountry.SelectedValue = gvcustomgrid.DataKeys[e.Row.RowIndex].Value.ToString();

                //Find the state DropDownList in the Row
                DropDownList ddlstate = (e.Row.FindControl("ddlstate") as DropDownList);
                ddlstate.DataSource = ttdal.getstate(Convert.ToInt32(ddlcountry.SelectedValue));
                ddlstate.DataTextField = "sname";
                ddlstate.DataValueField = "sid";
                ddlstate.DataBind();
                ddlstate.SelectedValue = gvcustomgrid.DataKeys[e.Row.RowIndex].Value.ToString();

                //Find the city DropDownList in the Row
                DropDownList ddlcity = (e.Row.FindControl("ddlcity") as DropDownList);
                ddlcity.DataSource = ttdal.getcity(Convert.ToInt32(ddlstate.SelectedValue));
                ddlcity.DataTextField = "cname";
                ddlcity.DataValueField = "cid";
                ddlcity.DataBind();
                ddlcity.SelectedValue = gvcustomgrid.DataKeys[e.Row.RowIndex].Value.ToString();
            }




        }
    }

    protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Find the state DropDownList in the Row
        DropDownList ddlcountry = (DropDownList)sender;
        GridViewRow row = (GridViewRow)ddlcountry.NamingContainer;
        if (row != null)
        {
            if ((row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList ddlstate = (DropDownList)row.FindControl("ddlstate");

                ddlstate.DataSource = ttdal.getstate(Convert.ToInt32(ddlcountry.SelectedValue));
                ddlstate.DataValueField = "sid";
                ddlstate.DataTextField = "sname";
                ddlstate.DataBind();
            }
        }

    }

    protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Find the city DropDownList in the Row
        DropDownList ddlstate = (DropDownList)sender;
        GridViewRow row = (GridViewRow)ddlstate.NamingContainer;
        if (row != null)
        {
            if ((row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList ddlcity = (DropDownList)row.FindControl("ddlcity");

                ddlcity.DataSource = ttdal.getcity(Convert.ToInt32(ddlstate.SelectedValue));
                ddlcity.DataValueField = "cid";
                ddlcity.DataTextField = "cname";
                ddlcity.DataBind();
            }
        }
    }

如何在网格视图RowDataBound中获取正确的格式?

0 个答案:

没有答案