问题在生成页面时从DropDown列表中选择DB值

时间:2013-08-16 17:01:03

标签: asp.net c#-3.0

我有一个GV,我需要动态构建一个在过去180天内有活动数据库写入的用户列表。一个页面获取成员身份用户名并将其写入 表格1。然后,我在该表上执行SELECT Distinct以填充DDL并将该值写入Table2。问题是当我尝试通过SelectedValue='<%# Bind("DeliverPartTo") %>'设置DDL选择值时,我得到:

  
    

'DDL_DeliverPartTo'的SelectedValue无效,因为它在项目列表中不存在。     参数名称:值

  

以下是代码:

<asp:TemplateField HeaderText="DeliverPartTo" SortExpression="DeliverPartTo">
                <ItemTemplate>                         
                     <asp:DropDownList ID="DDL_DeliverPartTo" runat="server" SelectedValue='<%# Bind("DeliverPartTo") %>'                            DataSourceID="SQLTechs" DataTextField="RequestedBy" DataValueField="RequestedBy" 
                        Height="25px" Width="135px" AutoPostBack="True"  AppendDataBoundItems="true" 
                        OnDataBound="SqlDS_DeliverPartTo_DataBinding" OnSelectedIndexChanged="DDL_DeliverPartTo_SelectedIndexChanged" 
                        >
                        <asp:ListItem Text="" Enabled="true" Selected="True" Value='-1'></asp:ListItem>
                        <asp:ListItem Text="Stock" Enabled="true" Value='-1'></asp:ListItem>
                    </asp:DropDownList>
                     <asp:SqlDataSource ID="SQLTechs" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:inventory_v2ConnectionString %>"
                        SelectCommand="SELECT DISTINCT [RequestedBy] FROM [Process_PartRequests]">
                    </asp:SqlDataSource>
                </ItemTemplate>
        </asp:TemplateField> 



 <asp:SqlDataSource ID="SqlDataSource1" 
            runat="server" 
            ConnectionString="<%$ ConnectionStrings:inventory_v2ConnectionString %>" 
            SelectCommand="SELECT Process_WarrantyTracker.ID, Process_WarrantyTracker.RequestNumber, Process_PartRequests.DateRequested, Process_RequestStatuses.ID AS StatusID, 
                         Process_RequestStatuses.StatusCode, Process_PartRequests.CATicketNumber, Process_PartRequests.SystemSerialNumber, 
                         Models_PriModels.PriModel + '-' + Models_SecModels.SecModel + '-' + Models_FormFactor.Form AS Model, Part_Catalog.OEMPartCode, 
                         Part_Groups.GroupType + ' - ' + Part_Groups.GroupName AS Groups, Process_PartRequests.RequestedBy, Models_OEMs.Manufacturer AS OEM, 
                         Process_WarrantyTracker.DateOrderedFromOEM, Process_WarrantyTracker.OrderedByICGAdmin, Process_WarrantyTracker.OEMWorkOrderNumber, 
                         Process_WarrantyTracker.PartSerialNumber, Process_WarrantyTracker.OrderRecievedDate, Process_WarrantyTracker.DeliverPartTo, 
                         Process_WarrantyTracker.ProblemFlag, Process_WarrantyTracker.PartOfMultiPartOrder, Process_WarrantyTracker.ShipperTrackingNo, 
                         Process_WarrantyTracker.ReturnOrderShipped, Process_WarrantyTracker.Comment1, Process_WarrantyTracker.Comment2
FROM            Process_WarrantyTracker INNER JOIN
                         Process_PartRequests ON Process_WarrantyTracker.RequestNumber = Process_PartRequests.RequestNumber INNER JOIN
                         Process_RequestStatuses ON Process_PartRequests.RequestStatus = Process_RequestStatuses.ID INNER JOIN
                         Models ON Process_PartRequests.Model = Models.ID INNER JOIN
                         Models_FormFactor ON Models.FormFactor = Models_FormFactor.ID INNER JOIN
                         Models_OEMs ON Models.Manufacturer = Models_OEMs.ID INNER JOIN
                         Models_PriModels ON Models.PriModel = Models_PriModels.ID INNER JOIN
                         Models_SecModels ON Models.SecModel = Models_SecModels.ID INNER JOIN
                         Part_Catalog ON Process_PartRequests.OEMPartCode = Part_Catalog.ID INNER JOIN
                         Part_Groups ON Part_Catalog.GroupType = Part_Groups.ID
                         WHERE Process_RequestStatuses.ID != 4">

        </asp:SqlDataSource>
