2

现在,我有一个名为“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 功能的专家。

谢谢!

4

1 回答 1

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..

        }          
    }

这应该让你更进一步:)

于 2012-06-18T16:39:05.583 回答