在GridView中使用Eval为DropDownList选择的值

时间:2012-06-18 16:00:57

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

现在,我有一个名为'PROCESS_FLAG'的数据库字段,其中包含文本值(A,I,E,C),我希望能够更改我的下拉列表列的默认选定值,具体取决于值PROCESS_FLAG,我不知道从哪里开始。

以下是我拥有的GridView的代码:

 <asp:BoundField DataField="REQUESTQTY" HeaderText="Request Quantity" 
                SortExpression="REQUESTQTY" />
            <asp:BoundField DataField="MOVEFROM" HeaderText="Move From" 
                SortExpression="MOVEFROM" />
            <asp:BoundField DataField="MOVETO" HeaderText="Move To" 
                SortExpression="MOVETO" />
            <asp:BoundField DataField="COMPLETEDBY" HeaderText="Completed By" 
                SortExpression="COMPLETEDBY" Visible="false"/>
            <asp:BoundField DataField="COMPLETION_DATE" HeaderText="Completion Date" 
                SortExpression="COMPLETION_DATE" />
            <asp:BoundField DataField="COMMENTS" HeaderText="Comments" 
                SortExpression="COMMENTS" Visible="false" />
            <asp:BoundField DataField="RESPONSETIME" HeaderText="Response Time" 
                SortExpression="RESPONSETIME" Visible="false" />
            <asp:BoundField DataField="PROCESS_FLAG" HeaderText="Process Flag"
                SortExpression="PROCESS_FLAG" />
            <asp:BoundField DataField="UNIQUEKEY" HeaderText="Unique Key"
                SortExpression="UNIQUEKEY" Visible="true" />

            <asp:TemplateField HeaderText="Send To...">
                <ItemTemplate>
                    <asp:DropDownList ID="StatusDD" runat="server" AutoPostBack="false" OnSelectedIndexChanged="StatusDD_SelectedIndexChanged">
                        <asp:ListItem Value="A">Active</asp:ListItem>
                        <asp:ListItem Value="C">Complete</asp:ListItem>
                        <asp:ListItem Value="I">In Process</asp:ListItem>
                        <asp:ListItem Value="E">Error</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>

我想根据PROCESS_FLAG的值设置下拉列表的默认选定值。

对此的任何帮助将不胜感激。我不是Eval功能的专业人士。

谢谢!

1 个答案:

答案 0 :(得分:1)

在.aspx页面的gridview中添加一个OnRowDataBound属性:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="id" OnRowDataBound="GridViewRowEventHandler">

然后,替换

<asp:BoundField DataField="PROCESS_FLAG" HeaderText="Process Flag"
            SortExpression="PROCESS_FLAG" />

     <asp:TemplateField>
     <ItemTemplate>             
     <asp:Label ID="process_Flags" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PROCESS_FLAG").ToString()%>'/>
      </ItemTemplate>                    
      </asp:TemplateField>

然后在您的代码中,执行以下操作:

 protected void GridViewRowEventHandler(Object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           Label flag = (Label)e.Row.FindControl("process_Flags");
           DropDownList ddl = (DropDownList)e.Row.FindControl("StatusDD");
            if (flag.Text == "A")
            {
                ddl.SelectedValue = "A";
            }
        //add more conditions here..

        }          
    }

这应该会让你更进一步:)