protected void SqlDS_DeliverPartTo_DataBinding(object sender, EventArgs e)
{
    try
    {
        GridViewRow gr = (GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent;
        HiddenField RequestNumber = (HiddenField)gr.FindControl("RequestNumber");
        string ReqNum = RequestNumber.Value;
        string query = "SELECT [DeliverPartTo] FROM [Process_WarrantyTracker] WHERE [RequestNumber] = @RequestNumber ";
        DropDownList DDL_DeliverPartTo = (DropDownList)GridView1.FindControl("DDL_DeliverPartTo");
        SqlConnection connectionString = new SqlConnection(ConfigurationManager.ConnectionStrings["inventory_v2ConnectionString"].ConnectionString);
        connectionString.Open();
        SqlCommand cmd = new SqlCommand(query, connectionString);
        Object returnValue;
        cmd.CommandText = query;
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@RequestNumber", ReqNum);
        cmd.Connection = connectionString;            
        returnValue = cmd.ExecuteScalar();
        connectionString.Close();
        DDL_DeliverPartTo.SelectedValue = returnValue.ToString();
    }
    catch (Exception ex)
    {
        //Response.Write(ex.Message);
    }
}
protected void DDL_DeliverPartTo_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow gr = (GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent;
    DropDownList DDL_DeliverPartTo = (DropDownList)gr.FindControl("DDL_DeliverPartTo");
    HiddenField RequestNumber = (HiddenField)gr.FindControl("RequestNumber");
    string ReqNum = RequestNumber.Value;
    string query = "UPDATE [Process_WarrantyTracker] SET [DeliverPartTo]=@DeliverPartTo WHERE [RequestNumber] = @RequestNumber ";
    SqlConnection connectionString = new SqlConnection(ConfigurationManager.ConnectionStrings["inventory_v2ConnectionString"].ConnectionString);
    SqlCommand cmd = new SqlCommand(query, connectionString);
    cmd.Connection.Open();
    cmd.Parameters.Add("@DeliverPartTo", DDL_DeliverPartTo.SelectedValue);
    cmd.Parameters.Add("@RequestNumber", ReqNum);
    cmd.CommandType = CommandType.Text;
    Object ddlValues;
    ddlValues = cmd.ExecuteScalar();
    cmd.Connection.Close();
    cmd.Connection.Dispose();
    Response.Redirect("Admin-WarrantyTracker.aspx");

}

1 个答案:

答案 0 :(得分:0)

SelectedValue不会以这种方式工作。而不是DDL_DeliverPartTo.SelectedValue = returnValue.ToString();尝试:

DDL_DeliverPartTo.SelectedIndex = DDL_DeliverPartTo.Items.IndexOf(DDL_DeliverPartTo.Items.FindByValue(returnValue.ToString()));

编辑:您必须在GridView_RowDataBound和datarow中找到控件。请尝试以下代码:

void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        try
        {
            GridViewRow gr = e.Row;
            HiddenField RequestNumber = (HiddenField)gr.FindControl("RequestNumber");
            string ReqNum = RequestNumber.Value;
            string query = "SELECT [DeliverPartTo] FROM [Process_WarrantyTracker] WHERE [RequestNumber] = @RequestNumber ";
            DropDownList DDL_DeliverPartTo = (DropDownList)gr.FindControl("DDL_DeliverPartTo");
            SqlConnection connectionString = new SqlConnection(ConfigurationManager.ConnectionStrings["inventory_v2ConnectionString"].ConnectionString);
            connectionString.Open();
            SqlCommand cmd = new SqlCommand(query, connectionString);
            Object returnValue;
            cmd.CommandText = query;
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@RequestNumber", ReqNum);
            cmd.Connection = connectionString;
            returnValue = cmd.ExecuteScalar();
            connectionString.Close();
           DDL_DeliverPartTo.SelectedIndex = DDL_DeliverPartTo.Items.IndexOf(DDL_DeliverPartTo.Items.FindByValue(returnValue.ToString()));
        }
        catch (Exception ex)
        {
            //Response.Write(ex.Message);
        }
    }
}