下拉列表在 gridview RowCommand 上丢失值

发布于 2024-12-18 00:43:19 字数 3525 浏览 3 评论 0原文

我在 GridView 中有一个 DropDownList。我在 GridView RowCreated() 中动态填充 GridView。我想在单击 GridView 的命令按钮时获取 DropDownList 的值。

在 RowCommand 函数中,我试图获取该值。但是,我在该功能中找不到控件。令人惊讶的是,另一个 DropDownList 在同一函数中运行良好。

C# 代码如下:

if (Request.Params["ID"] != null && Request.Params["ID"] != "")
{
    FirmID = Convert.ToInt32(Request.Params["ID"]);
    //OfficeList = IFARecord.GetOffices(FirmID);    
}
if (!IsPostBack)
{
    GV_SABAdvisers.DataSourceID = "objectDataSourceSAV";
    GV_SABAdvisers.DataBind();
}

protected void GV_SABAdvisers_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Index")
    {
        int Index = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = GV_SABAdvisers.Rows[Index];

        //Not working
        DropDownList Offices = row.FindControl("ddlLocation") as DropDownList;
        String officeID = (Offices is DropDownList) ? Offices.SelectedValue : null;

        DropDownList ddlJobTitle = row.FindControl("ddlJobTitle") as DropDownList;

        if (ddlJobTitle is DropDownList)
        {
            newAdviserJobTitle.JobTitleID = Convert.ToInt32(ddlJobTitle.SelectedValue);// this works fine.
        }
    }
}    

protected void GV_SABAdvisers_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList ddlLocation = e.Row.FindControl("ddlLocation") as DropDownList;
        DropDownList ddlJobTitle = e.Row.FindControl("ddlJobTitle") as DropDownList;   
        DataTable OfficeList = GetOffices(FirmID);
        DataTable dtJob = MyTouchstone.Advisers.AdviserFirmJobTitle.AllJobTitle();

        foreach (DataRow aOffice in OfficeList.Rows)
        {
            ddlLocation.Items.Add(new ListItem(aOffice["Address1"].ToString() + ", " + aOffice["Postcode"].ToString(), aOffice["OfficeID"].ToString()));
        }

        foreach (DataRow aJob in dtJob.Rows)
        {
            ddlJobTitle.Items.Add(new ListItem(aJob["JobTitle"].ToString(), aJob["JobTitleID"].ToString()));
        }
    }
}

ASP 标记:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="True">
    <ContentTemplate>
        <asp:GridView ID="GV_SABAdvisers" AutoGenerateColumns="false" PageSize = "10"
            CssClass="cssPager" AllowPaging="true" AllowSorting="true"
            OnPageIndexChanging="GV_SABAdvisers_PageIndexChanging"  
            runat="server" onrowcommand="GV_SABAdvisers_RowCommand" 
            onrowcreated="GV_SABAdvisers_RowCreated">
            <PagerStyle />
            <Columns>
                <asp:TemplateField HeaderText="Job Title">
                    <ItemStyle Width="80px" />
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlJobTitle" Width="80px" runat="Server"></asp:DropDownList>  
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Location">
                    <ItemStyle Width="200px" />
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlLocation" Width="80px" runat="Server"></asp:DropDownList>  
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

请问有人可以帮助我吗?

I have a DropDownList in a GridView. I am filling the GridView dynamically in GridView RowCreated(). I want to get that DropDownList's value when I click the GridView's Command Button.

In the RowCommand function, I am trying to get the value. However, I can't find the control in that function. Surprisingly, another DropDownList works fine in the same function.

C# code follows:

if (Request.Params["ID"] != null && Request.Params["ID"] != "")
{
    FirmID = Convert.ToInt32(Request.Params["ID"]);
    //OfficeList = IFARecord.GetOffices(FirmID);    
}
if (!IsPostBack)
{
    GV_SABAdvisers.DataSourceID = "objectDataSourceSAV";
    GV_SABAdvisers.DataBind();
}

protected void GV_SABAdvisers_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Index")
    {
        int Index = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = GV_SABAdvisers.Rows[Index];

        //Not working
        DropDownList Offices = row.FindControl("ddlLocation") as DropDownList;
        String officeID = (Offices is DropDownList) ? Offices.SelectedValue : null;

        DropDownList ddlJobTitle = row.FindControl("ddlJobTitle") as DropDownList;

        if (ddlJobTitle is DropDownList)
        {
            newAdviserJobTitle.JobTitleID = Convert.ToInt32(ddlJobTitle.SelectedValue);// this works fine.
        }
    }
}    

protected void GV_SABAdvisers_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList ddlLocation = e.Row.FindControl("ddlLocation") as DropDownList;
        DropDownList ddlJobTitle = e.Row.FindControl("ddlJobTitle") as DropDownList;   
        DataTable OfficeList = GetOffices(FirmID);
        DataTable dtJob = MyTouchstone.Advisers.AdviserFirmJobTitle.AllJobTitle();

        foreach (DataRow aOffice in OfficeList.Rows)
        {
            ddlLocation.Items.Add(new ListItem(aOffice["Address1"].ToString() + ", " + aOffice["Postcode"].ToString(), aOffice["OfficeID"].ToString()));
        }

        foreach (DataRow aJob in dtJob.Rows)
        {
            ddlJobTitle.Items.Add(new ListItem(aJob["JobTitle"].ToString(), aJob["JobTitleID"].ToString()));
        }
    }
}

ASP markup:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="True">
    <ContentTemplate>
        <asp:GridView ID="GV_SABAdvisers" AutoGenerateColumns="false" PageSize = "10"
            CssClass="cssPager" AllowPaging="true" AllowSorting="true"
            OnPageIndexChanging="GV_SABAdvisers_PageIndexChanging"  
            runat="server" onrowcommand="GV_SABAdvisers_RowCommand" 
            onrowcreated="GV_SABAdvisers_RowCreated">
            <PagerStyle />
            <Columns>
                <asp:TemplateField HeaderText="Job Title">
                    <ItemStyle Width="80px" />
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlJobTitle" Width="80px" runat="Server"></asp:DropDownList>  
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Location">
                    <ItemStyle Width="200px" />
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlLocation" Width="80px" runat="Server"></asp:DropDownList>  
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Can anyone help me, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻风 2024-12-25 00:43:19

我不知道您在源代码中的何处使用了 CommandName“Index”。

I don't see where have you used the CommandName "Index" in your source code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文