无法使用OnRowCommand在EditItemTemplate中填充DropDownList

时间:2015-06-11 09:15:09

标签: c# asp.net drop-down-menu edititemtemplate

这是我的代码背后的代码。我想在用户点击编辑后填充DropDownList,但我得到的DropDownList为null。为什么呢?

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>sample</groupId>
    <artifactId>movies-spring-data</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>Movies-Example</name>


    <!-- tag::dependencies[] -->
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <start-class>movies.spring.data.neo4j.SampleMovieApplication</start-class>
        <spring-data-rest.version>2.3.0.RELEASE</spring-data-rest.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.4.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-neo4j-rest</artifactId>
            <version>3.3.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-neo4j</artifactId>
            <version>3.3.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <!-- end::dependencies[] -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>

// asp代码

protected void SupportSchedule_RowCommand(object sender, GridViewCommandEventArgs e) 
{
    if (e.CommandName == "EditRow") 
    {
        int rowIndex = ((GridViewRow)((ImageButton) e.CommandSource).NamingContainer).RowIndex;
        GridViewRow row = (GridViewRow)(((ImageButton) e.CommandSource).NamingContainer);
        SupportScheduleTable.EditIndex = rowIndex;
        shift.Enabled = true;
        resourcedate.Enabled = true;
        ListItemCollection c = db.fillList();

        DropDownList ddl1 = row.FindControl("ddlshiftmanager") as DropDownList;
        DropDownList ddl2 = row.FindControl("ddldispatcherone") as DropDownList;
        DropDownList ddl3 = row.FindControl("ddldispatchertwo") as DropDownList;

        if (c != null && ddl1 != null) 
        {
            ddl1.DataSource = c;
            ddl2.DataSource = c;
            ddl3.DataSource = c;
            ddl1.DataBind();
            ddl2.DataBind();
            ddl3.DataBind();
        }
        getSupportSchedule();
    } 
    else if (e.CommandName == "CancelUpdate")
    {
        //some codes here
    } else if (e.CommandName == "UpdateRow")
    {
        //some codes here
    }
}

我刚刚在最近的更新中添加了ImageButton,但这是我的原始代码无效。

2 个答案:

答案 0 :(得分:1)

我使用的是SqlDataSource,而不是从后面添加列表项 这就是我用来获取DropDownList的选定值。

else if (e.CommandName == "UpdateRow")
{
    int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
    DropDownList ddlshift = (DropDownList)SupportScheduleTable.Rows[rowIndex].FindControl("ddlshiftmanager");
    DropDownList ddlone = (DropDownList)SupportScheduleTable.Rows[rowIndex].FindControl("ddldispatcherone");
    DropDownList ddltwo = (DropDownList)SupportScheduleTable.Rows[rowIndex].FindControl("ddldispatchertwo");
    string manager = ddlshift.SelectedValue;
    string one = ddlone.SelectedValue;
    string two = ddltwo.SelectedValue;
    int supportID = Convert.ToInt32(e.CommandArgument);
    String sh = shift.Text;
    String date = resourcedate.Text;
    db.updateSS(supportID, sh, manager, one, two,date);
    SupportScheduleTable.EditIndex = -1;
    shift.Enabled = false;
    resourcedate.Enabled = false;
    getSupportSchedule();
} 

答案 1 :(得分:0)

您的答案是处理您所看到问题的正确方法。但是,如果你没有弄清楚实际原因......

您点击的ImageButton位于ItemTemplate。您要绑定的DropDownList位于EditItemTemplate。当不是处于编辑模式时,lbEdit存在,{<1}}仅在您 时才存在。

因此,修复方法是将GridView置于编辑模式。请注意,这是您实际上已经开始做的事情。您需要设置EditIndex,重新绑定GridView,然后再次获取该行。然后,您将使该行处于编辑模式。此行现在应包含ddlshiftmanager

ddlshiftmanager